2022-04-16 19:54:07 +00:00
|
|
|
# vim: ft=sh
|
|
|
|
|
|
|
|
# Explain prompt
|
2024-08-17 04:26:07 +00:00
|
|
|
if [[ $1 && $1 != 'h' && $1 != '-h' && $1 != 'help' && $1 != '--help' && $1 != '?' ]]; then
|
|
|
|
echo "Invalid argument: $1"
|
|
|
|
exit
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
2024-09-24 22:05:32 +00:00
|
|
|
# Dark mode terminals should have black text on color; light mode has white
|
|
|
|
TEXT=0
|
|
|
|
BG=3
|
|
|
|
if [[ -n $TERM_LIGHT ]]; then
|
|
|
|
TEXT=7
|
|
|
|
BG=4
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
|
|
|
GS="1" # git sigils
|
|
|
|
|
2024-08-17 04:26:07 +00:00
|
|
|
# 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"
|
2024-09-24 22:05:32 +00:00
|
|
|
[1,B]="\[\e[1;3${TEXT};41m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[2,S]="green"
|
2024-09-24 22:05:32 +00:00
|
|
|
[2,B]="\[\e[1;3${TEXT};42m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[3,S]="yellow"
|
2024-09-24 22:05:32 +00:00
|
|
|
[3,B]="\[\e[1;3${TEXT};43m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[4,S]="blue"
|
2024-09-24 22:05:32 +00:00
|
|
|
[4,B]="\[\e[1;3${TEXT};44m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[5,S]="magenta"
|
2024-09-24 22:05:32 +00:00
|
|
|
[5,B]="\[\e[1;3${TEXT};45m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[6,S]="cyan"
|
2024-09-24 22:05:32 +00:00
|
|
|
[6,B]="\[\e[1;3${TEXT};46m\]"
|
2024-08-17 04:26:07 +00:00
|
|
|
[7,S]="white"
|
|
|
|
[7,B]="\[\e[1;30;47m\]"
|
|
|
|
)'
|
|
|
|
|
2024-09-24 22:05:32 +00:00
|
|
|
if [[ $TEXT != 0 ]]; then
|
|
|
|
COLOURS[7,S]="black"
|
|
|
|
COLOURS[7,B]="\[\e[1;37;40m\]"
|
|
|
|
G=0
|
|
|
|
GS=7
|
|
|
|
fi
|
2024-08-17 04:26:07 +00:00
|
|
|
|
2022-12-28 15:38:43 +00:00
|
|
|
# If directory was just changed to the root of a git repository, print onefetch
|
2023-02-24 01:24:59 +00:00
|
|
|
if [ -n "$DIRCHANGED" ]; then
|
2024-02-01 04:53:28 +00:00
|
|
|
source ${HOME}/.dotfiles/bash/plenv-path.sh
|
2023-02-24 01:24:59 +00:00
|
|
|
if [ -d "$PWD/.git" ]; then
|
2023-10-24 17:17:35 +00:00
|
|
|
if [[ "$(which onefetch)" == "" ]]; then
|
|
|
|
echo "This is a git directory, but you don't have 'onefetch' in your PATH"
|
|
|
|
else
|
|
|
|
onefetch
|
|
|
|
fi
|
2023-01-07 17:03:31 +00:00
|
|
|
fi
|
|
|
|
unset DIRCHANGED
|
2022-12-28 15:38:43 +00:00
|
|
|
fi
|
|
|
|
|
2024-08-17 04:26:07 +00:00
|
|
|
# Choose a pseudo random separator character
|
|
|
|
|
|
|
|
# Local connections can just use ALACRITTY_WINDOW_ID
|
|
|
|
if [[ -n $ALACRITTY_WINDOW_ID ]]; then
|
|
|
|
# The last 4 bits are always 0, so drop them
|
|
|
|
SEP=$(($ALACRITTY_WINDOW_ID >> 4))
|
|
|
|
# SSH connections can use PID stored in SSH_CONNECTION
|
|
|
|
else
|
|
|
|
# Seems to always be even, so drop 1 bit
|
|
|
|
SEP=$(( $(echo $SSH_CONNECTION | cut -d ' ' -f 2) >> 1 ))
|
|
|
|
fi
|
|
|
|
# Just use the new last 4 bits to get a pseudo random number from 0-7
|
|
|
|
(( SEP &= 7 ))
|
|
|
|
|
|
|
|
# Select the separator character (some wide characters look better with a trailing space)
|
|
|
|
# See https://github.com/ryanoasis/powerline-extra-symbols
|
|
|
|
case "$SEP" in
|
|
|
|
0) SEP="" ;;
|
|
|
|
1) SEP=" " ;;
|
|
|
|
2) SEP=" " ;;
|
|
|
|
3) SEP=" " ;;
|
|
|
|
4) SEP=" " ;;
|
|
|
|
5) SEP="" ;;
|
|
|
|
6) SEP=" " ;;
|
|
|
|
7) SEP="" ;;
|
|
|
|
esac
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Get current git branch
|
|
|
|
function parse_git_branch() {
|
2024-08-17 04:26:07 +00:00
|
|
|
BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
|
2024-09-24 22:05:32 +00:00
|
|
|
[ ! "${BRANCH}" == "" ] && echo "\[\e[1;3${GS};4${G}m\]$(parse_git_dirty)\[\e[1;3${TEXT};4${G}m\]${BRANCH}" || echo ""
|
2022-04-16 19:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get git status
|
|
|
|
function parse_git_dirty {
|
2024-08-17 04:26:07 +00:00
|
|
|
status=$(git status 2>/dev/null)
|
|
|
|
modified=$(echo -n "${status}" | grep "modified:" &> /dev/null; echo "$?")
|
|
|
|
untracked=$(echo -n "${status}" | grep "Untracked files" &> /dev/null; echo "$?")
|
|
|
|
ahead=$(echo -n "${status}" | grep "Your branch is ahead of" &> /dev/null; echo "$?")
|
|
|
|
newfile=$(echo -n "${status}" | grep "new file:" &> /dev/null; echo "$?")
|
|
|
|
renamed=$(echo -n "${status}" | grep "renamed:" &> /dev/null; echo "$?")
|
|
|
|
deleted=$(echo -n "${status}" | grep "deleted:" &> /dev/null; echo "$?")
|
2022-12-29 22:50:45 +00:00
|
|
|
bits=''
|
2024-08-17 04:26:07 +00:00
|
|
|
[ "${modified}" == "0" ] && bits="${bits} "
|
|
|
|
[ "${newfile}" == "0" ] && bits="${bits} "
|
|
|
|
[ "${deleted}" == "0" ] && bits="${bits} "
|
|
|
|
[ "${renamed}" == "0" ] && bits="${bits} "
|
|
|
|
[ "${untracked}" == "0" ] && bits="${bits}🯄 "
|
|
|
|
[ "${ahead}" == "0" ] && bits="${bits} "
|
|
|
|
[ "${bits}" != "" ] && echo "${bits}" || echo ""
|
2022-04-16 19:54:07 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 04:26:07 +00:00
|
|
|
# Opening # so that PS1 gets interpretted as a comment if pasted into a terminal or script
|
|
|
|
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
|
2022-11-05 03:05:08 +00:00
|
|
|
if [ -f "/run/.containerenv" ]; then
|
2024-08-17 04:26:07 +00:00
|
|
|
H=$B
|
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "distrobox" && setterm --foreground default
|
2024-09-24 22:05:32 +00:00
|
|
|
PS1="${PS1}\[\e[0;${BG}${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}📦"
|
2022-11-05 03:05:08 +00:00
|
|
|
elif [[ -z $SSH_CLIENT ]]; then
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "host session" && setterm --foreground default
|
2024-09-24 22:05:32 +00:00
|
|
|
PS1="${PS1}\[\e[0;${BG}${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}🏠"
|
2022-11-05 03:05:08 +00:00
|
|
|
else
|
2024-08-17 04:26:07 +00:00
|
|
|
H=$S
|
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS[$H,"S"]} && echo "ssh connection" && setterm --foreground default
|
2024-09-24 22:05:32 +00:00
|
|
|
PS1="${PS1}\[\e[0;${BG}${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}🖧 "
|
2022-11-05 03:05:08 +00:00
|
|
|
fi
|
2024-08-17 04:26:07 +00:00
|
|
|
LASTB=$H
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Time
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$T","S"]} && echo "time" && setterm --foreground default
|
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};4${T}m\]"${SEP}
|
|
|
|
PS1="${PS1}${COLOURS["$T","B"]}\\t"
|
|
|
|
LASTB=$T
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Python venv
|
2023-02-24 01:24:59 +00:00
|
|
|
if [ "$VIRTUAL_ENV" ]; then
|
2022-12-29 22:50:45 +00:00
|
|
|
VENV="${VIRTUAL_ENV##*/}"
|
2023-02-24 01:24:59 +00:00
|
|
|
VENV=$(echo "$VENV" | sed -r 's/.*/[\0]/')
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$E","S"]} && echo "venv" && setterm --foreground default
|
|
|
|
PS1=${PS1}\[\e[0;3${LASTB};4${E}m\]${SEP}${COLOURS["$E","B"]}${VENV}
|
|
|
|
LASTB=$E
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Debian Chroot
|
|
|
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$C","S"]} && echo "debian chroot" && setterm --foreground default
|
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};4${C}m\]${SEP}"${COLOURS["$C","B"]}"<$(cat /etc/debian_chroot)>"
|
|
|
|
LASTB=$C
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# User
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $(whoami) == 'root' ]] && U=$R
|
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$U","S"]} && echo "user" && setterm --foreground default
|
2024-09-24 22:05:32 +00:00
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};4${U}m\]"${SEP}"${COLOURS["$U","B"]}\\u\[\e[0;3${TEXT};4${U}m\]@"
|
2024-08-17 04:26:07 +00:00
|
|
|
LASTB=$U
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Host
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$H","S"]} && echo "hostname" $([[ -z $SSH_TTY ]] || echo " (ssh)") && setterm --foreground default
|
2024-09-24 22:05:32 +00:00
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};4${H}m\]"${SEP}"${COLOURS["$H","B"]}\\h\[\e[0;3${TEXT};4${H}m\]:"
|
2024-08-17 04:26:07 +00:00
|
|
|
LASTB=$H
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Dir
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$D","S"]} && echo "directory" && setterm --foreground default
|
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};4${D}m\]"${SEP}"${COLOURS["$D","B"]}\\w"
|
|
|
|
LASTB=$D
|
2022-04-16 19:54:07 +00:00
|
|
|
|
|
|
|
# Git
|
|
|
|
GIT=$(parse_git_branch)
|
|
|
|
if [[ $GIT != '' ]]; then
|
2024-08-17 04:26:07 +00:00
|
|
|
[[ $1 ]] && setterm --foreground ${COLOURS["$G","S"]} && echo "git branch status" && setterm --foreground default
|
|
|
|
PS1="$PS1\[\e[0;3${LASTB};4${G}m\]"${SEP}${GIT}
|
|
|
|
LASTB=$G
|
2022-04-16 19:54:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# \n - start input on new line, again to support copy-paste into bash script
|
2024-08-17 04:26:07 +00:00
|
|
|
PS1="${PS1}\[\e[0;3${LASTB};1m\]"${SEP}"\[\e[0m\]\n"
|
2022-04-16 19:54:07 +00:00
|
|
|
|
2023-02-24 01:24:59 +00:00
|
|
|
export PS1
|