shopt -s expand_aliases

xl () {
 ls -lFhXv --color=auto --group-directories-first
}

xla () {
 ls -lFhXvA --color=auto --group-directories-first
}

xx () {
 local path=${*:-'.'}
 clear
 cd ${path} && ls -Xv --color=auto --group-directories-first
}

xh () {
 local path=${*:-'.'}
 clear
 cd ${path} && ls -XvA --color=auto --group-directories-first -d .*
}

xa () {
 local path=${*:-'.'}
 clear
 cd ${path} && ls -XvA --color=auto --group-directories-first
}

h () {
 history -n
 history | grep ${*:-''}
}

f () {
 clear
 grep -d skip -C3 -HITREns --color -e $* .
}

t () {
 local level=${*:-'2'}
 clear
 tree -L ${level}
}

v () {
 feh -xdN -B black -R 1 --keep-zoom-vp --force-aliasing -e "yudit/8" ${*}
}

alias tunnelout='autossh -f -nNT -R 9700:localhost:22'
alias untunnelout='killall autossh'
alias tunnelin='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -p 9700 localhost'

export EDITOR=nvim

alias sudo='sudo '
alias rm='rm -viI'
alias mv='mv -vi'
alias cp='cp -vip'
alias gx='xdg-open'
alias e='nvim'
alias vi='nvim'
alias vim='nvim'
alias p='cat'
alias b='bat'
alias c='clear'
alias x='xx'
alias ..='x ..'
alias ~='x ~'
alias less='less -R'
# alias xf='vifm'
xf () {
 local dst="$(command vifm --choose-dir - "$@")"
 if [ -z "$dst" ]; then
  echo 'Directory picking cancelled/failed'
  return 1
 fi
 cd "$dst"
}
alias network='nmtui'
alias m='sshfs -o allow_other'
alias um='sudo umount'
alias sd='sudo shutdown -h now'
alias hi='killall xflux; redshift -x'
alias lo='killall xflux; redshift -O 2000'
alias g='googler'
alias www='w3m -N duckduckgo.com read.jrvieira.com/quote'
alias irc='vim -c "term irssi"'
alias ascii='ascii-image-converter -Cc'
alias fzf='fzf --reverse --preview "bat --style=numbers --color=always --theme=Nord {}"'
alias vzf='fzf --reverse --preview "ascii-image-converter -Cc -W 119 {}"'
z () {
 local path=${*:-'/'}
 cd $(dirname -- $(find ${path} 2>/dev/null | fzf --reverse --preview "bat --style=numbers --color=always --theme=Nord {}"))
}
zd () {
 local path=${*:-'/'}
 cd $(find ${path} -type d 2>/dev/null | fzf --reverse --preview "bat --style=numbers --color=always --theme=Nord {}")
}

alias gl='git log --stat'
alias gh='git log -c'
alias gg='git log --graph --all --oneline --decorate'
alias gs='git status -sb'
alias gd='git diff --histogram -U1 --ignore-space-at-eol'
alias gc='git commit'
alias gb='git branch'
alias go='git checkout'
ga () {
 git pull && git add -A && git commit -a -m"${*}" && git push
}

diff () {
 colordiff -y ${*} | less -R
}

function extract {
 if [ -z "$1" ]; then
  # display usage if no parameters given
  echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
 else
  if [ -f $1 ] ; then
   # NAME=${1%.*}
   # mkdir $NAME && cd $NAME
   case $1 in
    *.tar.bz2)   tar xvjf ./$1    ;;
    *.tar.gz)    tar xvzf ./$1    ;;
    *.tar.xz)    tar xvJf ./$1    ;;
    *.lzma)      unlzma ./$1      ;;
    *.bz2)       bunzip2 ./$1     ;;
    *.rar)       unrar x -ad ./$1 ;;
    *.gz)        gunzip ./$1      ;;
    *.tar)       tar xvf ./$1     ;;
    *.tbz2)      tar xvjf ./$1    ;;
    *.tgz)       tar xvzf ./$1    ;;
    *.zip)       unzip ./$1       ;;
    *.Z)         uncompress ./$1  ;;
    *.7z)        7z x ./$1        ;;
    *.xz)        unxz ./$1        ;;
    *.exe)       cabextract ./$1  ;;
    *)           echo "extract: '$1' - unknown archive method" ;;
   esac
  else
   echo "$1 - file does not exist"
  fi
 fi
}

xtr () {
 extract $1 && rm -f $1
}

paste () {

 function write {
  echo -n $1 | xclip -sel clip ; echo $1
 }

 write $(curl -s -X POST -H "Content-Type: text/plain" --data-binary "@${*}" https://paste.jrvieira.com/paste)

}

shot () {

 function write {
  echo -n $1 | xclip -sel clip && echo $1
 }

 file=${HOME}/$(date +%s).png

 maim -s -u --quality=10 ${file} && write $(curl -s -F "image=@${file}" https://shot.jrvieira.com/shot) && \rm ${file}

}

alias watch='watch --color --interval=0.1'

o () {
 watch --no-title --color --interval=0.1 --differences ${*}
}

ot () {
 watch --no-title --color --interval=${*}
}

os () {
 watch --no-title --color --interval=1 --differences ${*}
}

d () {
 watch --color --interval=0.1 --differences=permanent ${*}
}

rust () {
 local path=${*:-'.'}
 c; cargo run --manifest-path ${path}/Cargo.toml
}

haskell () {
 ghc ${*} -O2 -threaded -Wall -fforce-recomp -j -no-keep-hi-files -no-keep-o-files && echo "compiled (O2, threaded)" && echo "running..." && ./${*} # ghc --make ${*}.hs && ./${*}
}

hask () {
 ghc ${*} -j -no-keep-hi-files -no-keep-o-files
}

hs () {
 # local file=${*-${PWD##*/}}
 local file=${*}
 ghcid --warnings --no-status --run --test-message="
" --clear --no-height-limit ${file}
}

hstest () {
 # local file=${*-${PWD##*/}}
 local file=${*}
 ghcid --warnings --lint --no-status --test=test --test-message="
" --clear --no-height-limit ${file}
}

hsprof () {

 # i ghc-prof

 local file=${*-${PWD##*/}}

 mkdir prof
 cd prof
 cp ../${file}.hs ${file}.hs
 ghc --make ${file} -O2 -prof -fprof-auto -rtsopts -fforce-recomp

 ./${file} +RTS -p -hc -i0.0001
 /usr/lib/ghc/bin/hp2ps -c ${file}
 mv ${file}.ps ${file}-hc.ps

 ./${file} +RTS -p -hy -i0.0001
 /usr/lib/ghc/bin/hp2ps -c ${file}
 mv ${file}.ps ${file}-hy.ps

 cd ..

 echo "Done."
}

haskstack () {
 local file=${*:-${PWD##*/}}
 c;
 o "stack ghc ${file}.hs && ./${file}"
}

haskstack-build () {
 c;
 stack build --file-watch --verbosity warn --exec "stack exec --profile ${PWD##*/}-exe"
}

haskstack-run () {
 c;
 watch --color --interval=0.1 --differences "stack run --verbosity warn --allow-different-user"
}

haskstack-test () {
 c;
 watch --color --interval=0.1 --differences  "stack test --fast --verbosity silent --allow-different-user"
}

xn ()
{
    if [ -n $NNNLVL ] && [ "${NNNLVL:-0}" -ge 1 ]; then
        echo "nnn is already running"
        return
    fi

    export NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd"

    nnn "$@"

    if [ -f "$NNN_TMPFILE" ]; then
            . "$NNN_TMPFILE"
            \rm -f "$NNN_TMPFILE" > /dev/null
    fi
}