Skip to content

Instantly share code, notes, and snippets.

@catleeball
Created January 29, 2020 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catleeball/d11a4224e65b92cad89a9fe15e61f815 to your computer and use it in GitHub Desktop.
Save catleeball/d11a4224e65b92cad89a9fe15e61f815 to your computer and use it in GitHub Desktop.
# ==============================================================================
# PS1 setup
# Color Variables
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
PURPLE=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
RESET=$(tput sgr0)
function ps1ify () {
echo "\[$1\]"
}
PS1_BOLD=$(ps1ify $BOLD)
PS1_RED=$(ps1ify $RED)
PS1_GREEN=$(ps1ify $GREEN)
PS1_BLUE=$(ps1ify $BLUE)
PS1_RESET=$(ps1ify $RESET)
# PS1_USER_HOST="\u@\h"
PS1_DATE="$PS1_GREEN[\t]$PS1_RESET"
PS1_PWD="\w"
# Set term window title
PS1_BASE="\[\e]0;$PS1_PWD\a\]"
#PS1_PRE_COLON=$PS1_BOLD$PS1_GREEN$PS1_USER_HOST$PS1_RESET
PS1_BASE="$PS1_BASE:$PS1_BOLD$PS1_BLUE$PS1_PWD$PS1_RESET\$ "
# Elapsed time of last command
function timer_start {
timer=${timer:-$SECONDS}
}
function timer_stop {
timer_show=$(($SECONDS - $timer))
unset timer
}
function convertsecs() {
m=$((($1/60)))
s=$(($1%60))
if [[ $m -lt 1 ]]; then
printf "%ds" $s
else
printf "%dm%ds" $m $s
fi
}
trap 'timer_start' DEBUG
# Add an indicator of an error code to the PS1
function error_code() {
if [[ $1 != 0 ]]; then
echo -n "$PS1_BOLD$PS1_RED<$1>$PS1_RESET "
fi
}
function finalize_ps1() {
status=$?
timer_stop
PS1="$PS1_DATE$PS1_BASE"
# Add elapsed time and error code
if [[ $timer_show -lt 1 ]]; then
PS1="$(error_code $status)$PS1"
else
PS1="$(error_code $status)$PS1_BLUE[$(convertsecs $timer_show)]$PS1_RESET$PS1"
fi
}
# Truncates beginning of path with '...' showing last N dir levels
export PROMPT_DIRTRIM=4
PROMPT_COMMAND=finalize_ps1
# ==============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment