Skip to content

Instantly share code, notes, and snippets.

@SegHaxx
Created April 24, 2025 13:21
Show Gist options
  • Save SegHaxx/4fd061b39e5ea520c783d23fcd4f3458 to your computer and use it in GitHub Desktop.
Save SegHaxx/4fd061b39e5ea520c783d23fcd4f3458 to your computer and use it in GitHub Desktop.
05-prompt.sh
#!/bin/bash
function update {
errcod="$?"
newPWD=${PWD/#$HOME/\~}
if [ "$errcod" -eq "0" ]
then
unset fill errcod
else
errcod=" Err: $errcod"
fi
local prompt="${newPWD}${errcod}"
local promptsizetotal
let promptsizetotal=${promptsize}+${#prompt}
if [ "$promptsizetotal" -gt ${COLUMNS} ]
then
local cut
let cut=3+${promptsizetotal}-${COLUMNS}
newPWD="...$(echo -n $newPWD | sed -e "s/\(^.\{$cut\}\)\(.*\)/\2/")"
else
if [ -n "$errcod" ]
then
local fillsize
let fillsize=${COLUMNS}-${promptsizetotal}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="${fill} "
let fillsize=${fillsize}-1
done
fi
fi
}
function setprompt {
local cur_tty=$(tty)
cur_tty=${cur_tty:5}
COLUMNS=80
case $TERM in
xterm*|rxvt*|screen*)
local prompt=$(echo -n "${USER}@${HOSTNAME%%.*}:");
PROMPT_COMMAND="update;echo -ne \"\033]0;${USER}@${HOSTNAME%%.*}:\${newPWD} ($cur_tty)\007\""
;;
*)
local prompt=$(echo -n "${USER}@${HOSTNAME%%.*}: (${cur_tty})")
PROMPT_COMMAND=update
;;
esac
let promptsize=$(echo -n ${prompt}|wc -c|tr -d " ")
if [ "$UID" -eq "0" ]
then
local COLOR1="\[\033[0;1;7;31;47m\]"
local promptchar="#"
else
local COLOR1="\[\033[0;7;36m\]"
local promptchar="$"
fi
local COLOR2="\[\033[7;44m\]"
local COLOR3="\[\033[0;34m\]"
local COLOR4="\[\033[1;37m\]"
local NO_COLOR="\[\033[0m\]"
if [ $(tput colors) -lt "8" ]; then
unset COLOR1
unset COLOR2
unset COLOR3
unset COLOR4
unset NO_COLOR
fi
case $TERM in
xterm*|rxvt*|screen*)
PS1="$COLOR1\u$COLOR2@$COLOR1\h$COLOR3:$NO_COLOR\${newPWD}\${fill}\${errcod}\n\
$COLOR4$promptchar$NO_COLOR "
;;
*)
PS1="$COLOR1\u$COLOR2@$COLOR1\h$COLOR3:$NO_COLOR\${newPWD} ($cur_tty)\${fill}\${errcod}\n\
$COLOR4$promptchar$NO_COLOR "
;;
esac
}
setprompt
unset setprompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment