# Device-specifig setup (ignored by git)
# source local config first to overwrite default theme if wanted
[[ ! -f ~/.zsh_local ]] || source ~/.zsh_local

### U S E R  C O N F I G ###

# random string function
random-string()
{
  LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | fold -w ${1:-32} | head -n 1
}

# make code printable with pandocs
# printable-code filename.ext [forced-extension]
printable-code()
{
  body=`cat ${1}`
  [[ ${1} =~ "([^.]+).([^.]+)" ]] && name=$match[1] && ext=$match[2]
  ext=`test -n "${2}" && echo ${2} || echo $ext`
  doc="# ${1}\n\`\`\`$ext\n$body\n\`\`\`"
  oformat=`test -n "${3}" && echo ${3} || echo "pdf"`
  echo $doc | pandoc -o "$name.$oformat"
}

# help creating links to emails
# in apple mail, go to viewing → Show message headers → Custom... → Add "Message-ID"
function murl () {
    echo message://"%3c"$@"%3e"
}


#
# Aliases
#

# Git
alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias glola="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"
alias imgcat="~/.dotfiles/imgcat"

# Utility
alias rm="nocorrect rm"

# bitwarden: ensure a valid BW_SESSION, caching it in the macOS keychain.
# reads cached session, validates, and unlocks/logs in only if needed.
bwl() {
  local svc="BW_SESSION"
  export BW_SESSION="$(security find-generic-password -a "$USER" -s "$svc" -w 2>/dev/null)"

  local bw_status
  bw_status="$(bw status --session "$BW_SESSION" 2>/dev/null | jq -r '.status')"

  case "$bw_status" in
    unlocked)
      echo "bw session valid" ;;
    locked|"")
      BW_SESSION="$(bw unlock --raw)" || return 1
      security add-generic-password -U -a "$USER" -s "$svc" -w "$BW_SESSION"
      export BW_SESSION
      echo "bw session unlocked + cached" ;;
    unauthenticated)
      BW_SESSION="$(bw login --raw)" || return 1
      security add-generic-password -U -a "$USER" -s "$svc" -w "$BW_SESSION"
      export BW_SESSION
      echo "bw session logged in + cached" ;;
  esac
}


#
# Python
#

alias pip_update_all="pip install -r <(pip freeze | sed 's|==.*||') -U"


#
# direnv
#

eval "$(direnv hook zsh)"


# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# load homebrew autocompletions
if [[ -d /opt/homebrew/share/zsh/site-functions ]]; then
  fpath=(/opt/homebrew/share/zsh/site-functions $fpath)
fi

# load custom completions
fpath=(~/.zfunc $fpath)

zmodload zsh/complist

# zplug load
if which sheldon > /dev/null; then
  eval "$(sheldon source)"
fi

zstyle ':completion:*' menu select

[[ ! -f ~/.zsh_local_completions ]] || source ~/.zsh_local_completions

# Better cp -- must go after compinit and stuff
eval "$(zoxide init zsh)"

# vi mode
bindkey -v
export KEYTIMEOUT=1

# Use vim keys in tab complete menu:
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
bindkey -v '^?' backward-delete-char

# Store if light or dark mode is active
if [[ $(defaults read -g AppleInterfaceStyle 2&> /dev/null) == "Dark" ]]
then
    UIMODE="dark"
else
    UIMODE="light"
fi
export TASKRC=~/.taskrc-${UIMODE}
