# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTCONTROL=ignoreboth
HISTSIZE=2500
SAVEHIST=2500
setopt appendhistory
bindkey -e
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '${HOME}/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

if [[ -f /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]]; then
  # arch
  . /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
elif [[ -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]]; then
  # fedora
  . /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi

if [[ ! -z "${USERPROFILE}" ]]; then
  HISTFILE="${USERPROFILE}/.${USER}.histfile"
fi

if [[ -d ~/.local/bin ]]; then
  PATH=~/.local/bin:${PATH}
fi

if [[ -d ~/go/bin ]]; then
    PATH=~/go/bin:${PATH}
fi

alias cp="cp -i"                          # confirm before overwriting something
alias df='df -h'                          # human-readable sizes
alias free='free -h'                      # human-readable sizes
alias more=less

case $TERM in
  termite|*xterm*|rxvt|rxvt-unicode|rxvt-256color|rxvt-unicode-256color|(dt|k|E)term)
    precmd () {
      #vcs_info
      print -Pn "\e]0;%n@%M:${PWD}\a"
    }
    preexec () { print -Pn "\e]0;%n@%M:${PWD}\a"}
    ;;
  screen|screen-256color)
    precmd () {
      vcs_info
      print -Pn "\e]83;title \"$1\"\a"
      print -Pn "\e]0;$TERM - (%L) %n@%M:${PWD}\a"
    }
    preexec () {
      print -Pn "\e]83;title \"$1\"\a"
      print -Pn "\e]0;$TERM - (%L) %n@%M:${PWD}\a"
    }
    ;;
esac
autoload -Uz colors && colors
if [[ ${EUID} == 0 ]] ; then
        PROMPT="%{$fg_bold[red]%}[%m %{$fg_bold[white]%}%1~%{$fg_bold[red]%}]%#%{$reset_color%} "
else
        PROMPT="%{$fg_bold[green]%}[%n@%m %{$fg_bold[white]%}%1~%{$fg_bold[green]%}]%#%{$reset_color%} "
fi
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
#RPROMPT="[%{$fg_no_bold[yellow]%}%?%{$reset_color%}]"

precmd() {
    print -Pn "\e]133;A\e\\"
}

typeset -A key

key[Home]=${terminfo[khome]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}

# setup key accordingly
[[ -n "${key[Home]}"     ]]  && bindkey  "${key[Home]}"     beginning-of-line
[[ -n "${key[End]}"      ]]  && bindkey  "${key[End]}"      end-of-line
[[ -n "${key[Insert]}"   ]]  && bindkey  "${key[Insert]}"   overwrite-mode
[[ -n "${key[Delete]}"   ]]  && bindkey  "${key[Delete]}"   delete-char
[[ -n "${key[Up]}"       ]]  && bindkey  "${key[Up]}"       up-line-or-history
[[ -n "${key[Down]}"     ]]  && bindkey  "${key[Down]}"     down-line-or-history
[[ -n "${key[Left]}"     ]]  && bindkey  "${key[Left]}"     backward-char
[[ -n "${key[Right]}"    ]]  && bindkey  "${key[Right]}"    forward-char
[[ -n "${key[PageUp]}"   ]]  && bindkey  "${key[PageUp]}"   beginning-of-history
[[ -n "${key[PageDown]}" ]]  && bindkey  "${key[PageDown]}" end-of-history
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word

# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
    function zle-line-init () {
        echoti smkx
    }
    function zle-line-finish () {
        echoti rmkx
    }
    zle -N zle-line-init
    zle -N zle-line-finish
fi

if [[ $(which-command man) ]]; then
man() {
  env \
    LESS_TERMCAP_mb=$(printf "\e[1;31m") \
    LESS_TERMCAP_md=$(printf "\e[1;31m") \
    LESS_TERMCAP_me=$(printf "\e[0m") \
    LESS_TERMCAP_se=$(printf "\e[0m") \
    LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
    LESS_TERMCAP_ue=$(printf "\e[0m") \
    LESS_TERMCAP_us=$(printf "\e[1;32m") \
    man "$@"
}
fi

if [[ $(which-command pinfo) ]]; then
pinfo() {
  env \
    LESS_TERMCAP_mb=$(printf "\e[1;31m") \
    LESS_TERMCAP_md=$(printf "\e[1;31m") \
    LESS_TERMCAP_me=$(printf "\e[0m") \
    LESS_TERMCAP_se=$(printf "\e[0m") \
    LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
    LESS_TERMCAP_ue=$(printf "\e[0m") \
    LESS_TERMCAP_us=$(printf "\e[1;32m") \
    pinfo "$@"
}
fi

#
# # ex - archive extractor
# # usage: ex <file>
ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       unrar x $1     ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

json2yaml () {
  if [ "${1:--}" = "-" ]; then
    python3 -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))'
  elif [ -f $1 ]; then
    python3 -c "import yaml, json; f = open(\"$1\"); print(yaml.dump(json.loads(f))); f.close()"
  fi
}

yaml2json () {
  if [ "${1:--}" = "-" ]; then
    python3 -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read())))'
  elif [ -f $1 ]; then
    python3 -c "import yaml,json; f = open(\"$1\"); print(json.dumps(yaml.safe_load(f))); f.close()"
  fi
}

toml2json() {
  if [ "${1:--}" = "-" ]; then
    python3 -c 'import sys, tomllib, json; print(json.dumps(tomllib.load(sys.stdin.read())))'
  else
    python3 -c "import sys, tomllib, json; f = open('${1}', "rb"); print(json.dumps(tomllib.load(f))); f.close()"
  fi
}

urlencode () {
  python3 -c "from urllib.parse import quote_plus as quote; print(quote('${1}'))"
}

urldecode () {
  python3 -c "from urllib.parse import unquote_plus as quote; print(unquote('${1}'))"
}

XDG_DATA_DIRS=/var/lib/flatpak/exports/share:${XDG_DATA_DIRS}
