Skip to content

Instantly share code, notes, and snippets.

@cfraizer
Created June 4, 2020 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfraizer/33e0e2ae1845cd17a15b6b3e00fe7e54 to your computer and use it in GitHub Desktop.
Save cfraizer/33e0e2ae1845cd17a15b6b3e00fe7e54 to your computer and use it in GitHub Desktop.
Pure Bash vs. Sed
foo() {
local -a statusValues=(
"Discharging"
"Not Charging"
"Charging"
"Unknown"
"Full"
)
local -i valCount=0
valCount="${#statusValues[@]}"
local -i loopCount=1000
local -i i=0
for (( i = 0; i < loopCount; ++i )); do
local status="${statusValues[$RANDOM % valCount]}"
local warn="";
(( RANDOM % 2 == 0 )) && warn="YOW!"
local capacity="$(( RANDOM % 101 ))"
status="${status//,/}"
status="${status/#Discharging/Bat-}"
status="${status/#Not Charging/RedSq}"
status="${status/#Charging/Bat+}"
status="${status/#Unknown/Bat}"
status="${status/#Full/Bat+}"
status="${status} "
capacity="${capacity}%"
printf "%s%s%s\n" "$status" "$warn" "$capacity"
done
}
bar() {
local -a statusValues=(
"Discharging"
"Not Charging"
"Charging"
"Unknown"
"Full"
)
local -i valCount=0
valCount="${#statusValues[@]}"
local -i loopCount=1000
local -i i=0
for (( i = 0; i < loopCount; ++i )); do
local status="${statusValues[$RANDOM % valCount]}"
local warn="";
(( RANDOM % 2 == 0 )) && warn="YOW!"
local capacity="$(( RANDOM % 101 ))"
printf "%s%s%s\n" "$(echo "$status" | sed -e \
"s/,//\
;s/Discharging/Bat- / \
;s/Not Charging/RedSq / \
;s/Charging/Bat+ / \
;s/Unknown/Bat / \
;s/Full/Bat+ /")" "$warn" "$(echo "$capacity" | sed -e 's/$/%/' )"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment