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() { lip() {
local OLD_IFS="$IFS" local OLD_IFS="$IFS"
local LINE_NUM=0 local LINE_NUM=0
local STR_NUM=0 while read -r _ IP _; do
while read -r LINE; do if [[ "$IP" == "${1:-$WIFI}": ]]; then
if [[ "$LINE" == *"${1:-$WIFI}"* ]]; then ((LINE_NUM++))
LINE_NUM=$((LINE_NUM+1))
if [[ $LINE_NUM == 2 ]]; then if [[ $LINE_NUM == 2 ]]; then
for STR in $LINE; do IFS="/"
STR_NUM=$((STR_NUM+1)) for STR in $IP; do
if [[ $STR_NUM == 2 ]]; then echo "$STR"
local IP="$STR" break 3
IFS="/"
for STR in $IP; do
echo "$STR"
break 3
done
fi
done done
fi fi
fi fi
[[ $LINE_NUM -gt 0 ]] && ((LINE_NUM++))
done <<< "$(ip a)" done <<< "$(ip a)"
IFS="$OLD_IFS" IFS="$OLD_IFS"
} }
@ -82,7 +76,7 @@ urlencode() {
local LENGTH="${#TEXT}" local LENGTH="${#TEXT}"
for (( i = 0; i < LENGTH; i++ )); do for (( i = 0; i < LENGTH; i++ )); do
local c="${TEXT:$i:1}" local c="${TEXT:$i:1}"
case $c in case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;; [a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;; *) printf '%%%02X' "'$c" ;;
esac esac
@ -92,4 +86,8 @@ urlencode() {
diffhome() { find . -type f -regex '\./\.[^g].*' -exec diff -q ~/{} {} 2> /dev/null \;; } 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() { __get_cmd_time() {
# Set hours minutes seconds and remove preceding zeros # Set hours minutes seconds and remove preceding zeros
local YEAR=$((10#0$(($(TZ=UTC printf '%(%Y)T' $CMD_TIME)))-1970)) local YEAR
local DAYS=$((10#0$(TZ=UTC printf '%(%j)T' $CMD_TIME))) local DAYS
local HOUR=$((10#0$(TZ=UTC printf '%(%H)T' $CMD_TIME))) local HOUR
local MINS=$((10#0$(TZ=UTC printf '%(%M)T' $CMD_TIME))) local MINS
local SECS=$((10#0$(TZ=UTC printf '%(%S)T' $CMD_TIME))) 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 # Choose whether or not to print hours minutes and seconds
[[ $CMD_TIME -ge 31536000 ]] && printf '%sy ' ${YEAR} [[ $CMD_TIME -ge 31536000 ]] && printf '%sy ' ${YEAR}
@ -48,49 +53,51 @@ __get_cmd_time() {
__sig() { __sig() {
# Giant switch case for getting the name of the signal (`kill -l`) # Giant switch case for getting the name of the signal (`kill -l`)
j=0 local ITR=0
for i in $@; do local RET
if [[ $j != 0 ]]; then for RET in $@; do
if [[ $ITR != 0 ]]; then
printf '%s|%s' "$_WHT" "$_RED" printf '%s|%s' "$_WHT" "$_RED"
fi fi
j=$((j+1)) ((ITR++))
case $i in case $RET in
126) printf ACCES ;; 126): ACCES ;;
127) printf NOENT ;; 127): NOENT ;;
129) printf HUP ;; 129): HUP ;;
130) printf INT ;; 130): INT ;;
131) printf QUIT ;; 131): QUIT ;;
132) printf ILL ;; 132): ILL ;;
133) printf TRAP ;; 133): TRAP ;;
134) printf ABRT ;; 134): ABRT ;;
135) printf BUS ;; 135): BUS ;;
136) printf FPE ;; 136): FPE ;;
137) printf KILL ;; 137): KILL ;;
138) printf USR1 ;; 138): USR1 ;;
139) printf SEGV ;; 139): SEGV ;;
140) printf USR2 ;; 140): USR2 ;;
141) printf PIPE ;; 141): PIPE ;;
142) printf ALRM ;; 142): ALRM ;;
143) printf TERM ;; 143): TERM ;;
144) printf STKFLT ;; 144): STKFLT ;;
145) printf CHLD ;; 145): CHLD ;;
146) printf CONT ;; 146): CONT ;;
147) printf STOP ;; 147): STOP ;;
148) printf TSTP ;; 148): TSTP ;;
149) printf TTIN ;; 149): TTIN ;;
150) printf TTOU ;; 150): TTOU ;;
151) printf URG ;; 151): URG ;;
152) printf XCPU ;; 152): XCPU ;;
153) printf XFSZ ;; 153): XFSZ ;;
154) printf VTALRM ;; 154): VTALRM ;;
155) printf PROF ;; 155): PROF ;;
156) printf WINCH ;; 156): WINCH ;;
157) printf IO ;; 157): IO ;;
158) printf PWR ;; 158): PWR ;;
159) printf SYS ;; 159): SYS ;;
16[3-9]|1[7-8][0-9]|19[0-2]) printf RT$(($i-128)) ;; # Savagery 16[3-9]|1[7-8][0-9]|19[0-2]): RT$(($RET-128)) ;; # Savagery
*) printf $i ;; # Print exit code if not in list *): $RET ;; # Print exit code if not in list
esac esac
printf '%s' "$_"
done done
} }
@ -111,7 +118,7 @@ __prompt() {
local PLC=(${PIPESTATUS[@]}) local PLC=(${PIPESTATUS[@]})
# Reset time when prompt was first displayed after command # 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 if [[ "$INC_TIME" != 0 ]]; then
PROMPT_RT="$EPOCHREALTIME" PROMPT_RT="$EPOCHREALTIME"
INC_TIME=0 INC_TIME=0
@ -127,15 +134,15 @@ __prompt() {
PROMPT_RT1M="${PROMPT_RT/.}" PROMPT_RT1M="${PROMPT_RT/.}"
LAST_RT1M="${LAST_RT/.}" LAST_RT1M="${LAST_RT/.}"
CMD_US="$(($PROMPT_RT1M-$LAST_RT1M))" ((CMD_US=PROMPT_RT1M-LAST_RT1M))
CMD_TIME=0 CMD_TIME=0
# Remove last 6 chars, simulating dividing by 1 million to get a more accurate difference # Divide by 1 million to get a more accurate difference
[[ ${#CMD_US} -lt 6 ]] || CMD_TIME="${CMD_US::-6}" ((CMD_TIME=CMD_US/1000000))
[[ ${#CMD_US} -lt 6 ]] || CMD_US="${CMD_US: -6}" [[ ${#CMD_US} -lt 6 ]] || CMD_US="${CMD_US: -6}"
CMD_US="$((10#0$CMD_US))" ((CMD_US=10#0$CMD_US))
# Set prompt sections # Set prompt sections
@ -145,9 +152,9 @@ __prompt() {
[[ "$SSH_CONNECTION" ]] && local SSH="$(__ssh) " [[ "$SSH_CONNECTION" ]] && local SSH="$(__ssh) "
# [INT], [4], etc. # [INT], [4], etc.
local i local RET
for i in ${PLC[@]}; do for RET in ${PLC[@]}; do
if [[ $i > 0 ]]; then if [[ $RET > 0 ]]; then
local SIG="$(printf '%s[%s%s%s] ' "$_BLU" "$_RED" "$(__sig ${PLC[@]})" "$_BLU")" local SIG="$(printf '%s[%s%s%s] ' "$_BLU" "$_RED" "$(__sig ${PLC[@]})" "$_BLU")"
break break
fi fi