<commit_before>#! /bin/cat -n

export FAIL_COLOUR=red
export PASS_COLOUR=green


get_git_status() {
    local branch_=$1; shift
    local bump_version_=$1; shift
    [[ $bump_version_ =~ [0-9] ]] && branch_="$branch_ v$bump_version_"
    local modified_=$(quietly git status --porcelain | wc -l | tr -d ' ')
    local remote="$(git config --get branch.${branch_}.remote 2>/dev/null)"
    local remote_branch_="$(git config --get branch.${branch_}.merge)"
    local pushes_=$(git rev-list --count ${remote_branch_/refs\/heads/refs\/remotes\/$remote}..HEAD 2>/dev/null)
    [[ -z $pushes_ ]] && pushes_=?
    local pulls_=$(git rev-list --count HEAD..${remote_branch_/refs\/heads/refs\/remotes\/$remote} 2>/dev/null)
    [[ -z $pulls_ ]] && pulls_=?
    local short_branch_=$branch_
    [[ $branch_ =~ [A-Z][A-Z][A-Z][A-Z][-] ]] && short_branch_=$(echo $branch | sed -e "s,\(....-[0-9]*\).*,\1,")
    if [[ $modified_ == 0 && $pushes_ == 0 && $pulls_ == 0 ]]; then
        echo $short_branch_
        return 0
    fi
    if [[ $pulls_ == 0 ]]; then
        if [[ $pushes_ == 0 ]]; then
            short_branch_="$short_branch_ $modified_"
        else
            short_branch_="$short_branch_ $modified_+$pushes_"
        fi
    else
        short_branch_="$short_branch_ $modified_+$pushes-$pulls_"
    fi
    echo $short_branch_
    return 0
}

_rgb () {
    local one_="$1"; shift
    local light_= no_colour_="\033[0m"
    local regexp_="red|green|blue|cyan|magenta|yellow"
    local hue_=$one_
    if [[ "$one_" =~ ^l_ ]]; then
        light_="LIGHT_"
        [[ $one_ =~ l_$regexp_ ]] && hue_=${one_/l_/}
    fi
    local var_=
    if [[ $hue_ =~ $regexp_ ]]; then
        [[ $hue_ == red ]] && var_="${light_}RED"
        [[ $hue_ == green ]] && var_="${light_}GREEN"
        [[ $hue_ == blue ]] && var_="${light_}BLUE"
        [[ $hue_ == cyan ]] && var_="${light_}CYAN"
        [[ $hue_ == magenta ]] && var_="${light_}MAGENTA"
        [[ $hue_ == yellow ]] && var_="${light_}YELLOW"
    fi
    local hues_=${!var_}
    echo -n "$hues_""$@""$no_colour_"
}

_blue () {
    _rgb blue "$@"
}

_green () {
    _rgb green "$@"
}

_l_blue () {
    _rgb l_blue "$@"
}

_l_green () {
    _rgb l_green "$@"
}

_l_red () {
    _rgb l_red "$@"
}

emoji_error () {
    if [[ $1 == 0 ]]; then
        echo "😎 "
    else
        local _faces=(👿 👎 💀 👻 💩 😢 😥 😰 😮 😫 😲 ☹️  😤 😭 😦 😧 😨 😩 🤯 😬 😱 🥵 🥶 😳 🤢 🤮 🤨 😐 😑 )
        echo "${_faces[$1]} "
    fi
}

_colour_prompt () {
    local __doc__="""Use a coloured prompt with helpful info"""
    printf "$(emoji_error $1) $(blue_date) $(blue_user):$(blue_pwd_git) $(red_python)\n$ "
    #printf "$(emoji_error $1) $(blue_date) $(blue_user):$(blue_pwd_git) $(red_python)\n\[$(iterm2_prompt_mark)\]$ "
}

path_to_venv () {
    env | grep VIRTUAL_ENV= | cut -d= -f2
}

venv_name () {
    local _path_to_venv=$(path_to_venv)
    [[ -e $_path_to_venv ]] || return 1
    local _venv_name=$(basename "$_path_to_venv")
    if [[ $_venv_name == ".venv" ]]; then
        local _parent=$(dirname $_path_to_venv)
        local _parent_name=$(basename $_parent)
        _venv_name=$_parent_name
    fi
    echo $(_l_red "${_venv_name/./}")
}

blue_date () {
    local _colour_day=$(_rgb blue $(date +'%A'))
    local _colour_date=$(_rgb l_blue $(date +' %F %H:%M'))
    echo $_colour_day $_colour_date
}

short_pwd () {
    echo $(PYTHONPATH="$HOME/pysyte/" ~/pysyte/bin/short_dir "$PWD" 2> /dev/null)
}

blue_pwd_git () {
    local _branch_name="$(get_branch)"
    local _version=
    if [[ $_branch_name ]]; then
        local _bump_version="v$(bump get)"
        [[ $_bump_version == v ]] && _bump_version=
        _version=", $_branch_name $_bump_version"
    fi
    local pwd_="$(short_pwd)"
    [[ $pwd_ ]] || pwd_=$(basename "$(readlink -f .)")
    local project_=$(git remote get-url origin 2>/dev/null | sed -e "s,.*/\([a-z.]*\)/\([a-z.]*\)[.git]*,\1/\2,")
    [[ $project_ ]] && pwd_="${project_}:${pwd_}"
    echo $(_l_blue "${pwd_}${_version}")
}

blue_user () {
    local user_=$(whoami)
    local hostname_=$(hostname -s)
    local _hue_user=$(_rgb blue ${user_:$USER})
    local _hue_host=$(_rgb blue ${hostname_:-$HOSTNAME})
    echo "${_hue_user}@$_hue_host"
}

red_python () {

    local _virtual_env_name=
    local _virtual_env_root=$(env | grep VIRTUAL_ENV | cut -d= -f2)
    [[ $_virtual_env_root ]] && _virtual_env_name=$(basename "$_virtual_env_root")
    if [[ $_virtual_env_name == ".venv" ]]; then
        local _virtual_env_directory=$(dirname $_virtual_env_root)
        _virtual_env_name=$(basename "$_virtual_env_directory")
    fi

    local _python_version=$(python -V 2>&1 | head -n1 | cut -d' ' -f2)
    local _colour_python=$(_l_red "${_python_version}")
    local _colour_venv=$(venv_name)
    if [[ ! $_colour_venv ]]; then
        echo $_colour_python
        return 0
    fi
    local _path_to_venv=$(path_to_venv)
    local _join='/'
    [[ $_path_to_venv =~ ~ ]] && _join='~'
    [[ $_path_to_venv =~ ^[.] ]] && _join='.'
    echo "${_colour_python}${_join}${_colour_venv}"
}

set_status_bit () {
    local __doc__="""Set the status bit from $?"""<commit_msg>Tidy colouring of prompt<commit_after>#! /bin/cat -n

export FAIL_COLOUR=red
export PASS_COLOUR=green


get_git_status() {
    local branch_=$1; shift
    local bump_version_=$1; shift
    [[ $bump_version_ =~ [0-9] ]] && branch_="$branch_ v$bump_version_"
    local modified_=$(quietly git status --porcelain | wc -l | tr -d ' ')
    local remote="$(git config --get branch.${branch_}.remote 2>/dev/null)"
    local remote_branch_="$(git config --get branch.${branch_}.merge)"
    local pushes_=$(git rev-list --count ${remote_branch_/refs\/heads/refs\/remotes\/$remote}..HEAD 2>/dev/null)
    [[ -z $pushes_ ]] && pushes_=?
    local pulls_=$(git rev-list --count HEAD..${remote_branch_/refs\/heads/refs\/remotes\/$remote} 2>/dev/null)
    [[ -z $pulls_ ]] && pulls_=?
    local short_branch_=$branch_
    [[ $branch_ =~ [A-Z][A-Z][A-Z][A-Z][-] ]] && short_branch_=$(echo $branch | sed -e "s,\(....-[0-9]*\).*,\1,")
    if [[ $modified_ == 0 && $pushes_ == 0 && $pulls_ == 0 ]]; then
        echo $short_branch_
        return 0
    fi
    if [[ $pulls_ == 0 ]]; then
        if [[ $pushes_ == 0 ]]; then
            short_branch_="$short_branch_ $modified_"
        else
            short_branch_="$short_branch_ $modified_+$pushes_"
        fi
    else
        short_branch_="$short_branch_ $modified_+$pushes-$pulls_"
    fi
    echo $short_branch_
    return 0
}

_rgb () {
    local one_="$1"; shift
    local light_=NIGHT_ no_colour_="\033[0m"
    local regexp_="red|green|blue|cyan|magenta|yellow"
    local hue_=$one_
    if [[ "$one_" =~ ^l_ ]]; then
        light_="LIGHT_"
        [[ $one_ =~ l_$regexp_ ]] && hue_=${one_/l_/}
    fi
    local var_=
    if [[ $hue_ =~ $regexp_ ]]; then
        [[ $hue_ == red ]] && var_="${light_}RED"
        [[ $hue_ == green ]] && var_="${light_}GREEN"
        [[ $hue_ == blue ]] && var_="${light_}BLUE"
        [[ $hue_ == cyan ]] && var_="${light_}CYAN"
        [[ $hue_ == magenta ]] && var_="${light_}MAGENTA"
        [[ $hue_ == yellow ]] && var_="${light_}YELLOW"
    fi
    local hues_=${!var_}
    echo -n "$hues_""$@""$no_colour_"
}

emoji_error () {
    if [[ $1 == 0 ]]; then
        echo "😎 "
    else
        local _faces=(👿 👎 💀 👻 💩 😢 😥 😰 😮 😫 😲 ☹️  😤 😭 😦 😧 😨 😩 🤯 😬 😱 🥵 🥶 😳 🤢 🤮 🤨 😐 😑 )
        echo "${_faces[$1]} "
    fi
}

_colour_prompt () {
    local __doc__="""Use a coloured prompt with helpful info"""
    printf "\n$(emoji_error $1) $(blue_date) $(blue_user):$(blue_pwd_git) $(red_python)\n$ "
    #printf "$(emoji_error $1) $(blue_date) $(blue_user):$(blue_pwd_git) $(red_python)\n\[$(iterm2_prompt_mark)\]$ "
}

path_to_venv () {
    env | grep VIRTUAL_ENV= | cut -d= -f2
}

venv_name () {
    local _path_to_venv=$(path_to_venv)
    [[ -e $_path_to_venv ]] || return 1
    local _venv_name=$(basename "$_path_to_venv")
    if [[ $_venv_name == ".venv" ]]; then
        local _parent=$(dirname $_path_to_venv)
        local _parent_name=$(basename $_parent)
        _venv_name=$_parent_name
    fi
    echo $(_rgb l_red "${_venv_name/./}")
}

blue_date () {
    local _colour_day=$(_rgb blue $(date +'%A'))
    local _colour_date=$(_rgb l_blue $(date +' %F %H:%M'))
    echo $_colour_day $_colour_date
}

short_pwd () {
    echo $(PYTHONPATH="$HOME/pysyte/" ~/pysyte/bin/short_dir "$PWD" 2> /dev/null)
}

blue_pwd_git () {
    local _branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
    local _version=
    if [[ $_branch_name ]]; then
        local _bump_version="v$(bump get)"
        [[ $_bump_version == v ]] && _bump_version=
        _version=", $_branch_name $_bump_version"
    fi
    local pwd_="$(short_pwd)"
    [[ $pwd_ ]] || pwd_=$(basename "$(readlink -f .)")
    local project_=$(git remote get-url origin 2>/dev/null | sed -e "s,.*/\([a-z.]*\)/\([a-z.]*\)[.git]*,\1/\2,")
    [[ $project_ ]] && pwd_="${project_}:${pwd_}"
    echo $(_rgb l_blue "${pwd_}${_version}")
}

blue_user () {
    local user_=$(whoami)
    local hostname_=$(hostname -s)
    local _hue_user=$(_rgb blue ${user_:$USER})
    local _hue_host=$(_rgb blue ${hostname_:-$HOSTNAME})
    echo "${_hue_user}@$_hue_host"
}

red_python () {
    local _virtual_env_name=
    local _virtual_env_root=$(env | grep VIRTUAL_ENV | cut -d= -f2)
    [[ $_virtual_env_root ]] && _virtual_env_name=$(basename "$_virtual_env_root")
    if [[ $_virtual_env_name == ".venv" ]]; then
        local _virtual_env_directory=$(dirname $_virtual_env_root)
        _virtual_env_name=$(basename "$_virtual_env_directory")
    fi

    local _python_version=$(python -V 2>&1 | head -n1 | cut -d' ' -f2)
    local _colour_python=$(_rgb l_red "${_python_version}")
    local _colour_venv=$(venv_name)
    if [[ ! $_colour_venv ]]; then
        echo $_colour_python
        return 0
    fi
    local _path_to_venv=$(path_to_venv)
    local _join='/'
    [[ $_path_to_venv =~ ~ ]] && _join='~'
    [[ $_path_to_venv =~ ^[.] ]] && _join='.'
    echo "${_colour_python}${_join}${_colour_venv}"
}

set_status_bit () {
    local __doc__="""Set the status bit from $?"""
{'fraction_non_alphanumeric': 0.1999209798498617, 'fraction_numerical': 0.008000790201501382, 'mean_word_length': 3.00990099009901, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3555613', 'n_tokens_mistral': 4245, 'n_tokens_neox': 3998, 'n_words': 789}