.files/.prompt

125 lines
3.3 KiB
Bash

#!/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
# 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
# [INT], [4], etc.
local SIG="$([ $PEC -ne 0 ] && printf "$BLU[$RED$(__sig $PEC)$BLU] ")"
# [user@homeserver:~]
local CLR="$([ $UID -eq 0 ] && printf $RED || printf $YLW)"
local UHD="$BLD$BLU[$CLR\u$PRP@$CYN\h$PRP:$GRN\w$BLU]"
# 2y 351d 12m 43s 382969μs
local TIME="$YLW$(__get_cmd_time)"
# Random colored $ or #
local RAND256="\[\033[38;2;$RANDOM;$RANDOM;${RANDOM}m\]"
local IND="$RAND256\\$"
# Set the prompt
PS1="$NON$SIG$UHD $TIME $IND $NON"
}
PROMPT_COMMAND=__prompt