Freshen up that prompt!
Emulate a Powerline setup. Includes: * Change custom foreground colours to background colours * Add 8 different separators, selected randomly based on terminal ID * Prettier and more sensible Git repo sigils * Cleaned and condensed code
This commit is contained in:
parent
25a0523524
commit
a8920ad9b7
246
bash/prompt.sh
246
bash/prompt.sh
|
@ -1,14 +1,45 @@
|
||||||
# vim: ft=sh
|
# vim: ft=sh
|
||||||
|
|
||||||
# Explain prompt
|
# Explain prompt
|
||||||
ARG=$1
|
if [[ $1 && $1 != 'h' && $1 != '-h' && $1 != 'help' && $1 != '--help' && $1 != '?' ]]; then
|
||||||
if [[ $ARG == 'h' || $ARG == '-h' || $ARG == 'help' || $ARG == '--help' || $ARG == '?' ]]; then
|
echo "Invalid argument: $1"
|
||||||
ARG="-h"
|
exit
|
||||||
elif [[ $ARG != '' ]]; then
|
|
||||||
echo "Invalid argument: $ARG"
|
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Declare the S name (as used by `setterm`) and B for POSIX background colour
|
||||||
|
# Reference them later like: ${COLOURS["BLACK","S"]} or ${COLOURS["CYAN","B"]}
|
||||||
|
declare -A COLOURS='(
|
||||||
|
[0,S]="default"
|
||||||
|
[0,B]="\[\e[0m\]"
|
||||||
|
[1,S]="red"
|
||||||
|
[1,B]="\[\e[1;30;41m\]"
|
||||||
|
[2,S]="green"
|
||||||
|
[2,B]="\[\e[1;30;42m\]"
|
||||||
|
[3,S]="yellow"
|
||||||
|
[3,B]="\[\e[1;30;43m\]"
|
||||||
|
[4,S]="blue"
|
||||||
|
[4,B]="\[\e[1;30;44m\]"
|
||||||
|
[5,S]="magenta"
|
||||||
|
[5,B]="\[\e[1;30;45m\]"
|
||||||
|
[6,S]="cyan"
|
||||||
|
[6,B]="\[\e[1;30;46m\]"
|
||||||
|
[7,S]="white"
|
||||||
|
[7,B]="\[\e[1;30;47m\]"
|
||||||
|
)'
|
||||||
|
|
||||||
|
# Assign colour to each module
|
||||||
|
P="7" # prompt default, white
|
||||||
|
T="3" # time, yellow
|
||||||
|
E="7" # venv, white
|
||||||
|
C="0" # chroot, default
|
||||||
|
U="2" # user
|
||||||
|
R="1" # root user
|
||||||
|
H="1" # host (overwritten for distrobox and ssh)
|
||||||
|
B="4" # distrobox
|
||||||
|
S="5" # ssh
|
||||||
|
D="6" # dir
|
||||||
|
G="7" # git
|
||||||
|
|
||||||
# If directory was just changed to the root of a git repository, print onefetch
|
# If directory was just changed to the root of a git repository, print onefetch
|
||||||
if [ -n "$DIRCHANGED" ]; then
|
if [ -n "$DIRCHANGED" ]; then
|
||||||
source ${HOME}/.dotfiles/bash/plenv-path.sh
|
source ${HOME}/.dotfiles/bash/plenv-path.sh
|
||||||
|
@ -22,166 +53,127 @@ if [ -n "$DIRCHANGED" ]; then
|
||||||
unset DIRCHANGED
|
unset DIRCHANGED
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Default colours
|
# Choose a pseudo random separator character
|
||||||
# 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
|
# Local connections can just use ALACRITTY_WINDOW_ID
|
||||||
# Reference them later like: ${COLOURS["5","SET"]} or ${COLOURS["2","PS"]}
|
if [[ -n $ALACRITTY_WINDOW_ID ]]; then
|
||||||
declare -A COLOURS='(
|
# The last 4 bits are always 0, so drop them
|
||||||
[0,SET]="default"
|
SEP=$(($ALACRITTY_WINDOW_ID >> 4))
|
||||||
[0,PS]="\[\033[0;0m\]"
|
(( SEP &= 7 ))
|
||||||
[1,SET]="red"
|
# SSH connections can use PID stored in SSH_CONNECTION
|
||||||
[1,PS]="\[\033[0;31m\]"
|
else
|
||||||
[2,SET]="green"
|
# Seems to always be even, so drop 1 bit
|
||||||
[2,PS]="\[\033[0;32m\]"
|
SEP=$(( $(echo $SSH_CONNECTION | cut -d ' ' -f 2) >> 1 ))
|
||||||
[3,SET]="yellow"
|
fi
|
||||||
[3,PS]="\[\033[0;33m\]"
|
# Just use the new last 4 bits to get a pseudo random number from 0-7
|
||||||
[4,SET]="blue"
|
(( SEP &= 7 ))
|
||||||
[4,PS]="\[\033[0;34m\]"
|
|
||||||
[5,SET]="magenta"
|
# Select the separator character (some wide characters look better with a trailing space)
|
||||||
[5,PS]="\[\033[0;35m\]"
|
# See https://github.com/ryanoasis/powerline-extra-symbols
|
||||||
[6,SET]="cyan"
|
case "$SEP" in
|
||||||
[6,PS]="\[\033[0;36m\]"
|
0) SEP="" ;;
|
||||||
[7,SET]="white"
|
1) SEP=" " ;;
|
||||||
[7,PS]="\[\033[0;37m\]"
|
2) SEP=" " ;;
|
||||||
)'
|
3) SEP=" " ;;
|
||||||
|
4) SEP=" " ;;
|
||||||
|
5) SEP="" ;;
|
||||||
|
6) SEP=" " ;;
|
||||||
|
7) SEP="" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
# Get current git branch
|
# Get current git branch
|
||||||
function parse_git_branch() {
|
function parse_git_branch() {
|
||||||
BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
|
BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
|
||||||
if [ ! "${BRANCH}" == "" ]
|
[ ! "${BRANCH}" == "" ] && echo "\[\e[1;31;4${G}m\]$(parse_git_dirty)${COLOURS["$G","B"]}${BRANCH}" || echo ""
|
||||||
then
|
|
||||||
STAT=$(parse_git_dirty)
|
|
||||||
echo "${COLOURS[\"$GC\",\"PS\"]}(${BRANCH}${STAT})${COLOURS[\"0\",\"PS\"]}"
|
|
||||||
else
|
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get git status
|
# Get git status
|
||||||
function parse_git_dirty {
|
function parse_git_dirty {
|
||||||
status=$(git status 2>&1 | tee)
|
status=$(git status 2>/dev/null)
|
||||||
dirty=$(echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?")
|
modified=$(echo -n "${status}" | grep "modified:" &> /dev/null; echo "$?")
|
||||||
untracked=$(echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?")
|
untracked=$(echo -n "${status}" | grep "Untracked files" &> /dev/null; echo "$?")
|
||||||
ahead=$(echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?")
|
ahead=$(echo -n "${status}" | grep "Your branch is ahead of" &> /dev/null; echo "$?")
|
||||||
newfile=$(echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?")
|
newfile=$(echo -n "${status}" | grep "new file:" &> /dev/null; echo "$?")
|
||||||
renamed=$(echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?")
|
renamed=$(echo -n "${status}" | grep "renamed:" &> /dev/null; echo "$?")
|
||||||
deleted=$(echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?")
|
deleted=$(echo -n "${status}" | grep "deleted:" &> /dev/null; echo "$?")
|
||||||
bits=''
|
bits=''
|
||||||
if [ "${renamed}" == "0" ]; then
|
[ "${modified}" == "0" ] && bits="${bits} "
|
||||||
bits=">${bits}"
|
[ "${newfile}" == "0" ] && bits="${bits} "
|
||||||
fi
|
[ "${deleted}" == "0" ] && bits="${bits} "
|
||||||
if [ "${ahead}" == "0" ]; then
|
[ "${renamed}" == "0" ] && bits="${bits} "
|
||||||
bits="*${bits}"
|
[ "${untracked}" == "0" ] && bits="${bits}🯄 "
|
||||||
fi
|
[ "${ahead}" == "0" ] && bits="${bits} "
|
||||||
if [ "${newfile}" == "0" ]; then
|
[ "${bits}" != "" ] && echo "${bits}" || echo ""
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Comment - allow for command history to be copied directly to bash script
|
# Opening # so that PS1 gets interpretted as a comment if pasted into a terminal or script
|
||||||
PS1="${COLOURS["$PC","PS"]}# "
|
PS1="${COLOURS["$P","B"]}#"
|
||||||
|
|
||||||
|
# Track the last Background colour so that it can be used for the next separator
|
||||||
|
LASTB=$P
|
||||||
|
|
||||||
|
# Sigil to indicate host, distrobox or ssh. Store HC so that host matches connection type
|
||||||
if [ -f "/run/.containerenv" ]; then
|
if [ -f "/run/.containerenv" ]; then
|
||||||
# Sigil to indicate host, distrobox or ssh
|
H=$B
|
||||||
PS1="${PS1}${COLOURS["0","PS"]}⧉ "
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "distrobox" && setterm --foreground default
|
||||||
|
PS1="${PS1}\[\e[0;3${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}📦"
|
||||||
elif [[ -z $SSH_CLIENT ]]; then
|
elif [[ -z $SSH_CLIENT ]]; then
|
||||||
PS1="${PS1}${COLOURS["1","PS"]}⌂ "
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "host session" && setterm --foreground default
|
||||||
|
PS1="${PS1}\[\e[0;3${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}🏠"
|
||||||
else
|
else
|
||||||
PS1="${PS1}${COLOURS["1","PS"]}🖧 "
|
H=$S
|
||||||
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "ssh connection" && setterm --foreground default
|
||||||
|
PS1="${PS1}\[\e[0;3${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}🖧 "
|
||||||
fi
|
fi
|
||||||
|
LASTB=$H
|
||||||
|
|
||||||
# Time
|
# Time
|
||||||
PS1="${PS1}${COLOURS["$TC","PS"]}\\t "
|
[[ $1 ]] && setterm --foreground ${COLOURS["$T","S"]} && echo "time" && setterm --foreground default
|
||||||
if [[ $ARG == '-h' ]]; then
|
PS1="${PS1}\[\e[0;3${LASTB};4${T}m\]"${SEP}
|
||||||
setterm --foreground ${COLOURS["$TC","SET"]}; echo "time"; setterm --foreground default
|
PS1="${PS1}${COLOURS["$T","B"]}\\t"
|
||||||
fi
|
LASTB=$T
|
||||||
|
|
||||||
# Python venv
|
# Python venv
|
||||||
if [ "$VIRTUAL_ENV" ]; then
|
if [ "$VIRTUAL_ENV" ]; then
|
||||||
VENV="${VIRTUAL_ENV##*/}"
|
VENV="${VIRTUAL_ENV##*/}"
|
||||||
VENV=$(echo "$VENV" | sed -r 's/.*/[\0]/')
|
VENV=$(echo "$VENV" | sed -r 's/.*/[\0]/')
|
||||||
PS1="${PS1}${COLOURS["$EC","PS"]}${VENV} "
|
[[ $1 ]] && setterm --foreground ${COLOURS["$E","S"]} && echo "venv" && setterm --foreground default
|
||||||
if [[ $ARG == '-h' ]]; then
|
PS1=${PS1}\[\e[0;3${LASTB};4${E}m\]${SEP}${COLOURS["$E","B"]}${VENV}
|
||||||
setterm --foreground ${COLOURS["$EC","SET"]}; echo "venv"; setterm --foreground default
|
LASTB=$E
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Debian Chroot
|
# Debian Chroot
|
||||||
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
||||||
PS1="${PS1}${COLOURS["$CC","PS"]}<$(cat /etc/debian_chroot)>"
|
[[ $1 ]] && setterm --foreground ${COLOURS["$C","S"]} && echo "debian chroot" && setterm --foreground default
|
||||||
if [[ $ARG == '-h' ]]; then
|
PS1="${PS1}\[\e[0;3${LASTB};4${C}m\]${SEP}"${COLOURS["$C","B"]}"<$(cat /etc/debian_chroot)>"
|
||||||
setterm --foreground ${COLOURS["$CC","SET"]}; echo "debian chroot"; setterm --foreground default
|
LASTB=$C
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# User
|
# User
|
||||||
if [[ $(whoami) == 'root' ]]; then
|
[[ $(whoami) == 'root' ]] && U=$R
|
||||||
UC=$RC
|
[[ $1 ]] && setterm --foreground ${COLOURS["$U","S"]} && echo "user" && setterm --foreground default
|
||||||
fi
|
PS1="${PS1}\[\e[0;3${LASTB};4${U}m\]"${SEP}"${COLOURS["$U","B"]}\\u\[\e[0;30;4${U}m\]@"
|
||||||
PS1="${PS1}${COLOURS["$UC","PS"]}\\u"
|
LASTB=$U
|
||||||
if [[ $ARG == '-h' ]]; then
|
|
||||||
setterm --foreground ${COLOURS["$UC","SET"]}; echo "user"; setterm --foreground default
|
|
||||||
fi
|
|
||||||
|
|
||||||
# @
|
|
||||||
PS1="${PS1}${COLOURS["$PC","PS"]}@"
|
|
||||||
|
|
||||||
# Host
|
# Host
|
||||||
if [[ $SSH_TTY != '' ]]; then
|
[[ $1 ]] && setterm --foreground ${COLOURS["$H","S"]} && echo "hostname" $([[ -z $SSH_TTY ]] || echo " (ssh)") && setterm --foreground default
|
||||||
HC="${SC}" # Host colour
|
PS1="${PS1}\[\e[0;3${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}\\h\[\e[0;30;4${H}m\]:"
|
||||||
if [[ $ARG == '-h' ]]; then
|
LASTB=$H
|
||||||
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host"; setterm --foreground default
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [[ $ARG == '-h' ]]; then
|
|
||||||
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host (ssh)"; setterm --foreground default
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
PS1="${PS1}${COLOURS["$HC","PS"]}\\h"
|
|
||||||
|
|
||||||
# :
|
|
||||||
PS1="${PS1}${COLOURS["$PC","PS"]}:"
|
|
||||||
|
|
||||||
# Dir
|
# Dir
|
||||||
PS1="${PS1}${COLOURS["$DC","PS"]}\\w "
|
[[ $1 ]] && setterm --foreground ${COLOURS["$D","S"]} && echo "directory" && setterm --foreground default
|
||||||
if [[ $ARG == '-h' ]]; then
|
PS1="${PS1}\[\e[0;3${LASTB};4${D}m\]"${SEP}"${COLOURS["$D","B"]}\\w"
|
||||||
setterm --foreground ${COLOURS["$DC","SET"]}; echo "directory"; setterm --foreground default
|
LASTB=$D
|
||||||
fi
|
|
||||||
|
|
||||||
# Git
|
# Git
|
||||||
GIT=$(parse_git_branch)
|
GIT=$(parse_git_branch)
|
||||||
if [[ $GIT != '' ]]; then
|
if [[ $GIT != '' ]]; then
|
||||||
PS1="${PS1}${COLOURS["$GC","PS"]}${GIT}"
|
[[ $1 ]] && setterm --foreground ${COLOURS["$G","S"]} && echo "git branch status" && setterm --foreground default
|
||||||
if [[ $ARG == '-h' ]]; then
|
PS1="$PS1\[\e[0;3${LASTB};4${G}m\]"${SEP}${GIT}
|
||||||
setterm --foreground ${COLOURS["$GC","SET"]}; echo "git branch status"; setterm --foreground default
|
LASTB=$G
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# \n - start input on new line, again to support copy-paste into bash script
|
# \n - start input on new line, again to support copy-paste into bash script
|
||||||
PS1="${PS1}${COLOURS["$PC","PS"]}\\n"
|
PS1="${PS1}\[\e[0;3${LASTB};1m\]"${SEP}"\[\e[0m\]\n"
|
||||||
|
|
||||||
export PS1
|
export PS1
|
||||||
|
|
Loading…
Reference in New Issue