.files/.prompt

125 lines
3.3 KiB
Plaintext
Raw Normal View History

2022-03-01 17:32:57 +00:00
#!/bin/bash
# shebang for syntax highlightin purposes
2022-03-01 20:11:44 +00:00
__debug_trap() {
# Set necessary pre-command variables (PROMPT_COMMAND is a command
# so its excluded here)
if [[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]]; then
2022-03-01 20:23:03 +00:00
LAST_EPOCH="$EPOCHSECONDS"
LAST_REALTIME="$EPOCHREALTIME"
INC_TIME=1
2022-03-01 20:11:44 +00:00
fi
}
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
local YEAR=$(($((10#$(TZ=UTC printf '%(%Y)T' $CMD_TIME)))-1970))
local DAYS=$((10#$(TZ=UTC printf '%(%j)T' $CMD_TIME)))
2022-03-01 20:11:44 +00:00
local HOUR=$((10#$(TZ=UTC printf '%(%H)T' $CMD_TIME)))
local MINS=$((10#$(TZ=UTC printf '%(%M)T' $CMD_TIME)))
local SECS=$((10#$(TZ=UTC printf '%(%S)T' $CMD_TIME))) #wtf sex???
# Choose whether or not to print hours minutes and seconds
[ $CMD_TIME -ge 31536000 ] && TZ=UTC printf '%sd ' ${YEAR:-0}
[ $CMD_TIME -ge 86400 ] && TZ=UTC printf '%sd ' ${DAYS:-0}
[ $CMD_TIME -ge 3600 ] && TZ=UTC printf '%sh ' ${HOUR:-0}
[ $CMD_TIME -ge 60 ] && TZ=UTC printf '%sm ' ${MINS:-0}
[ $CMD_TIME -ge 1 ] && TZ=UTC printf '%ss ' ${SECS:-0}
2022-03-02 04:55:56 +00:00
TZ=UTC 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-03-01 17:32:57 +00:00
case $1 in
126) printf ACCES ;;
127) printf NOENT ;;
129) printf HUP ;;
130) printf INT ;;
131) printf QUIT ;;
132) printf ILL ;;
133) printf TRAP ;;
134) printf ABRT ;;
135) printf BUS ;;
136) printf FPE ;;
137) printf KILL ;;
138) printf USR1 ;;
139) printf SEGV ;;
140) printf USR2 ;;
141) printf PIPE ;;
142) printf ALRM ;;
143) printf TERM ;;
144) printf STKFLT ;;
145) printf CHLD ;;
146) printf CONT ;;
147) printf STOP ;;
148) printf TSTP ;;
149) printf TTIN ;;
150) printf TTOU ;;
151) printf URG ;;
152) printf XCPU ;;
153) printf XFSZ ;;
154) printf VTALRM ;;
155) printf PROF ;;
156) printf WINCH ;;
157) printf IO ;;
158) printf PWR ;;
159) printf SYS ;;
2022-03-01 20:11:44 +00:00
16[3-9]|1[7-8][0-9]|19[0-2]) printf RT$(($1-128)) ;; # Savagery
*) printf $1 ;; # Print exit code if not in list
2022-03-01 17:32:57 +00:00
esac
}
__prompt() {
2022-03-01 20:11:44 +00:00
# Get exit code
2022-03-01 17:32:57 +00:00
PEC=$?
2022-03-01 20:11:44 +00:00
# Reset time when prompt was first displayed after command
2022-03-01 17:32:57 +00:00
if [ $INC_TIME -ne 0 ]; then
2022-03-01 20:23:03 +00:00
PROMPT_EPOCH="$EPOCHSECONDS"
PROMPT_REALTIME="$EPOCHREALTIME"
INC_TIME=0
2022-03-01 17:32:57 +00:00
fi
2022-03-01 20:11:44 +00:00
# Get relative times
2022-03-01 20:23:03 +00:00
CMD_TIME="$((PROMPT_EPOCH-LAST_EPOCH))"
local LAST_US="$((10#${LAST_REALTIME: -6}))"
CMD_US="$((1${PROMPT_REALTIME: -6}-${LAST_US:-0}))"
2022-03-01 20:23:03 +00:00
CMD_US="$((10#${CMD_US: -6}))"
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
# Colors
local NON="\[\033[0m\]"
local BLD="\[\033[1m\]" # YEP BALD
local BLK="\[\033[30m\]"
local RED="\[\033[31m\]"
local GRN="\[\033[32m\]"
local YLW="\[\033[33m\]"
local BLU="\[\033[34m\]"
local PRP="\[\033[35m\]"
local CYN="\[\033[36m\]"
local WHT="\[\033[37m\]"
# Text
2022-03-02 04:33:34 +00:00
# [INT], [4], etc.
2022-03-02 04:55:56 +00:00
local SIG="$([ $PEC -ne 0 ] && printf "$BLU[$RED$(__sig $PEC)$BLU] ")"
2022-03-02 04:33:34 +00:00
# [user@homeserver:~]
2022-03-02 04:55:56 +00:00
local CLR="$([ $UID -eq 0 ] && printf $RED || printf $YLW)"
local UHD="$BLD$BLU[$CLR\u$PRP@$CYN\h$PRP:$GRN\w$BLU]"
2022-03-02 04:33:34 +00:00
# 2y 351d 12m 43s 382969μs
2022-03-02 04:55:56 +00:00
local TIME="$YLW$(__get_cmd_time)"
2022-03-02 04:33:34 +00:00
# Random colored $ or #
2022-03-02 04:55:56 +00:00
local RAND256="\[\033[38;2;$RANDOM;$RANDOM;${RANDOM}m\]"
local IND="$RAND256\\$"
2022-03-02 04:33:34 +00:00
2022-03-01 20:11:44 +00:00
# Set the prompt
2022-03-02 04:55:56 +00:00
PS1="$NON$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