From 906f7d707087cabce4b4ab89d80d7dfa48e86f56 Mon Sep 17 00:00:00 2001 From: Aleksandar 'The Cobra' Widulski Date: Sun, 1 May 2022 00:57:36 -0400 Subject: [PATCH] Update prompt to make time more accurate and faster Signed-off-by: Aleksandar 'The Cobra' Widulski --- .prompt | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/.prompt b/.prompt index e8a3a7a..e8fa955 100644 --- a/.prompt +++ b/.prompt @@ -1,13 +1,25 @@ #!/bin/bash # shebang for syntax highlighting purposes +# Colors +_NON="\[\033[0m\]" +_BLD="\[\033[1m\]" # YEP BALD +_BLK="\[\033[30m\]" +_RED="\[\033[31m\]" +_GRN="\[\033[32m\]" +_YLW="\[\033[33m\]" +_BLU="\[\033[34m\]" +_PRP="\[\033[35m\]" +_CYN="\[\033[36m\]" +_WHT="\[\033[37m\]" + __debug_trap() { # Set necessary pre-command variables (PROMPT_COMMAND is a # command so its excluded here) if [[ "$BASH_COMMAND" != "$PROMPT_COMMAND" && "$LAST_BASH_COMMAND" == "$PROMPT_COMMAND" ]]; then - LAST_RT="$EPOCHREALTIME" INC_TIME=1 + LAST_RT="$EPOCHREALTIME" # This should be the last thing done in this function fi LAST_BASH_COMMAND="$BASH_COMMAND" } @@ -28,6 +40,9 @@ __get_cmd_time() { [[ $CMD_TIME -ge 3600 ]] && printf '%sh ' ${HOUR:-0} [[ $CMD_TIME -ge 60 ]] && printf '%sm ' ${MINS:-0} [[ $CMD_TIME -ge 1 ]] && printf '%ss ' ${SECS:-0} + # If you want to have a limit uncomment the next line and replace somenum with + # the minimum microseconds +# [[ $CMD_US -ge somenum ]] && printf '%sμs' ${CMD_US:-0} } @@ -92,47 +107,36 @@ __ssh() { } __prompt() { - # Get exit code + # Get exit code (must be first) local PLC=(${PIPESTATUS[@]}) - # *_RT may not be set - LAST_RT="${LAST_RT:-$EPOCHREALTIME}" - PROMPT_RT="${PROMPT_RT:-$EPOCHREALTIME}" - # Reset time when prompt was first displayed after command if [[ ${INC_TIME:-1} != 0 ]]; then PROMPT_RT="$EPOCHREALTIME" INC_TIME=0 fi + # *_RT may not be set + LAST_RT="${LAST_RT:-$EPOCHREALTIME}" + PROMPT_RT="${PROMPT_RT:-$EPOCHREALTIME}" + # Get relative times # Remove decimal point, simulating multiplying by 1 million - PROMPT_RT1M="${PROMPT_RT%.*}${PROMPT_RT#*.}" - LAST_RT1M="${LAST_RT%.*}${LAST_RT#*.}" - - # Subtract and divide by 1 million to get a more accurate difference - CMD_TIME="$(((PROMPT_RT1M-LAST_RT1M)/1000000))" - CMD_TIME="${CMD_TIME:-0}" + PROMPT_RT1M="${PROMPT_RT/.}" + LAST_RT1M="${LAST_RT/.}" CMD_US="$(($PROMPT_RT1M-$LAST_RT1M))" + + # Subtract and divide by 1 million to get a more accurate difference + CMD_TIME="$((CMD_US/1000000))" + CMD_TIME="${CMD_TIME:-0}" + [[ ${#CMD_US} -lt 6 ]] || CMD_US="${CMD_US: -6}" CMD_US="$((10#0$CMD_US))" # Set prompt sections - # Colors - _NON="\[\033[0m\]" - _BLD="\[\033[1m\]" # YEP BALD - _BLK="\[\033[30m\]" - _RED="\[\033[31m\]" - _GRN="\[\033[32m\]" - _YLW="\[\033[33m\]" - _BLU="\[\033[34m\]" - _PRP="\[\033[35m\]" - _CYN="\[\033[36m\]" - _WHT="\[\033[37m\]" - # Text # ssh detection and indicator