<commit_before>#!/bin/bash

# An intelligent and 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>>
# 2012 (c) <NAME> <<EMAIL>> #major rewrite and adaptation

# Below are function for having a flexible dynamic prompt for power user:
# - display the battery level (if necessary), with colormap (if any battery attached)
# - if necessary, prints a counter for jobs that are attached to the current
#   term (e.g. xterm &) or that are sleeping (e.g. Ctrl-z) or detached "screen" sessions
# - display the load average, colored with a colormap
# - displays the colored login@hostname
#     - when root displays in red
#     - when user displays in green
# - displays the current path in blue
# - when user, print the colored git branch name (if in a git repository):
#     red = some changes are not commited, and yellow in parenthesis any unpushed commits (if any)
#     yellow = some commits are not pushed. Number of commits in parenthesis
#     green = no changes or commits

# Check for recent enough version of bash.
[[ -z "$BASH_VERSION" || -z "$PS1" || -z "$TERM" ]] && return;

bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [[ $bmajor -lt 3 ]] || [[ $bmajor -eq 3 && $bminor -lt 2 ]]; then
    unset bash bmajor bminor
    return
fi
unset bash bmajor bminor


#################
# CONFIGURATION #
#################

# Maximal value under which the battery level is displayed
# Recommended value is 75
BATTERY_THRESHOLD=75

# Minimal value after which the load average is displayed
# Recommended value is 60
LOAD_THRESHOLD=60

# The maximum percentage of the screen width used to display the path
# Recommended value is 35
PATH_LENGTH=35

# How many directories to keep at the beginning of a shortened path
# Recommended value is 2
PATH_KEEP=2

# Do we use reverse colors (black on white) instead of normal theme (white on black)
# Defaults to unset (white on black)
# Otherwise use REVERSE="1" source liquidprompt.bash


###############
# OS specific #
###############

# OS detection, default to Linux
OS="Linux"
case $(uname) in
    "Linux"  ) OS="Linux"   ;;
    "FreeBSD") OS="FreeBSD" ;;
    "Darwin") OS="FreeBSD" ;;
    "DragonFly") OS="FreeBSD" ;;
    "SunOS") OS="SunOS" ;;
esac

# Colors declarations
if [[ "$OS" == "FreeBSD" ]] ; then

    BLACK="\[$(tput AF 0)\]"
    BOLD_GRAY="\[$(tput md ; tput AF 0)\]"
    WHITE="\[$(tput AF 7)\]"
    BOLD_WHITE="\[$(tput md ; tput AF 7)\]"

    RED="\[$(tput AF 1)\]"
    BOLD_RED="\[$(tput md ; tput AF 1)\]"
    WARN_RED="\[$(tput AF 0 ; tput setab 1)\]"
    CRIT_RED="\[$(tput md; tput AF 7 ; tput setab 1)\]"

    GREEN="\[$(tput AF 2)\]"
    BOLD_GREEN="\[$(tput md ; tput AF 2)\]"

    YELLOW="\[$(tput AF 3)\]"
    BOLD_YELLOW="\[$(tput md ; tput AF 3)\]"

    BLUE="\[$(tput AF 4)\]"
    BOLD_BLUE="\[$(tput md ; tput AF 4)\]"

    PURPLE="\[$(tput AF 5)\]"
    PINK="\[$(tput md ; tput AF 5)\]"

    CYAN="\[$(tput AF 6)\]"
    BOLD_CYAN="\[$(tput md ; tput AF 6)\]"

    NO_COL="\[$(tput me)\]"

else
    # default to Linux
    BLACK="\[$(tput setaf 0)\]"
    BOLD_GRAY="\[$(tput bold ; tput setaf 0)\]"
    WHITE="\[$(tput setaf 7)\]"
    BOLD_WHITE="\[$(tput bold ; tput setaf 7)\]"

    RED="\[$(tput setaf 1)\]"
    BOLD_RED="\[$(tput bold ; tput setaf 1)\]"
    WARN_RED="\[$(tput setaf 0 ; tput setab 1)\]"
    CRIT_RED="\[$(tput bold; tput setaf 7 ; tput setab 1)\]"

    GREEN="\[$(tput setaf 2)\]"
    BOLD_GREEN="\[$(tput bold ; tput setaf 2)\]"

    YELLOW="\[$(tput setaf 3)\]"
    BOLD_YELLOW="\[$(tput bold ; tput setaf 3)\]"

    BLUE="\[$(tput setaf 4)\]"
    BOLD_BLUE="\[$(tput bold ; tput setaf 4)\]"

    PURPLE="\[$(tput setaf 5)\]"
    PINK="\[$(tput bold ; tput setaf 5)\]"

    CYAN="\[$(tput setaf 6)\]"
    BOLD_CYAN="\[$(tput bold ; tput setaf 6)\]"

    NO_COL="\[$(tput sgr0)\]"

fi

# Foreground colors
# can be set to white or black
FG=$WHITE
BOLD_FG=$BOLD_WHITE
if [[ $REVERSE ]] ; then
    FG=$BLACK
    BOLD_FG=$BOLD_GRAY
fi


# get cpu number
__cpunum_Linux()
{
    grep ^[Pp]rocessor /proc/cpuinfo | wc -l
}

__cpunum_FreeBSD()
{
    sysctl -n hw.ncpu
}

__cpunum_SunOS()
{
    kstat -m cpu_info | grep "module: cpu_info"  | wc -l
}

__CPUNUM=$(__cpunum_$OS)


# get current load

__load_Linux()
{
    load=$(awk '{print $1}' /proc/loadavg)
    echo -n "$load"
}

__load_FreeBSD()
{
    load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}')
    echo -n "$load"
}

__load_SunOS()
{
    load=$(LANG=C uptime | awk '{print $10}'| sed -e 's/,//')
    echo -n "$load"
}


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

# Yellow for root, light grey if the user is not the login one, else no color.
__user()
{
    # if user is not root
    if [[ "$EUID" -ne "0" ]] ; then<commit_msg>Revert "less indirections, directly call FreeBSD primitives"<commit_after>#!/bin/bash

# An intelligent and 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>>
# 2012 (c) <NAME> <<EMAIL>> #major rewrite and adaptation

# Below are function for having a flexible dynamic prompt for power user:
# - display the battery level (if necessary), with colormap (if any battery attached)
# - if necessary, prints a counter for jobs that are attached to the current
#   term (e.g. xterm &) or that are sleeping (e.g. Ctrl-z) or detached "screen" sessions
# - display the load average, colored with a colormap
# - displays the colored login@hostname
#     - when root displays in red
#     - when user displays in green
# - displays the current path in blue
# - when user, print the colored git branch name (if in a git repository):
#     red = some changes are not commited, and yellow in parenthesis any unpushed commits (if any)
#     yellow = some commits are not pushed. Number of commits in parenthesis
#     green = no changes or commits

# Check for recent enough version of bash.
[[ -z "$BASH_VERSION" || -z "$PS1" || -z "$TERM" ]] && return;

bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [[ $bmajor -lt 3 ]] || [[ $bmajor -eq 3 && $bminor -lt 2 ]]; then
    unset bash bmajor bminor
    return
fi
unset bash bmajor bminor


#################
# CONFIGURATION #
#################

# Maximal value under which the battery level is displayed
# Recommended value is 75
BATTERY_THRESHOLD=75

# Minimal value after which the load average is displayed
# Recommended value is 60
LOAD_THRESHOLD=60

# The maximum percentage of the screen width used to display the path
# Recommended value is 35
PATH_LENGTH=35

# How many directories to keep at the beginning of a shortened path
# Recommended value is 2
PATH_KEEP=2

# Do we use reverse colors (black on white) instead of normal theme (white on black)
# Defaults to unset (white on black)
# Otherwise use REVERSE="1" source liquidprompt.bash


###############
# OS specific #
###############

# OS detection, default to Linux
OS="Linux"
case $(uname) in
    "Linux"  ) OS="Linux"   ;;
    "FreeBSD") OS="FreeBSD" ;;
    "Darwin") OS="Darwin" ;;
    "DragonFly") OS="FreeBSD" ;;
    "SunOS") OS="SunOS" ;;
esac

# Colors declarations
if [[ "$OS" == "FreeBSD" ]] ; then

    BLACK="\[$(tput AF 0)\]"
    BOLD_GRAY="\[$(tput md ; tput AF 0)\]"
    WHITE="\[$(tput AF 7)\]"
    BOLD_WHITE="\[$(tput md ; tput AF 7)\]"

    RED="\[$(tput AF 1)\]"
    BOLD_RED="\[$(tput md ; tput AF 1)\]"
    WARN_RED="\[$(tput AF 0 ; tput setab 1)\]"
    CRIT_RED="\[$(tput md; tput AF 7 ; tput setab 1)\]"

    GREEN="\[$(tput AF 2)\]"
    BOLD_GREEN="\[$(tput md ; tput AF 2)\]"

    YELLOW="\[$(tput AF 3)\]"
    BOLD_YELLOW="\[$(tput md ; tput AF 3)\]"

    BLUE="\[$(tput AF 4)\]"
    BOLD_BLUE="\[$(tput md ; tput AF 4)\]"

    PURPLE="\[$(tput AF 5)\]"
    PINK="\[$(tput md ; tput AF 5)\]"

    CYAN="\[$(tput AF 6)\]"
    BOLD_CYAN="\[$(tput md ; tput AF 6)\]"

    NO_COL="\[$(tput me)\]"

else
    # default to Linux
    BLACK="\[$(tput setaf 0)\]"
    BOLD_GRAY="\[$(tput bold ; tput setaf 0)\]"
    WHITE="\[$(tput setaf 7)\]"
    BOLD_WHITE="\[$(tput bold ; tput setaf 7)\]"

    RED="\[$(tput setaf 1)\]"
    BOLD_RED="\[$(tput bold ; tput setaf 1)\]"
    WARN_RED="\[$(tput setaf 0 ; tput setab 1)\]"
    CRIT_RED="\[$(tput bold; tput setaf 7 ; tput setab 1)\]"

    GREEN="\[$(tput setaf 2)\]"
    BOLD_GREEN="\[$(tput bold ; tput setaf 2)\]"

    YELLOW="\[$(tput setaf 3)\]"
    BOLD_YELLOW="\[$(tput bold ; tput setaf 3)\]"

    BLUE="\[$(tput setaf 4)\]"
    BOLD_BLUE="\[$(tput bold ; tput setaf 4)\]"

    PURPLE="\[$(tput setaf 5)\]"
    PINK="\[$(tput bold ; tput setaf 5)\]"

    CYAN="\[$(tput setaf 6)\]"
    BOLD_CYAN="\[$(tput bold ; tput setaf 6)\]"

    NO_COL="\[$(tput sgr0)\]"

fi

# Foreground colors
# can be set to white or black
FG=$WHITE
BOLD_FG=$BOLD_WHITE
if [[ $REVERSE ]] ; then
    FG=$BLACK
    BOLD_FG=$BOLD_GRAY
fi


# get cpu number
__cpunum_Linux()
{
    grep ^[Pp]rocessor /proc/cpuinfo | wc -l
}

__cpunum_FreeBSD()
{
    sysctl -n hw.ncpu
}

__cpunum_Darwin()
{
	__cpunum_FreeBSD
}

__cpunum_SunOS()
{
    kstat -m cpu_info | grep "module: cpu_info"  | wc -l
}

__CPUNUM=$(__cpunum_$OS)


# get current load

__load_Linux()
{
    load=$(awk '{print $1}' /proc/loadavg)
    echo -n "$load"
}

__load_FreeBSD()
{
    load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}')
    echo -n "$load"
}

__load_Darwin()
{
	__load_FreeBSD
}

__load_SunOS()
{
    load=$(LANG=C uptime | awk '{print $10}'| sed -e 's/,//')
    echo -n "$load"
}


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

# Yellow for root, light grey if the user is not the login one, else no color.
__user()
{
    # if user is not root
    if [[ "$EUID" -ne "0" ]] ; then
{'fraction_non_alphanumeric': 0.16736822840409957, 'fraction_numerical': 0.013726207906295754, 'mean_word_length': 3.3010625737898467, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '575146', 'n_tokens_mistral': 4296, 'n_tokens_neox': 3844, 'n_words': 1359}