#!/usr/bin/env bash

#
# Bash aliases and customizations
#

# Shell options

shopt -s expand_aliases
shopt -s histappend

HISTCONTROL=ignoreboth:erasedups

# Prompt customization

if command -v powerline-go > /dev/null
then
    INTERACTIVE_BASHPID_TIMER="${HOME}/.cache/bash_timer_$$"

    PS0='$(echo $SECONDS > "$INTERACTIVE_BASHPID_TIMER")'

    function _update_ps1() {
        local __ERRCODE=$?

        local __DURATION=0
        if [ -e "$INTERACTIVE_BASHPID_TIMER" ]; then
            local __END=$SECONDS
            local __START=$(cat "$INTERACTIVE_BASHPID_TIMER")
            __DURATION="$(($__END - ${__START:-__END}))"
            \rm -f "$INTERACTIVE_BASHPID_TIMER"
        fi

        echo -en "\033]0; ${USER}@${HOSTNAME} $PWD\007"
        # echo -en "… $\r"
        eval "$(powerline-go -shell bash -eval -duration $__DURATION -error $__ERRCODE "${POWERLINE_GO_DEFAULT_OPTS[@]}")"
    }

    PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"

else
    export PS1="\[\e]2;\u@\H \w\a\]\[\e[0;37m\][\[\e[0;${col}m\]\u\[\e[0;37m\]@\[\e[0;34m\]\h \[\e[0;36m\]\W\[\e[0;37m\]]\$\[\e[0m\] "
fi

# Completion
trysource /usr/share/bash-completion/bash_completion

# Fuzzy matching all the way
trysource /usr/share/fzf/completion.bash
trysource /usr/share/fzf/key-bindings.bash