1
0
Fork 0
mirror of https://github.com/kastdeur/dotfiles.git synced 2024-11-01 01:53:32 +01:00
dotfiles/bash/bash.d/prompt_command

316 lines
7.2 KiB
Text
Raw Normal View History

# .bash_files
# /PS
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
# Make sure we have the VTE thing when we define our prompt command
#if [ $TILIX_ID ] || [ $VTE_VERSION ]; then
#source /etc/profile.d/vte.sh
#fi
# Old PS1
#if [ "$color_prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\]\u@\H\[\033[00m\] \[\033[0;33;40m\](\t)\[\033[00m\] \[\033[01;34m\]\[$(ls |wc -l)\]@\W\[\033[00m\]:\$ '
#else
# PS1='${debian_chroot:+($debian_chroot)}\u@\H (\t) \[$(ls | wc -l)\]@\W:\$ '
#fi
function color() {
if [ -n "$force_color_prompt" ] ; then
if [ -n "$color_prompt" ] ; then
return 0
else
return 1
fi
else
return 1
fi
}
function vte_thing() {
# if [ $TILIX_ID ] || [ $VTE_VERSION ]; then
# __vte_osc7
# fi
true
}
export PROMPT_COMMAND=__prompt_command
2020-11-23 21:34:04 +01:00
function __colored_hostname() {
local HOST="${1:-$HOSTNAME}"
local color="${2:-$(color)}"
local PS1
if color ; then
case "${HOSTNAME%%.*}" in
'esson') PS1+="${IGre}\h${RCol}";;
'tinsmith') PS1+="${Gre}\h${RCol}";;
'blacksmith') PS1+="${Blu}\h${RCol}";;
'locksmith') PS1+="${Pur}\h${RCol}";;
'bladesmith') PS1+="${Yel}\h${RCol}";;
'gunsmith') PS1+="${Red}\h${RCol}";;
'goldsmith') PS1+="${IYel}\h${RCol}";;
'lilo'*) PS1+="${BWhi}\h${RCol}";;
'hg'*) PS1+="${BRed}\h${RCol}";;
'gouda'|'bunnik'|'houten'|'zandberg'|'bussum'|'baarn') PS1+="${Gre}${On_Whi}HEF:${Blu}${On_Whi}\h${RCol}";;
'messier'*) PS1+="${Blu}Astro:${Red}\h${RCol}";;
*) PS1+="${Blu}${On_Whi}\h${RCol}";;
esac
else
PS1+="\h"
fi
echo "$PS1"
}
function __colored_username() {
local USER="${1:-$USER}"
local color="${2:-$(color)}"
local PS1
if color ; then
case "${USER}" in
'root') PS1+="${Red}\u${RCol}";;
'kastdeur'|'ericteunis') PS1+="${Gre}\u${RCol}";;
*) PS1+="${Blu}\u${RCol}";;
esac
else
PS1+="\u"
fi
echo "$PS1"
}
function __prompt_command() {
local RETVAL=$?
if [ ! $RETVAL -ne 0 ]; then
RETVAL=0
fi
PS1="${debian_chroot:+($debian_chroot)}"
#Return Value
if [ -n $RETVAL ]; then
PS1+="$(retval ${RETVAL})"
fi
2016-10-11 23:18:39 +02:00
### Add amount of jobs ###
if true; then
PS1+="$(jobscount)"
fi
### Add Git Status ###
if [[ $(command -v git) ]]; then
PS1+="$(git_status) "
fi
2016-10-11 23:18:39 +02:00
#user@hostname
if color ; then
2020-11-23 21:34:04 +01:00
PS1+="$(__colored_username)"
else
PS1+="\u"
fi
PS1+="@"
if color ; then
2020-11-23 21:34:04 +01:00
PS1+="$(__colored_hostname)"
else
PS1+="\h"
fi
#time w/ seconds
if color ; then
PS1+="${Yel}${On_Bla}"
fi
PS1+="(\t)"
if color ; then
PS1+="${RCol}"
fi
PS1+=" "
#dir count, pwd
if color ; then
PS1+="${BBlu}"
fi
PS1+="\[$(ls |wc -l)\]@\W"
if color ; then
PS1+="${RCol}"
fi
PS1+=" "
## Short prompt
if [ -n "${SHORTPS1}" ]; then
PS1+="\r\n"
fi
#UID
PS1+=':\$ '
# Something for VTE
PS1+=$(vte_thing)
}
function retval() {
### Determine Return Value
local PS1
if [ -z "$color_prompt" ]; then
PS1="[$1] "
else
if [ ! $1 == 0 ]; then
2017-01-27 16:22:49 +01:00
PS1="${Red}x${RCol}"
else
PS1="${Gre}+${RCol}"
fi
fi
2016-10-11 23:18:39 +02:00
echo "${PS1}"
}
### count jobs
function jobscount() {
local stopped=$(jobs -sp | wc -l)
local running=$(jobs -rp | wc -l)
((running+stopped)) && echo -n "${IYel}${running}${RCol}|${Yel}${stopped}${RCol}"
}
function git_status() {
### Determine Git Status
local PS1
local GStat="$(git status --porcelain -b 2>/dev/null | tr '\n' ':')"
if [ "$GStat" ]; then
### Fetch Time Check ### {{{
local LAST=$(stat -c %Y $(git rev-parse --git-dir 2>/dev/null)/FETCH_HEAD 2>/dev/null)
if [ "${LAST}" ]; then
local TIME=$(echo $(date +"%s") - ${LAST} | bc)
## Check if more than 60 minutes since last
if [ "${TIME}" -gt "3600" ]; then
git fetch 2>/dev/null
2016-10-11 23:18:39 +02:00
PS1+='+ '
## Refresh var
local GStat="$(git status --porcelain -b 2>/dev/null | tr '\n' ':')"
fi
fi
### End Fetch Check ### }}}
### Test For Changes ### {{{
## Change this to test for 'ahead' or 'behind'!
local GChanges="$(echo ${GStat} | tr ':' '\n' | grep -v "^$" | grep -v "^\#\#" | wc -l | tr -d ' ')"
if [ "$GChanges" == "0" ]; then
local GitCol=$Gre
else
local GitCol=$Red
fi
### End Test Changes ### }}}
### Find Branch ### {{{
local GBra="$(echo ${GStat} | tr ':' '\n' | grep "^##" | cut -c4- | grep -o "^[a-zA-Z]\{1,\}[^\.]")"
if [ "$GBra" ]; then
if [ "$GBra" == "master" ]; then
local GBra="M" # Because why waste space
fi
else
local GBra="ERROR" # It could happen supposedly?
fi
### End Branch ### }}}
2016-10-11 23:18:39 +02:00
PS1+="${GitCol}[$GBra]${RCol}" # Add result to prompt
### Find Commit Status ### {{{
## Test Modified and Untracked for "0"
# # local GDel="$(echo ${GStat} | tr ':' '\n' | grep -c "^[ MARC]D")"
local GAhe="$(echo ${GStat} | tr ':' '\n' | grep "^##" | grep -o "ahead [0-9]\{1,\}" | grep -o "[0-9]\{1,\}")"
if [ "$GAhe" ]; then
PS1+="${Gre}↑${RCol}${GAhe}" # Ahead
fi
## Needs a `git fetch`
local GBeh="$(echo ${GStat} | tr ':' '\n' | grep "^##" | grep -o "behind [0-9]\{1,\}" | grep -o "[0-9]\{1,\}")"
if [ "$GBeh" ]; then
PS1+="${Red}↓${RCol}${GBeh}" # Behind
fi
local GMod="$(echo ${GStat} | tr ':' '\n' | grep -c "^[ MARC]M")"
if [ "$GMod" -gt "0" ]; then
PS1+="${Pur}≠${RCol}${GMod}" # Modified
fi
local GUnt="$(echo ${GStat} | tr ':' '\n' | grep -c "^\?")"
if [ "$GUnt" -gt "0" ]; then
PS1+="${Yel}?${RCol}${GUnt}" # Untracked
fi
### End Commit Status ### }}}
echo "${PS1}"
fi
}
# Git
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]; then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}