Skip to content

Instantly share code, notes, and snippets.

@nopeless
Created June 28, 2025 17:20
Show Gist options
  • Select an option

  • Save nopeless/a5eb297f7f9fbe1c26f2736bf15d648c to your computer and use it in GitHub Desktop.

Select an option

Save nopeless/a5eb297f7f9fbe1c26f2736bf15d648c to your computer and use it in GitHub Desktop.
pathColor="\033[38;2;104;169;196m"
errColor="\033[38;2;255;60;60m"
sucColor="\033[38;2;152;82;173m"
resetColor="\033[m"
if ((EUID)); then
userColor="\033[38;2;201;116;173m"
userSymbol='$'
else
userColor="\033[37m"
userSymbol='#'
fi
__command_current_start_ns=""
__last_command_duration_ms=0
__record_command_start_time() {
case "$BASH_COMMAND" in
"PROMPT_COMMAND" | "__record_command_start_time" | "__calculate_duration" | "prompt_command") return ;;
*)
__command_current_start_ns=$(date +%s%N)
;;
esac
}
__calculate_duration() {
local end_ns=$(date +%s%N)
if [[ -n "${__command_current_start_ns:-}" ]]; then
local duration_ns=$((end_ns - __command_current_start_ns))
__last_command_duration_ms=$((duration_ns / 1000000))
unset __command_current_start_ns
else
__last_command_duration_ms=0
fi
}
prompt_command() {
local PS1_string=""
local display_pwd="$PWD"
[[ "$PWD" =~ ^$HOME ]] && display_pwd="~${PWD#$HOME}"
PS1_string+="\[$userColor\]$USER \[$pathColor\]$display_pwd\[$resetColor\]"
local branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
local tag="$(git describe --tags --abbrev=0 2>/dev/null)"
if [[ -n "$branch" ]]; then
if [[ "$branch" != "HEAD" && "$branch" != "main" && "$branch" != "master" ]]; then
PS1_string+=" \[\033[2m\]$branch\[$resetColor\]"
fi
PS1_string+=" \[\033[38;2;243;79;41m\]\[$resetColor\]\[\033[2m\]"
if [[ -n "$tag" ]]; then
PS1_string+="(""$tag"")"
fi
PS1_string+="\[$resetColor\]"
fi
local display_duration=""
local duration_ms="${__last_command_duration_ms}"
local second=1000
local minute=60000
local hour=3600000
local day=86400000
if ((duration_ms >= 0)); then
if ((duration_ms < second)); then
printf -v display_duration "%5dms" "$duration_ms"
elif ((duration_ms < minute)); then
local s_int=$((duration_ms / second))
local s_dec=$(((duration_ms % second) / 10))
printf -v display_duration "%2d.%02ds " "$s_int" "$s_dec"
elif ((duration_ms < hour)); then
local m=$((duration_ms / minute))
local s=$((duration_ms % minute / second))
printf -v display_duration "%2dm %2ds" "$m" "$s"
elif ((duration_ms < day)); then
local h=$((duration_ms / hour))
local m=$((duration_ms % hour / minute))
printf -v display_duration "%2dh %2dm" "$h" "$m"
elif ((duration_ms < 100 * day)); then
local d=$((duration_ms / day))
local h=$((duration_ms % day / hour))
printf -v display_duration "%2dd %2dh" "$d" "$h"
else
local d=$((duration_ms / day))
printf -v display_duration "%6dd" "$d"
fi
PS1_string+=" \[\033[38;2;219;118;249m\]$display_duration\[$resetColor\]"
fi
local status_color
if (($?)); then
status_color="$errColor"
else
status_color="$sucColor"
fi
PS1_string+=" \[$status_color\]$userSymbol\[$resetColor\]"
PS1="${PS1_string} "
}
trap '__record_command_start_time' DEBUG
PROMPT_COMMAND='__calculate_duration; prompt_command'
PS4="-[\033[33m${BASH_SOURCE[0]%.sh}\033[m: \033[32m$LINENO\033[m]\
${FUNCNAME:+${FUNCNAME[0]}(): }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment