Update prompt to make time more accurate and faster

Signed-off-by: Aleksandar 'The Cobra' Widulski <thecobra@riseup.net>
This commit is contained in:
Aleksandar 'The Cobra' Widulski 2022-05-01 00:57:36 -04:00
parent 9b6d88b0b9
commit 906f7d7070
No known key found for this signature in database
GPG Key ID: C8F7E7CA91BCBA64
1 changed files with 28 additions and 24 deletions

52
.prompt
View File

@ -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