# Remove older command from the history if a duplicate is to be added.
setopt HIST_IGNORE_ALL_DUPS

# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -v

# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}

# Disable automatic widget re-binding on each precmd. This can be set when
# zsh-users/zsh-autosuggestions is the last module in your ~/.zimrc.
ZSH_AUTOSUGGEST_MANUAL_REBIND=1

# Set what highlighters will be used.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)

ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim

# Download zimfw plugin manager if missing.
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
  if (( ${+commands[curl]} )); then
    curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
        https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
  else
    mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh \
        https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
  fi
fi

# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
  source ${ZIM_HOME}/zimfw.zsh init -q
fi

# Initialize modules.
source ${ZIM_HOME}/init.zsh

# zsh-history-substring-search
zmodload -F zsh/terminfo +p:terminfo

# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
for key ('^[[A' '^P' ${terminfo[kcuu1]}) bindkey ${key} history-substring-search-up
for key ('^[[B' '^N' ${terminfo[kcud1]}) bindkey ${key} history-substring-search-down
for key ('k') bindkey -M vicmd ${key} history-substring-search-up
for key ('j') bindkey -M vicmd ${key} history-substring-search-down
unset key

srcs=(
  "/opt/miniforge3/etc/profile.d/conda.sh"
  "/opt/miniforge3/etc/profile.d/mamba.sh"
  "$HOME/.cargo/env"
  "$HOME/.zshklaviyo"
)

for fp in $srcs; do
  if [[ -s "$fp" ]]; then
    source "$fp"
  fi
done

alias i="ipython3"
alias devlog="cd ~/projects/personal/devlog && hx logs/$(date '+%Y-%m-%d').gmi && cd -"

{{ if eq .chezmoi.hostname "rattusrattus" -}}
alias colima_start="colima start --memory 8 --vm-type vz --mount-type virtiofs --dns 8.8.8.8 --dns 8.8.4.4"
{{- end }}

{{- if eq .chezmoi.hostname "devterm" }}
function battery() {
  upower -i /org/freedesktop/UPower/devices/battery_axp20x_battery
}

function battery_percentage() {
  battery | grep 'percentage' | awk '{print $2}'
}

function battery_time_to_empty() {
  battery | grep 'time to empty' | awk '{print $4, $5}'
}

export LPDEST=devterm_printer
alias receipt="lp"

function receipt_feed () {
  echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" > /tmp/DEVTERM_PRINTER_IN
}

function brightness_half {
  brightnessctl set 5
}

function brightness_max {
  brightnessctl set 9
}

function brightness_min {
  brightnessctl set 1
}

# ttyscheme
{{- end -}}

{{ if eq .hosttype "work" -}}
eval "$(/opt/homebrew/bin/brew shellenv)"
{{ end }}
eval "$(direnv hook zsh)"
eval "$(jj util completion zsh)"

function reldate() {
    date -v "${1}" "+%Y-%m-%d"
}

function _zkhx() {
    hx --hsplit tasks.xit ${1}
}

[ "$TERM" = "xterm-kitty" ] && alias ssh="TERM=xterm ssh"

alias strip_jira="sed 's/\[\{0,1\}[a-zA-Z]*-[0-9]*\]\{0,1\}//g'"