.files/.prompt

108 lines
3.1 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# shebang for syntax highlightin purposes
__debug_trap() {
# Set necessary pre-command variables (PROMPT_COMMAND is a command
# so its excluded here)
if [[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]]; then
LAST_EPOCH="$EPOCHSECONDS"
LAST_REALTIME="$EPOCHREALTIME"
INC_TIME=1
fi
}
trap '__debug_trap' DEBUG
__get_cmd_time() {
# 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)))
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}
TZ=UTC printf '%sμs ' ${CMD_US:-0}
}
__sig() {
# Giant switch case for getting the name of the signal (`kill -l`)
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 ;;
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
esac
}
__prompt() {
# Get exit code
PEC=$?
# Reset time when prompt was first displayed after command
if [ $INC_TIME -ne 0 ]; then
PROMPT_EPOCH="$EPOCHSECONDS"
PROMPT_REALTIME="$EPOCHREALTIME"
INC_TIME=0
fi
# Get relative times
CMD_TIME="$((PROMPT_EPOCH-LAST_EPOCH))"
local LAST_US="$((10#${LAST_REALTIME: -6}))"
CMD_US="$((1${PROMPT_REALTIME: -6}-${LAST_US:-0}))"
CMD_US="$((10#${CMD_US: -6}))"
# Set prompt sections
# [INT], [4], etc.
local SIG="$([ $PEC -ne 0 ] && printf "\[\][\[\]$(__sig $PEC)\[\]] ")"
# [user@homeserver:~]
local UHD="\[\][\[[3\$([ $(id -u) -eq 0 ] && printf 1 || printf 3)m\]\u\[\]@\[\]\h\[\]:\[\]\w\[\]]"
# 2y 351d 12m 43s 382969μs
local TIME="\[\]$(__get_cmd_time)"
# Random colored $ or #
local IND="\[[38;2;$RANDOM;$RANDOM;${RANDOM}m\]\\$"
# Remove all color
local ZERO="\[\]"
# Set the prompt
PS1="$ZERO$SIG$UHD $TIME$IND $ZERO"
}
PROMPT_COMMAND=__prompt