2022-04-16 19:54:07 +00:00
|
|
|
# vim: ft=sh
|
|
|
|
|
|
|
|
# Explain prompt
|
|
|
|
ARG=$1
|
|
|
|
if [[ $ARG == 'h' || $ARG == '-h' || $ARG == 'help' || $ARG == '--help' || $ARG == '?' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
ARG="-h"
|
2022-04-16 19:54:07 +00:00
|
|
|
elif [[ $ARG != '' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
echo "Invalid argument: $ARG"
|
|
|
|
exit 0
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
2022-12-28 15:38:43 +00:00
|
|
|
# If directory was just changed to the root of a git repository, print onefetch
|
|
|
|
if [ ! -z $DIRCHANGED ]; then
|
2023-01-07 17:03:31 +00:00
|
|
|
if [ -d $PWD/.git ]; then
|
|
|
|
$HOME/bin/onefetch
|
|
|
|
fi
|
|
|
|
unset DIRCHANGED
|
2022-12-28 15:38:43 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-16 19:54:07 +00:00
|
|
|
# Default colours
|
|
|
|
# 0 default, 1 red 2 green 3 yellow 4 blue 5 magenta 6 cyan 7 white (grey)
|
|
|
|
PC="0" # prompt default
|
|
|
|
TC="3" # time
|
|
|
|
EC="7" # venv
|
|
|
|
CC="1" # chroot
|
|
|
|
UC="2" # user
|
|
|
|
RC="1" # root user
|
|
|
|
HC="4" # host
|
|
|
|
SC="1" # ssh host
|
|
|
|
DC="6" # dir
|
|
|
|
GC="5" # git
|
|
|
|
|
|
|
|
# Declare the SET name (as used by `setterm`) and PS string for colour options
|
|
|
|
# Reference them later like: ${COLOURS["5","SET"]} or ${COLOURS["2","PS"]}
|
|
|
|
declare -A COLOURS='(
|
2022-12-29 22:50:45 +00:00
|
|
|
[0,SET]="default"
|
|
|
|
[0,PS]="\[\033[0;0m\]"
|
|
|
|
[1,SET]="red"
|
|
|
|
[1,PS]="\[\033[0;31m\]"
|
|
|
|
[2,SET]="green"
|
|
|
|
[2,PS]="\[\033[0;32m\]"
|
|
|
|
[3,SET]="yellow"
|
|
|
|
[3,PS]="\[\033[0;33m\]"
|
|
|
|
[4,SET]="blue"
|
|
|
|
[4,PS]="\[\033[0;34m\]"
|
|
|
|
[5,SET]="magenta"
|
|
|
|
[5,PS]="\[\033[0;35m\]"
|
|
|
|
[6,SET]="cyan"
|
|
|
|
[6,PS]="\[\033[0;36m\]"
|
|
|
|
[7,SET]="white"
|
|
|
|
[7,PS]="\[\033[0;37m\]"
|
2022-04-16 19:54:07 +00:00
|
|
|
)'
|
|
|
|
|
|
|
|
# Get current git branch
|
|
|
|
function parse_git_branch() {
|
2022-12-29 22:50:45 +00:00
|
|
|
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
|
|
|
|
if [ ! "${BRANCH}" == "" ]
|
|
|
|
then
|
|
|
|
STAT=`parse_git_dirty`
|
|
|
|
echo "${COLOURS[\"$GC\",\"PS\"]}(${BRANCH}${STAT})${COLOURS[\"0\",\"PS\"]}"
|
|
|
|
else
|
|
|
|
echo ""
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get git status
|
|
|
|
function parse_git_dirty {
|
2022-12-29 22:50:45 +00:00
|
|
|
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
|
2022-04-16 19:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Comment - allow for command history to be copied directly to bash script
|
|
|
|
PS1="${COLOURS["$PC","PS"]}# "
|
2022-11-05 03:05:08 +00:00
|
|
|
if [ -f "/run/.containerenv" ]; then
|
|
|
|
# Sigil to indicate host, distrobox or ssh
|
2022-11-08 02:45:04 +00:00
|
|
|
PS1="${PS1}${COLOURS["0","PS"]}⧉ "
|
2022-11-05 03:05:08 +00:00
|
|
|
elif [[ -z $SSH_CLIENT ]]; then
|
2022-11-08 02:45:04 +00:00
|
|
|
PS1="${PS1}${COLOURS["1","PS"]}⌂ "
|
2022-11-05 03:05:08 +00:00
|
|
|
else
|
2022-11-08 02:45:04 +00:00
|
|
|
PS1="${PS1}${COLOURS["1","PS"]}🖧 "
|
2022-11-05 03:05:08 +00:00
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Time
|
|
|
|
PS1="${PS1}${COLOURS["$TC","PS"]}\\t "
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
setterm --foreground ${COLOURS["$TC","SET"]}; echo "time"; setterm --foreground default
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Python venv
|
|
|
|
if [ $VIRTUAL_ENV ]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
VENV="${VIRTUAL_ENV##*/}"
|
|
|
|
VENV=`echo $VENV | sed -r 's/.*/[\0]/'`
|
|
|
|
PS1="${PS1}${COLOURS["$EC","PS"]}${VENV} "
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
|
|
|
setterm --foreground ${COLOURS["$EC","SET"]}; echo "venv"; setterm --foreground default
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Debian Chroot
|
|
|
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
PS1="${PS1}${COLOURS["$CC","PS"]}<$(cat /etc/debian_chroot)>"
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
|
|
|
setterm --foreground ${COLOURS["$CC","SET"]}; echo "debian chroot"; setterm --foreground default
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# User
|
|
|
|
if [[ `whoami` == 'root' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
UC=$RC
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
PS1="${PS1}${COLOURS["$UC","PS"]}\\u"
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
setterm --foreground ${COLOURS["$UC","SET"]}; echo "user"; setterm --foreground default
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# @
|
|
|
|
PS1="${PS1}${COLOURS["$PC","PS"]}@"
|
|
|
|
|
|
|
|
# Host
|
|
|
|
if [[ $SSH_TTY != '' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
HC="${SC}" # Host colour
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
|
|
|
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host"; setterm --foreground default
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
else
|
2022-12-29 22:50:45 +00:00
|
|
|
if [[ $ARG == '-h' ]]; then
|
|
|
|
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host (ssh)"; setterm --foreground default
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
PS1="${PS1}${COLOURS["$HC","PS"]}\\h"
|
|
|
|
|
|
|
|
# :
|
|
|
|
PS1="${PS1}${COLOURS["$PC","PS"]}:"
|
|
|
|
|
|
|
|
# Dir
|
|
|
|
PS1="${PS1}${COLOURS["$DC","PS"]}\\w "
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
setterm --foreground ${COLOURS["$DC","SET"]}; echo "directory"; setterm --foreground default
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Git
|
|
|
|
GIT=$(parse_git_branch)
|
|
|
|
if [[ $GIT != '' ]]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
PS1="${PS1}${COLOURS["$GC","PS"]}${GIT}"
|
|
|
|
if [[ $ARG == '-h' ]]; then
|
|
|
|
setterm --foreground ${COLOURS["$GC","SET"]}; echo "git branch status"; setterm --foreground default
|
|
|
|
fi
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# \n - start input on new line, again to support copy-paste into bash script
|
|
|
|
PS1="${PS1}${COLOURS["$PC","PS"]}\\n"
|
|
|
|
|
|
|
|
export PS1=$PS1
|