diff --git a/.local/libexec/statusbar/colors b/.local/libexec/statusbar/libsb similarity index 58% rename from .local/libexec/statusbar/colors rename to .local/libexec/statusbar/libsb index f56b3e19..64f5c270 100755 --- a/.local/libexec/statusbar/colors +++ b/.local/libexec/statusbar/libsb @@ -21,3 +21,50 @@ export bgmagenta="background='#BB9AF7$bgalpha'" export bgcyan="background='#7DCFFF$bgalpha'" export bggray="background='#414868$bgalpha'" export bgwhite="background='#C0CAF5$bgalpha'" + +meter_bar() { + # shellcheck disable=SC2194 + case $1 in + 9[0-9] | 100) + color="$fgred" + bar="▉" + ;; + 8[0-9]) + color="$fgorange" + bar="█" + ;; + 7[0-9]) + color="$fgorange" + bar="▇" + ;; + 6[0-9]) + color="$fgorange" + bar="▆" + ;; + 5[0-9]) + color="$fggreen" + bar="▅" + ;; + 4[0-9]) + color="$fggreen" + bar="▄" + ;; + 3[0-9]) + color="$fggreen" + bar="▂" + ;; + 2[0-9]) + color="$fggreen" + bar="▂" + ;; + 1[0-9]) + color="$fggreen" + bar="▁" + ;; + 0[0-9] | [0-9]) + color="$fggreen" + bar=" " + ;; + esac + printf "%s:%s" "${color}" "${bar}" +} diff --git a/.local/libexec/statusbar/sb-battery b/.local/libexec/statusbar/sb-battery index af58a968..af9c0795 100755 --- a/.local/libexec/statusbar/sb-battery +++ b/.local/libexec/statusbar/sb-battery @@ -1,45 +1,45 @@ #!/bin/sh -. "colors" +. libsb [ -z "$(ls /sys/class/power_supply)" ] && exit 0 for battery in /sys/class/power_supply/BAT?*; do - pidof -x sbd-battery >/dev/null 2>&1 || "$HOME/.local/libexec/daemons/sbd-battery" "$battery" >/dev/null 2>&1 & - [ -n "${capacity+x}" ] && printf " " - capacity="$(cat "$battery/capacity" 2>&1)" - case "$(cat "$battery/status" 2>&1)" in - "Full") - icon="" - color="$fgcyan" - ;; - "Not charging") - icon="" - color="$fgorange" - ;; - "Unknown") - icon="" - color="$fgorange" - ;; - "Charging") - icon="" - color="$fgcyan" - ;; - "Discharging") - case $capacity in - [0-5]) icon="" color="$fgred" ;; - [6-9] | 10) icon="" color="$fgred" ;; - 1[1-9] | 20) icon="" color="$fgred" ;; - 2[1-9] | 30) icon="" color="$fgwhite" ;; - 3[1-9] | 40) icon="" color="$fgwhite" ;; - 4[1-9] | 50) icon="" color="$fgwhite" ;; - 5[1-9] | 60) icon="" color="$fgwhite" ;; - 6[1-9] | 70) icon="" color="$fgwhite" ;; - 7[1-9] | 80) icon="" color="$fgwhite" ;; - 8[1-9] | 90) icon="" color="$fgwhite" ;; - 9[1-9] | 100) icon="" color="$fgwhite" ;; - esac - ;; - *) exit 1 ;; - esac - printf "%b" "<span $color>$icon</span>" + pidof -x sbd-battery >/dev/null 2>&1 || "$HOME/.local/libexec/daemons/sbd-battery" "$battery" >/dev/null 2>&1 & + [ -n "${capacity+x}" ] && printf " " + capacity="$(cat "$battery/capacity" 2>&1)" + case "$(cat "$battery/status" 2>&1)" in + "Full") + icon="" + color="$fgcyan" + ;; + "Not charging") + icon="" + color="$fgorange" + ;; + "Unknown") + icon="" + color="$fgorange" + ;; + "Charging") + icon="" + color="$fgcyan" + ;; + "Discharging") + case $capacity in + [0-5]) icon="" color="$fgred" ;; + [6-9] | 10) icon="" color="$fgred" ;; + 1[1-9] | 20) icon="" color="$fgred" ;; + 2[1-9] | 30) icon="" color="$fgwhite" ;; + 3[1-9] | 40) icon="" color="$fgwhite" ;; + 4[1-9] | 50) icon="" color="$fgwhite" ;; + 5[1-9] | 60) icon="" color="$fgwhite" ;; + 6[1-9] | 70) icon="" color="$fgwhite" ;; + 7[1-9] | 80) icon="" color="$fgwhite" ;; + 8[1-9] | 90) icon="" color="$fgwhite" ;; + 9[1-9] | 100) icon="" color="$fgwhite" ;; + esac + ;; + *) exit 1 ;; + esac + printf "%b" "<span $color>$icon</span>" done diff --git a/.local/libexec/statusbar/sb-cpu b/.local/libexec/statusbar/sb-cpu index 4b09128c..48870a67 100755 --- a/.local/libexec/statusbar/sb-cpu +++ b/.local/libexec/statusbar/sb-cpu @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb n_cpu=$(grep -c 'cpu[0-9]\+' /proc/stat) cache=$XDG_RUNTIME_DIR/cpubarscache @@ -28,23 +28,16 @@ for i in $(seq "$n_cpu"); do delta_idle=$((new_idle - old_idle)) delta_total=$((new_total - old_total)) - percent=$((((delta_total - delta_idle) * 100 / delta_total) / 10)) + percent=$(((delta_total - delta_idle) * 100 / delta_total)) - case "$percent" in - 0 | 1) color="$fggreen" bar="▁" ;; - 2) color="$fggreen" bar="▂" ;; - 3 | 4) color="$fggreen" bar="▃" ;; - 5) color="$fgorange" bar="▄" ;; - 6 | 7) color="$fgorange" bar="▅" ;; - 8) color="$fgred" bar="▆" ;; - *) color="$fgred" bar="▇" ;; - esac + meter=$(meter_bar "$percent") + color=$(echo "$meter" | cut -d: -f1) if [ "$color" != "$prevcolor" ]; then $first || printf "%b" "</span>" printf "%b" "<span $color>" fi - printf "%b" "$bar" + printf "%b" "$(echo "$meter" | cut -d: -f2)" first=false prevcolor=$color diff --git a/.local/libexec/statusbar/sb-disk b/.local/libexec/statusbar/sb-disk index 3749ee21..4b7dd2fa 100755 --- a/.local/libexec/statusbar/sb-disk +++ b/.local/libexec/statusbar/sb-disk @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb types=${SB_DISK_FS:-ext2,ext3,ext4,xfs,btrfs,vfat} @@ -8,29 +8,22 @@ IFS=' first=true for mnt in $(findmnt -Py -t "$types"); do - eval "$mnt" - case "$TARGET" in - /var/lib/docker) break ;; - /) icon="" ;; - /home) icon="" ;; - /boot*) icon="" ;; - *) icon="" ;; - esac + eval "$mnt" + case "$TARGET" in + /var/lib/docker) break ;; + /) icon="" ;; + /home) icon="" ;; + /boot*) icon="" ;; + *) icon="" ;; + esac - $first || printf " " + $first || printf " " - percent=$(df "$TARGET" | awk 'NR != 1 { gsub(/%/, "", $5); printf "%d", $5 / 10 }') - case "$percent" in - 0 | 1) color="$fggreen" bar="▁" ;; - 2) color="$fggreen" bar="▂" ;; - 3 | 4) color="$fggreen" bar="▃" ;; - 5) color="$fggreen" bar="▄" ;; - 6 | 7) color="$fggreen" bar="▅" ;; - 8) color="$fgorange" bar="▆" ;; - *) color="$fgred" bar="▇" ;; - esac + percent=$(df "$TARGET" | awk 'NR != 1 { gsub(/%/, "", $5); printf "%d", $5 }') - printf "%b <span %b %b>%b</span>" "${icon}" "${color}" "${bggray}" "${bar}" + meter=$(meter_bar "$percent") - first=false + printf "%b <span %b %b>%b</span>" "${icon}" "$(echo "$meter" | cut -d: -f1)" "${bggray}" "$(echo "$meter" | cut -d: -f2)" + + first=false done diff --git a/.local/libexec/statusbar/sb-forecast b/.local/libexec/statusbar/sb-forecast index cc389762..258f4000 100755 --- a/.local/libexec/statusbar/sb-forecast +++ b/.local/libexec/statusbar/sb-forecast @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb # NOTE: This script takes its dwmblocks update signal as an argument so that it can refresh itself (defaults to 5) # Options can be found at https://wttr.in/:help @@ -11,27 +11,27 @@ weatheropts="?FA" emojiopts="?FA&format=%c" getforecast() { - curl -sfL --retry 5 "$url$weatheropts" >"$weatherfile" || return 1 - curl -sfL "$url$emojiopts" --retry 5 | sed 's/☀️//;s/☁️//;s/⛅️//;s/⛈//;s/✨//;s/❄️//;s/🌦//;s/🌧//;s/🌨//;s/🌩//;s/🌫//;' | cut -d ' ' -f 1 >"$emojifile" || return 1 + curl -sfL --retry 5 "$url$weatheropts" >"$weatherfile" || return 1 + curl -sfL "$url$emojiopts" --retry 5 | sed 's/☀️//;s/☁️//;s/⛅️//;s/⛈//;s/✨//;s/❄️//;s/🌦//;s/🌧//;s/🌨//;s/🌩//;s/🌫//;' | cut -d ' ' -f 1 >"$emojifile" || return 1 } fresh() { - [ -s "$weatherfile" ] && [ "$(stat -c %y "$weatherfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] + [ -s "$weatherfile" ] && [ "$(stat -c %y "$weatherfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] } readfile() { - weather="$(cat "$weatherfile")" - precipitation="$(echo "$weather" | sed '16q;d' | grep -wo "[0-9]*%" | sort -rn | head -1q)" - highlow="$(echo "$weather" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/[+m]//g' | sort -g | sed -e 1b -e '$!d')" - emoji="$(cat "$emojifile")" + weather="$(cat "$weatherfile")" + precipitation="$(echo "$weather" | sed '16q;d' | grep -wo "[0-9]*%" | sort -rn | head -1q)" + highlow="$(echo "$weather" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/[+m]//g' | sort -g | sed -e 1b -e '$!d')" + emoji="$(cat "$emojifile")" } output() { - readfile - printf "%b" "$emoji $precipitation <span $fgblue></span> $(echo "$highlow" | cut -d" " -f1)° <span $fgred></span> $(echo "$highlow" | cut -d" " -f2)°\n" + readfile + printf "%b" "$emoji $precipitation <span $fgblue></span> $(echo "$highlow" | cut -d" " -f1)° <span $fgred></span> $(echo "$highlow" | cut -d" " -f2)°\n" } if fresh; then - output + output else - # WARN: Not currently used in dwmblocks - getforecast && pkill "-RTMIN+5" dwmblocks & - printf "%b" " Getting Weather\n" + # WARN: Not currently used in dwmblocks + getforecast && pkill "-RTMIN+5" dwmblocks & + printf "%b" " Getting Weather\n" fi diff --git a/.local/libexec/statusbar/sb-internet b/.local/libexec/statusbar/sb-internet index c4338a69..3a8741be 100755 --- a/.local/libexec/statusbar/sb-internet +++ b/.local/libexec/statusbar/sb-internet @@ -1,76 +1,76 @@ #!/bin/sh -. "colors" +. libsb status() { - nmcli --terse device status | awk -v device="$1" -F: '$1 == device { print $3 }' + nmcli --terse device status | awk -v device="$1" -F: '$1 == device { print $3 }' } wireless() { - if grep -vxq '0x\(1\|9\)003' "$info/flags"; then - icon="" - else - case "$(status "$device")" in - connected) - percent=$(awk -v interface="${device}:" '$1==interface { gsub(/[^0-9]/,"",$3); print $3 }' /proc/net/wireless) - # shellcheck disable=SC2194 - case 1 in - $((percent >= 80))) icon="" ;; - $((percent >= 60))) icon="" ;; - $((percent >= 40))) icon="" ;; - $((percent >= 20))) icon="" ;; - $((percent >= 0))) icon="" ;; - esac - ;; - disconnected) return ;; - *) icon="" ;; - esac + if grep -vxq '0x\(1\|9\)003' "$info/flags"; then + icon="" + else + case "$(status "$device")" in + connected) + percent=$(awk -v interface="${device}:" '$1==interface { gsub(/[^0-9]/,"",$3); print $3 }' /proc/net/wireless) + # shellcheck disable=SC2194 + case 1 in + $((percent >= 80))) icon="" ;; + $((percent >= 60))) icon="" ;; + $((percent >= 40))) icon="" ;; + $((percent >= 20))) icon="" ;; + $((percent >= 0))) icon="" ;; + esac + ;; + disconnected) return ;; + *) icon="" ;; + esac - wireless_status="${wireless_status:-}${icon} ${name} " - fi + wireless_status="${wireless_status:-}${icon} ${name} " + fi } ethernet() { - case "$(status "$device")" in - connected) icon="" ;; - unavailable | disconnected) return ;; - *) icon="" ;; - esac + case "$(status "$device")" in + connected) icon="" ;; + unavailable | disconnected) return ;; + *) icon="" ;; + esac - ethernet_status="${ethernet_status:-}${icon} " + ethernet_status="${ethernet_status:-}${icon} " } wireguard() { - wireguard_status="${wireguard_status:-} ${name} " + wireguard_status="${wireguard_status:-} ${name} " } vpn() { - vpn_status="${vpn_status:-} ${name} " + vpn_status="${vpn_status:-} ${name} " } fallback() { - : + : } output() { - printf "%b" "${wireless_status}${ethernet_status}${wireguard_status}${vpn_status}" | sed 's/ $//' + printf "%b" "${wireless_status}${ethernet_status}${wireguard_status}${vpn_status}" | sed 's/ $//' } IFS=" " for x in $(nmcli --terse connection show --active); do - name="$(echo "$x" | cut -d: -f1)" - type="$(echo "$x" | cut -d: -f3)" - device="$(echo "$x" | cut -d: -f4)" - info="/sys/class/net/${device}" + name="$(echo "$x" | cut -d: -f1)" + type="$(echo "$x" | cut -d: -f3)" + device="$(echo "$x" | cut -d: -f4)" + info="/sys/class/net/${device}" - case "$type" in - *wireless) wireless ;; - *ethernet) ethernet ;; - wireguard | vpn) $type ;; - *) fallback ;; - esac + case "$type" in + *wireless) wireless ;; + *ethernet) ethernet ;; + wireguard | vpn) $type ;; + *) fallback ;; + esac - unset name type device info icon + unset name type device info icon done output diff --git a/.local/libexec/statusbar/sb-ip b/.local/libexec/statusbar/sb-ip index 4f9aa090..181ab4d4 100755 --- a/.local/libexec/statusbar/sb-ip +++ b/.local/libexec/statusbar/sb-ip @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb cache="${XDG_CACHE_HOME:-$HOME/.cache}/geoip" @@ -8,8 +8,8 @@ pub_addr="$(curl -s ipv4.icanhazip.com 2>/dev/null)" location="$(awk -F: -v ip="$pub_addr" '$1==ip {print $2}' "$cache")" [ -z "$location" ] && { - location="$(curl -s "https://reallyfreegeoip.org/json/${pub_addr}" | jq -r '.country_name')" - echo "${pub_addr}:${location}" >>"$cache" + location="$(curl -s "https://reallyfreegeoip.org/json/${pub_addr}" | jq -r '.country_name')" + echo "${pub_addr}:${location}" >>"$cache" } flag="$(awk -v loc="$location" '$0 ~ loc {print $1}' "${XDG_DATA_HOME:-$HOME/.local/share}/emoji")" diff --git a/.local/libexec/statusbar/sb-memory b/.local/libexec/statusbar/sb-memory index fcc23165..a0e01336 100755 --- a/.local/libexec/statusbar/sb-memory +++ b/.local/libexec/statusbar/sb-memory @@ -1,14 +1,7 @@ #!/bin/sh -. "colors" +. libsb -case "$(free -m | awk 'NR==2 { printf "%d", ($3 / $2) * 10 }')" in - 0 | 1) color="$fggreen" bar="▁" ;; - 2) color="$fggreen" bar="▂" ;; - 3 | 4) color="$fggreen" bar="▃" ;; - 5) color="$fgorange" bar="▄" ;; - 6 | 7) color="$fgorange" bar="▅" ;; - 8) color="$fgred" bar="▆" ;; - *) color="$fgred" bar="▇" ;; -esac +percent="$(free -m | awk 'NR==2 { printf "%d", ($3 / $2) * 100 }')" +meter=$(meter_bar "$percent") -printf " <span %s %s>%s</span>" "${bggray}" "${color}" "$bar" +printf " <span %s %s>%s</span>" "$(echo "$meter" | cut -d: -f1)" "${bggray}" "$(echo "$meter" | cut -d: -f2)" diff --git a/.local/libexec/statusbar/sb-music b/.local/libexec/statusbar/sb-music index 66cd4f71..63d8021d 100755 --- a/.local/libexec/statusbar/sb-music +++ b/.local/libexec/statusbar/sb-music @@ -1,17 +1,17 @@ #!/bin/sh -. "colors" +. libsb filter() { - if mpc 1>/dev/null 2>&1 && [ "$(mpc | wc -l)" -eq 3 ]; then - song=$(mpc current -f "%artist% - %title%" | xargs -0) - [ ${#song} -gt 35 ] && song="$(printf %.35s "$song")…" - mpc | awk 'NR==2' | grep -q playing && icon="" || icon="" - echo "$icon $song" - elif mpc 1>/dev/null 2>&1 && [ "$(mpc | wc -l)" -eq 1 ]; then - echo "" - else - exit 1 - fi + if mpc 1>/dev/null 2>&1 && [ "$(mpc | wc -l)" -eq 3 ]; then + song=$(mpc current -f "%artist% - %title%" | xargs -0) + [ ${#song} -gt 35 ] && song="$(printf %.35s "$song")…" + mpc | awk 'NR==2' | grep -q playing && icon="" || icon="" + echo "$icon $song" + elif mpc 1>/dev/null 2>&1 && [ "$(mpc | wc -l)" -eq 1 ]; then + echo "" + else + exit 1 + fi } pidof -x sbd-music >/dev/null 2>&1 || "$HOME/.local/libexec/daemons/sbd-music" >/dev/null 2>&1 & filter diff --git a/.local/libexec/statusbar/sb-nettraf b/.local/libexec/statusbar/sb-nettraf index 3ea644c2..0efd3c10 100755 --- a/.local/libexec/statusbar/sb-nettraf +++ b/.local/libexec/statusbar/sb-nettraf @@ -1,17 +1,17 @@ #!/bin/sh -. "colors" +. libsb # TODO: Coloring based on speed update() { - sum=0 - for arg; do - read -r i <"$arg" - sum=$((sum + i)) - done - cache=$XDG_RUNTIME_DIR/${1##*/} - [ -f "$cache" ] && read -r old <"$cache" || old=0 - printf "%b" "$sum\n" >"$cache" - printf "%b" "$((sum - old))\n" + sum=0 + for arg; do + read -r i <"$arg" + sum=$((sum + i)) + done + cache=$XDG_RUNTIME_DIR/${1##*/} + [ -f "$cache" ] && read -r old <"$cache" || old=0 + printf "%b" "$sum\n" >"$cache" + printf "%b" "$((sum - old))\n" } rx=$(update /sys/class/net/[ew]*/statistics/rx_bytes) diff --git a/.local/libexec/statusbar/sb-packages b/.local/libexec/statusbar/sb-packages index f12bbaf9..259c492e 100755 --- a/.local/libexec/statusbar/sb-packages +++ b/.local/libexec/statusbar/sb-packages @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb icon="" diff --git a/.local/libexec/statusbar/sb-playerctl b/.local/libexec/statusbar/sb-playerctl index dd3d2fb2..7cfadcb2 100755 --- a/.local/libexec/statusbar/sb-playerctl +++ b/.local/libexec/statusbar/sb-playerctl @@ -1,5 +1,5 @@ #!/bin/sh -. "colors" +. libsb pidof -x sbd-playerctl >/dev/null 2>&1 || "$HOME/.local/libexec/daemons/sbd-playerctl" >/dev/null 2>&1 & [ "$(playerctl status 2>&1)" = "No players found" ] && printf "%b" "" && exit 1