2022-03-01 17:32:57 +00:00
|
|
|
#!/bin/bash
|
2022-04-23 02:13:29 +00:00
|
|
|
# shebang for syntax highlighting purposes
|
2022-03-01 17:32:57 +00:00
|
|
|
|
2022-05-01 04:57:36 +00:00
|
|
|
# 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\]"
|
|
|
|
|
2022-03-01 20:11:44 +00:00
|
|
|
__debug_trap() {
|
2022-03-02 15:58:27 +00:00
|
|
|
# Set necessary pre-command variables (PROMPT_COMMAND is a
|
|
|
|
# command so its excluded here)
|
2022-03-07 03:12:14 +00:00
|
|
|
if [[ "$BASH_COMMAND" != "$PROMPT_COMMAND"
|
|
|
|
&& "$LAST_BASH_COMMAND" == "$PROMPT_COMMAND" ]]; then
|
|
|
|
INC_TIME=1
|
2022-05-01 04:57:36 +00:00
|
|
|
LAST_RT="$EPOCHREALTIME" # This should be the last thing done in this function
|
2022-03-01 20:11:44 +00:00
|
|
|
fi
|
2022-03-02 06:29:04 +00:00
|
|
|
LAST_BASH_COMMAND="$BASH_COMMAND"
|
2022-03-01 20:11:44 +00:00
|
|
|
}
|
2022-03-01 17:32:57 +00:00
|
|
|
|
2022-03-01 20:11:44 +00:00
|
|
|
trap '__debug_trap' DEBUG
|
2022-03-01 17:32:57 +00:00
|
|
|
|
|
|
|
__get_cmd_time() {
|
2022-03-01 20:11:44 +00:00
|
|
|
# Set hours minutes seconds and remove preceding zeros
|
2022-05-18 03:32:04 +00:00
|
|
|
local YEAR
|
|
|
|
local DAYS
|
|
|
|
local HOUR
|
|
|
|
local MINS
|
|
|
|
local SECS
|
|
|
|
((YEAR=10#0$(($(TZ=UTC printf '%(%Y)T' $CMD_TIME)))-1970))
|
|
|
|
((DAYS=10#0$(TZ=UTC printf '%(%j)T' $CMD_TIME)))
|
|
|
|
((HOUR=10#0$(TZ=UTC printf '%(%H)T' $CMD_TIME)))
|
|
|
|
((MINS=10#0$(TZ=UTC printf '%(%M)T' $CMD_TIME)))
|
|
|
|
((SECS=10#0$(TZ=UTC printf '%(%S)T' $CMD_TIME)))
|
2022-03-01 20:11:44 +00:00
|
|
|
|
|
|
|
# Choose whether or not to print hours minutes and seconds
|
2022-05-01 06:21:53 +00:00
|
|
|
[[ $CMD_TIME -ge 31536000 ]] && printf '%sy ' ${YEAR}
|
|
|
|
[[ $CMD_TIME -ge 86400 ]] && printf '%sd ' ${DAYS}
|
|
|
|
[[ $CMD_TIME -ge 3600 ]] && printf '%sh ' ${HOUR}
|
|
|
|
[[ $CMD_TIME -ge 60 ]] && printf '%sm ' ${MINS}
|
|
|
|
[[ $CMD_TIME -ge 1 ]] && printf '%ss ' ${SECS}
|
2022-05-01 04:57:36 +00:00
|
|
|
# If you want to have a limit uncomment the next line and replace somenum with
|
|
|
|
# the minimum microseconds
|
|
|
|
# [[ $CMD_US -ge somenum ]] &&
|
2022-03-06 14:11:27 +00:00
|
|
|
printf '%sμs' ${CMD_US:-0}
|
2022-03-01 17:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__sig() {
|
2022-03-01 20:11:44 +00:00
|
|
|
# Giant switch case for getting the name of the signal (`kill -l`)
|
2022-05-18 03:32:04 +00:00
|
|
|
local ITR=0
|
|
|
|
local RET
|
|
|
|
for RET in $@; do
|
|
|
|
if [[ $ITR != 0 ]]; then
|
2022-04-18 17:48:29 +00:00
|
|
|
printf '%s|%s' "$_WHT" "$_RED"
|
2022-03-06 14:11:27 +00:00
|
|
|
fi
|
2022-05-18 03:32:04 +00:00
|
|
|
((ITR++))
|
|
|
|
case $RET in
|
|
|
|
126): ACCES ;;
|
|
|
|
127): NOENT ;;
|
|
|
|
129): HUP ;;
|
|
|
|
130): INT ;;
|
|
|
|
131): QUIT ;;
|
|
|
|
132): ILL ;;
|
|
|
|
133): TRAP ;;
|
|
|
|
134): ABRT ;;
|
|
|
|
135): BUS ;;
|
|
|
|
136): FPE ;;
|
|
|
|
137): KILL ;;
|
|
|
|
138): USR1 ;;
|
|
|
|
139): SEGV ;;
|
|
|
|
140): USR2 ;;
|
|
|
|
141): PIPE ;;
|
|
|
|
142): ALRM ;;
|
|
|
|
143): TERM ;;
|
|
|
|
144): STKFLT ;;
|
|
|
|
145): CHLD ;;
|
|
|
|
146): CONT ;;
|
|
|
|
147): STOP ;;
|
|
|
|
148): TSTP ;;
|
|
|
|
149): TTIN ;;
|
|
|
|
150): TTOU ;;
|
|
|
|
151): URG ;;
|
|
|
|
152): XCPU ;;
|
|
|
|
153): XFSZ ;;
|
|
|
|
154): VTALRM ;;
|
|
|
|
155): PROF ;;
|
|
|
|
156): WINCH ;;
|
|
|
|
157): IO ;;
|
|
|
|
158): PWR ;;
|
|
|
|
159): SYS ;;
|
|
|
|
16[3-9]|1[7-8][0-9]|19[0-2]): RT$(($RET-128)) ;; # Savagery
|
|
|
|
*): $RET ;; # Print exit code if not in list
|
2022-03-06 14:11:27 +00:00
|
|
|
esac
|
2022-05-18 03:32:04 +00:00
|
|
|
printf '%s' "$_"
|
2022-03-06 14:11:27 +00:00
|
|
|
done
|
2022-03-01 17:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:48:29 +00:00
|
|
|
__ssh() {
|
|
|
|
local CON=($SSH_CONNECTION)
|
|
|
|
local SRV_IP="${CON[2]}"
|
|
|
|
[[ -z "$SRV_IP" ]] && return
|
|
|
|
local SRV_PORT="${CON[3]}"
|
|
|
|
# 4 chars from startand 4 chars from end
|
|
|
|
local SRV_IP_CUT="${_CYN}${SRV_IP}"
|
|
|
|
[[ ${#SRV_IP} -gt 8 ]] && local SRV_IP_CUT="${_CYN}${SRV_IP:0:4}${_WHT}*${_CYN}${SRV_IP: -4}"
|
2022-05-01 06:21:53 +00:00
|
|
|
|
|
|
|
printf '%s' "${_GRN}${_BLU}[${SRV_IP_CUT}${_PRP}${_BLD}:${_NON}${_CYN}${SRV_PORT}${_BLU}]${_NON}"
|
2022-04-18 17:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 17:32:57 +00:00
|
|
|
__prompt() {
|
2022-05-01 04:57:36 +00:00
|
|
|
# Get exit code (must be first)
|
2022-03-06 14:11:27 +00:00
|
|
|
local PLC=(${PIPESTATUS[@]})
|
2022-03-02 15:58:27 +00:00
|
|
|
|
2022-03-01 20:11:44 +00:00
|
|
|
# Reset time when prompt was first displayed after command
|
2022-05-18 03:32:04 +00:00
|
|
|
# this contributes to the ~40 microsecond difference in $CMD_US and the actual time it took
|
2022-05-01 06:21:53 +00:00
|
|
|
if [[ "$INC_TIME" != 0 ]]; then
|
2022-03-02 15:58:27 +00:00
|
|
|
PROMPT_RT="$EPOCHREALTIME"
|
2022-03-01 20:23:03 +00:00
|
|
|
INC_TIME=0
|
2022-03-01 17:32:57 +00:00
|
|
|
fi
|
2022-03-01 20:11:44 +00:00
|
|
|
|
2022-05-01 04:57:36 +00:00
|
|
|
# *_RT may not be set
|
|
|
|
LAST_RT="${LAST_RT:-$EPOCHREALTIME}"
|
|
|
|
PROMPT_RT="${PROMPT_RT:-$EPOCHREALTIME}"
|
|
|
|
|
2022-03-01 20:11:44 +00:00
|
|
|
# Get relative times
|
2022-03-02 06:29:04 +00:00
|
|
|
|
2022-03-08 19:15:01 +00:00
|
|
|
# Remove decimal point, simulating multiplying by 1 million
|
2022-05-01 04:57:36 +00:00
|
|
|
PROMPT_RT1M="${PROMPT_RT/.}"
|
|
|
|
LAST_RT1M="${LAST_RT/.}"
|
|
|
|
|
2022-05-18 03:32:04 +00:00
|
|
|
((CMD_US=PROMPT_RT1M-LAST_RT1M))
|
2022-03-08 19:15:01 +00:00
|
|
|
|
2022-05-01 06:21:53 +00:00
|
|
|
CMD_TIME=0
|
|
|
|
|
2022-05-18 03:32:04 +00:00
|
|
|
# Divide by 1 million to get a more accurate difference
|
|
|
|
((CMD_TIME=CMD_US/1000000))
|
2022-03-02 06:29:04 +00:00
|
|
|
|
2022-03-19 04:02:50 +00:00
|
|
|
[[ ${#CMD_US} -lt 6 ]] || CMD_US="${CMD_US: -6}"
|
2022-05-18 03:32:04 +00:00
|
|
|
((CMD_US=10#0$CMD_US))
|
2022-03-01 20:11:44 +00:00
|
|
|
|
2022-03-02 04:33:34 +00:00
|
|
|
# Set prompt sections
|
|
|
|
|
2022-03-02 04:55:56 +00:00
|
|
|
# Text
|
|
|
|
|
2022-04-18 17:48:29 +00:00
|
|
|
# ssh detection and indicator
|
|
|
|
[[ "$SSH_CONNECTION" ]] && local SSH="$(__ssh) "
|
|
|
|
|
2022-03-02 04:33:34 +00:00
|
|
|
# [INT], [4], etc.
|
2022-05-18 03:32:04 +00:00
|
|
|
local RET
|
|
|
|
for RET in ${PLC[@]}; do
|
|
|
|
if [[ $RET > 0 ]]; then
|
2022-04-18 17:48:29 +00:00
|
|
|
local SIG="$(printf '%s[%s%s%s] ' "$_BLU" "$_RED" "$(__sig ${PLC[@]})" "$_BLU")"
|
2022-03-06 14:11:27 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2022-03-02 04:55:56 +00:00
|
|
|
|
2022-03-02 04:33:34 +00:00
|
|
|
# [user@homeserver:~]
|
2022-04-18 17:48:29 +00:00
|
|
|
local COL="$([[ $UID == 0 ]] && printf '%s' "$_RED" || printf '%s' "$_YLW")"
|
|
|
|
local UHD="${_BLD}${_BLU}[${COL}\u${_PRP}@${_CYN}\h${_PRP}:${_GRN}\w${_BLU}]"
|
2022-03-02 04:55:56 +00:00
|
|
|
|
2022-03-02 04:33:34 +00:00
|
|
|
# 2y 351d 12m 43s 382969μs
|
2022-04-18 17:48:29 +00:00
|
|
|
local TIME="${_YLW}$(__get_cmd_time)"
|
2022-03-02 04:55:56 +00:00
|
|
|
|
2022-03-02 04:33:34 +00:00
|
|
|
# Random colored $ or #
|
2022-05-01 06:25:40 +00:00
|
|
|
_RAND256="\[\033[38;2;$((RANDOM%256));$((RANDOM%256));$((RANDOM%256))m\]"
|
2022-04-23 02:13:29 +00:00
|
|
|
local IND="${_RAND256}\\$"
|
2022-03-02 04:33:34 +00:00
|
|
|
|
2022-03-01 20:11:44 +00:00
|
|
|
# Set the prompt
|
2022-04-18 17:48:29 +00:00
|
|
|
PS1="${_NON}${SSH}${SIG}${UHD} ${TIME} ${IND} ${_NON}"
|
2022-03-01 17:32:57 +00:00
|
|
|
}
|
2022-03-02 04:33:34 +00:00
|
|
|
|
2022-03-01 20:23:03 +00:00
|
|
|
PROMPT_COMMAND=__prompt
|