<commit_before>#!/bin/bash

# An intelligent an non intrusive prompt for bash

# Licensed under the AGPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 2012 (c) nojhan <<EMAIL>>


# Below are function for having a flexible dynamic prompt for power user:
# - display the colored hostname when connected via ssh
# - when root, color in yellow the [username path] and in red the sharp
# - when user, print the colored git branch name (if in a git repository): 
#     red = some changes are not commited, yellow = some commits are not pushed, 
#     green = no changes or commits
# - if necessary, print a counter for jobs that are attached to the current term (e.g. xterm &)
#     or that are sleeping (e.g. Ctrl-z)
# - display the load average, colored with a colormap
# - display the battery level (if necessary), with colormap

# Some colors
BLUE="\[\033[0;34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_CYAN="\[\033[1;36m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
RED="\[\033[0;31m\]"
NO_COL="\[\033[00m\]"


#################
# Where are we? #
#################

THIS_TTY=tty`ps aux | grep $$ | grep bash | awk '{ print $7 }'`
SESS_SRC=`who | grep $THIS_TTY | awk '{ print $6 }'`

# Are we in an SSH connexion?
SSH_FLAG=0
SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
if [ $SSH_IP ] ; then
  SSH_FLAG=1
fi
SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
if [ $SSH2_IP ] ; then
  SSH_FLAG=1
fi
if [ $SSH_FLAG -eq 1 ] ; then
  CONN=ssh
elif [ -z $SESS_SRC ] ; then
  CONN=lcl
elif [ $SESS_SRC = "(:0.0)" -o $SESS_SRC = "" ] ; then
  CONN=lcl
else
  CONN=tel
fi


###############
# Who are we? #
###############

if [ `/usr/bin/whoami` = "root" ] ; then
  USR=u_root
else
  USR=nou_root
fi


#####################################
# Count the number of attached jobs #
#####################################

# Either attached running jobs (started with $ myjob &)
# or attached stopped jobs (suspended with Ctrl-Z)
jobcount()
{
    rep=""
    running=`jobs -r | wc -l | tr -d " "`
    stopped=`jobs -s | wc -l | tr -d " "`

    if [ $running != "0" -a $stopped != "0" ] ; then
        rep=" ${running}r/${stopped}s"

    elif [ $running != "0" -a $stopped == "0" ] ; then
        rep=" ${running}r"

    elif [ $running == "0" -a $stopped != "0" ] ; then
        rep=" ${stopped}s"
    fi

    echo -ne "$rep"
}

######################
# GIT branch display #
######################

# Get the branch name of the current directory
git_branch()
{
    if git rev-parse --git-dir >/dev/null 2>&1 ; then
        gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
    else
        return 0
    fi
    echo -e "$gitver"
}

# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
git_branch_color()
{
    if git rev-parse --git-dir >/dev/null 2>&1 ; then
        red=`tput setaf 1`
        green=`tput setaf 2`
        yellow=`tput setaf 3`
     
        color=""
        if git diff --quiet 2>/dev/null >&2 ; then
            branch="$(git_branch)"
            has_commit=`git rev-list origin/$branch..$branch`
            if [ "$has_commit" != "" ] ; then
                color="${yellow}" # some commits to push
            else
                color="${green}" # nothing to commit or push
            fi
        else
            color="${red}" # changes to commit
        fi
    else
        return 0
    fi
    echo -ne $color
}


##################
# Battery status #
##################

# Get the battery status in percent
battery()
{
    bat=`acpi --battery | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/"`
    if [ ${bat} -lt 90 ] ; then
        echo -n " ${bat}%"
    else
        echo -n ""
    fi
}

# Compute a gradient of background/forground colors depending on the battery status
battery_color()
{
    gray=0
    red=1
    green=2<commit_msg>Prompt.bash : Verify if acpi command exists to avoid error message.<commit_after>#!/bin/bash

# An intelligent an non intrusive prompt for bash

# Licensed under the AGPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 2012 (c) nojhan <<EMAIL>>


# Below are function for having a flexible dynamic prompt for power user:
# - display the colored hostname when connected via ssh
# - when root, color in yellow the [username path] and in red the sharp
# - when user, print the colored git branch name (if in a git repository): 
#     red = some changes are not commited, yellow = some commits are not pushed, 
#     green = no changes or commits
# - if necessary, print a counter for jobs that are attached to the current term (e.g. xterm &)
#     or that are sleeping (e.g. Ctrl-z)
# - display the load average, colored with a colormap
# - display the battery level (if necessary), with colormap

# Some colors
BLUE="\[\033[0;34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_CYAN="\[\033[1;36m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
RED="\[\033[0;31m\]"
NO_COL="\[\033[00m\]"


#################
# Where are we? #
#################

THIS_TTY=tty`ps aux | grep $$ | grep bash | awk '{ print $7 }'`
SESS_SRC=`who | grep $THIS_TTY | awk '{ print $6 }'`

# Are we in an SSH connexion?
SSH_FLAG=0
SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
if [ $SSH_IP ] ; then
  SSH_FLAG=1
fi
SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
if [ $SSH2_IP ] ; then
  SSH_FLAG=1
fi
if [ $SSH_FLAG -eq 1 ] ; then
  CONN=ssh
elif [ -z $SESS_SRC ] ; then
  CONN=lcl
elif [ $SESS_SRC = "(:0.0)" -o $SESS_SRC = "" ] ; then
  CONN=lcl
else
  CONN=tel
fi


###############
# Who are we? #
###############

if [ `/usr/bin/whoami` = "root" ] ; then
  USR=u_root
else
  USR=nou_root
fi


#####################################
# Count the number of attached jobs #
#####################################

# Either attached running jobs (started with $ myjob &)
# or attached stopped jobs (suspended with Ctrl-Z)
jobcount()
{
    rep=""
    running=`jobs -r | wc -l | tr -d " "`
    stopped=`jobs -s | wc -l | tr -d " "`

    if [ $running != "0" -a $stopped != "0" ] ; then
        rep=" ${running}r/${stopped}s"

    elif [ $running != "0" -a $stopped == "0" ] ; then
        rep=" ${running}r"

    elif [ $running == "0" -a $stopped != "0" ] ; then
        rep=" ${stopped}s"
    fi

    echo -ne "$rep"
}

######################
# GIT branch display #
######################

# Get the branch name of the current directory
git_branch()
{
    if git rev-parse --git-dir >/dev/null 2>&1 ; then
        gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
    else
        return 0
    fi
    echo -e "$gitver"
}

# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
git_branch_color()
{
    if git rev-parse --git-dir >/dev/null 2>&1 ; then
        red=`tput setaf 1`
        green=`tput setaf 2`
        yellow=`tput setaf 3`
     
        color=""
        if git diff --quiet 2>/dev/null >&2 ; then
            branch="$(git_branch)"
            has_commit=`git rev-list origin/$branch..$branch`
            if [ "$has_commit" != "" ] ; then
                color="${yellow}" # some commits to push
            else
                color="${green}" # nothing to commit or push
            fi
        else
            color="${red}" # changes to commit
        fi
    else
        return 0
    fi
    echo -ne $color
}


##################
# Battery status #
##################

# Get the battery status in percent
battery()
{
    command -v acpi >/dev/null 2>&1 || { echo -n ""; return; }
    bat=`acpi --battery | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/"`
    if [ ${bat} -lt 90 ] ; then
        echo -n " ${bat}%"
    else
        echo -n ""
    fi
}

# Compute a gradient of background/forground colors depending on the battery status
battery_color()
{
    gray=0
    red=1
    green=2
{'fraction_non_alphanumeric': 0.1775366250678242, 'fraction_numerical': 0.02148670645686381, 'mean_word_length': 2.7941539728283242, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1587458', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3043, 'n_words': 1090}