# print username in color palette, one color per character
_ps1_print_user() {
    printf '%s' "\[\e[1;32m\]$USER"
}

_ps1_print_host() {
    if [ -f /etc/hostname ] ; then
        read -r _hostname < /etc/hostname
        printf '%s' "\[\e[1;34m\]${_hostname%%.*}"
    else
        printf '%s' "\[\e[1;34m\]$(/bin/hostname -s)"
    fi
}

# print username in color palette, one color per character
_ps1_print_rainbow_user() {
    code=1
    printf '%s\n' "$USER" | fold -w 1 | \
    while read -r c ; do
        printf '%s' "\[\e[1;3${code}m\]$c"
        code=$(( code + 1 ))
    done
    printf '%s' "\[\e[1;3$(( ${#USER} + 1 ))m\]"
}

_ps1_print_pwd() {
    printf '['
    printf '%s' "\[\e[1;36m\]\W"
    _clear_formatting
    printf ']'
}

_ps1_print_prompt() {
    printf '%s ' "\[\e[1;33m\]"
}

_ps1_print_git() {
    # print git repo name in parenthesis, if we're inside one
    if [ -d .git ] ; then
        set -- "$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2>/dev/null)"
        if [ "$1" ] ; then
            printf '(%s)' "🐙 $1"
            ##### 
        else
            printf '(%s)' "🐙 no branch"
        fi
    fi
}

_clear_formatting() {
    printf '%s' '\[\e[1;37m\]'
}

_ps1_print() {
    case $TERM in
        *-256color|*-kitty)
            _clear_formatting

            case ${PS1_STYLE:-user-pwd} in
                user-host-pwd)
                    _ps1_print_user
                    _clear_formatting
                    printf '@'
                    _ps1_print_host
                    # printf ' '
                    _clear_formatting
                    _ps1_print_pwd
                    ;;
                user-pwd)
                    _ps1_print_user
                    # printf ' '
                    _ps1_print_pwd
                    printf ' '
                    ;;
                user)
                    _ps1_print_user
                    ;;
                pwd|pwd-no-prompt)
                    _ps1_print_pwd
                    ;;
                percent)
                    printf '%'
            esac

            if [ "${PS1_GIT:-false}" = true ] && [ -d .git ] ; then
                _ps1_print_git
            fi

            _clear_formatting
            if [ "$PS1_STYLE" != pwd-no-prompt ] ; then
                _ps1_print_prompt
            else
                printf ' '
            fi
            _clear_formatting
            ;;
        dumb)
            printf '%'
            ;;
        *)
            printf '%s %s $ ' "$USER" "$PWD"
    esac

    export PS1="$PS1"
}

# check if we're in ranger
[ "$RANGER_LEVEL" ] && clear

# this activates our PS1
cd .

alias ps1='export PS1="% "'
