Optimizations

Signed-off-by: Aleksandar 'The Cobra' Widulski <thecobra@riseup.net>
This commit is contained in:
Aleksandar 'The Cobra' Widulski 2022-05-17 23:32:04 -04:00
parent 3fa1ef8d1b
commit 2736c6a371
Signed by: cobra
GPG Key ID: 4FD8F812083FF6F9
2 changed files with 75 additions and 70 deletions

32
.bashrc
View File

@ -51,24 +51,18 @@ copy() {
lip() {
local OLD_IFS="$IFS"
local LINE_NUM=0
local STR_NUM=0
while read -r LINE; do
if [[ "$LINE" == *"${1:-$WIFI}"* ]]; then
LINE_NUM=$((LINE_NUM+1))
while read -r _ IP _; do
if [[ "$IP" == "${1:-$WIFI}": ]]; then
((LINE_NUM++))
if [[ $LINE_NUM == 2 ]]; then
for STR in $LINE; do
STR_NUM=$((STR_NUM+1))
if [[ $STR_NUM == 2 ]]; then
local IP="$STR"
IFS="/"
for STR in $IP; do
echo "$STR"
break 3
done
fi
IFS="/"
for STR in $IP; do
echo "$STR"
break 3
done
fi
fi
fi
[[ $LINE_NUM -gt 0 ]] && ((LINE_NUM++))
done <<< "$(ip a)"
IFS="$OLD_IFS"
}
@ -82,7 +76,7 @@ urlencode() {
local LENGTH="${#TEXT}"
for (( i = 0; i < LENGTH; i++ )); do
local c="${TEXT:$i:1}"
case $c in
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
@ -92,4 +86,8 @@ urlencode() {
diffhome() { find . -type f -regex '\./\.[^g].*' -exec diff -q ~/{} {} 2> /dev/null \;; }
fixdh() { diffhome | awk '{print $2 " " $4}' | while read -r line; do cp $line; done; }
fixdh() {
diffhome | while read -r _ SRC _ DEST _; do
cp $SRC $DEST;
done;
}

113
.prompt
View File

@ -28,11 +28,16 @@ trap '__debug_trap' DEBUG
__get_cmd_time() {
# Set hours minutes seconds and remove preceding zeros
local YEAR=$((10#0$(($(TZ=UTC printf '%(%Y)T' $CMD_TIME)))-1970))
local DAYS=$((10#0$(TZ=UTC printf '%(%j)T' $CMD_TIME)))
local HOUR=$((10#0$(TZ=UTC printf '%(%H)T' $CMD_TIME)))
local MINS=$((10#0$(TZ=UTC printf '%(%M)T' $CMD_TIME)))
local SECS=$((10#0$(TZ=UTC printf '%(%S)T' $CMD_TIME)))
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)))
# Choose whether or not to print hours minutes and seconds
[[ $CMD_TIME -ge 31536000 ]] && printf '%sy ' ${YEAR}
@ -48,49 +53,51 @@ __get_cmd_time() {
__sig() {
# Giant switch case for getting the name of the signal (`kill -l`)
j=0
for i in $@; do
if [[ $j != 0 ]]; then
local ITR=0
local RET
for RET in $@; do
if [[ $ITR != 0 ]]; then
printf '%s|%s' "$_WHT" "$_RED"
fi
j=$((j+1))
case $i 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$(($i-128)) ;; # Savagery
*) printf $i ;; # Print exit code if not in list
((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
esac
printf '%s' "$_"
done
}
@ -111,7 +118,7 @@ __prompt() {
local PLC=(${PIPESTATUS[@]})
# Reset time when prompt was first displayed after command
# this contributes to the 40 microsecond difference in $CMD_US and the actual time it took
# this contributes to the ~40 microsecond difference in $CMD_US and the actual time it took
if [[ "$INC_TIME" != 0 ]]; then
PROMPT_RT="$EPOCHREALTIME"
INC_TIME=0
@ -127,15 +134,15 @@ __prompt() {
PROMPT_RT1M="${PROMPT_RT/.}"
LAST_RT1M="${LAST_RT/.}"
CMD_US="$(($PROMPT_RT1M-$LAST_RT1M))"
((CMD_US=PROMPT_RT1M-LAST_RT1M))
CMD_TIME=0
# Remove last 6 chars, simulating dividing by 1 million to get a more accurate difference
[[ ${#CMD_US} -lt 6 ]] || CMD_TIME="${CMD_US::-6}"
# Divide by 1 million to get a more accurate difference
((CMD_TIME=CMD_US/1000000))
[[ ${#CMD_US} -lt 6 ]] || CMD_US="${CMD_US: -6}"
CMD_US="$((10#0$CMD_US))"
((CMD_US=10#0$CMD_US))
# Set prompt sections
@ -145,9 +152,9 @@ __prompt() {
[[ "$SSH_CONNECTION" ]] && local SSH="$(__ssh) "
# [INT], [4], etc.
local i
for i in ${PLC[@]}; do
if [[ $i > 0 ]]; then
local RET
for RET in ${PLC[@]}; do
if [[ $RET > 0 ]]; then
local SIG="$(printf '%s[%s%s%s] ' "$_BLU" "$_RED" "$(__sig ${PLC[@]})" "$_BLU")"
break
fi