(xmobar): add various monitors, and create a now playing pipe to be read from mpc

This commit is contained in:
frosty 2024-07-14 16:37:37 -04:00
parent 98846e4012
commit d3d4a9c070
3 changed files with 239 additions and 59 deletions

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
PIPES="volume" PIPES="volume now-playing"
for pipe in $PIPES; do for pipe in $PIPES; do
echo "" >"/tmp/pipe-$pipe" echo "" >"/tmp/pipe-$pipe"

View file

@ -1,130 +1,304 @@
#!/bin/sh #!/bin/sh
STATUS_WELL_COLOR="#31f53e"
STATUS_OK_COLOR="#f58c31"
STATUS_UNWELL_COLOR="#f53131"
BATTERY_NAME="BAT1"
BATTERY_FULL_THRESHOLD=80 BATTERY_FULL_THRESHOLD=80
BATTERY_FULL_COLOR="#31f53e"
WIFI_ADAPTER="wlan0" WIFI_ADAPTER="wlan0"
DISK_ICON_COLOR="#48a3e8"
NOW_PLAYING_COLOR="#e647a1"
NOW_PLAYING_STATE_COLOR="#ccca4e"
readable_kib() { readable_kib() {
if [ "$1" -lt 1024 ]; then data="${1:-}"
printf "%.1fK\n" "$1" decimals="${2:-}"
elif [ "$1" -lt 1048576 ]; then [ -z "$data" ] || [ -z "$decimals" ] && exit
printf "%.1fM\n" $(($1 / 1024))
if [ "$data" -lt 1024 ]; then
amt="$1"
letter="K"
elif [ "$data" -lt 1048576 ]; then
amt="$(($1 / 1024))"
letter="M"
else else
printf "%.1fG\n" $(($1 / 1048576)) amt="$(($1 / 1048576))"
letter="G"
fi fi
printf "%.*f%s\n" "$decimals" "$amt" "$letter"
}
replace_substring() {
original="${1:-}"
pattern="${2:-}"
replacement="${3:-}"
[ -z "$original" ] || [ -z "$pattern" ] && exit
new="$original"
while :; do
case "$original" in
*"$pattern"*)
before_pattern="${original%%"$pattern"*}"
after_pattern="${original#*"$pattern"}"
new="$before_pattern$replacement$after_pattern"
;;
*) break ;;
esac
done
printf '%s\n' "$new"
} }
monitor_battery() { monitor_battery() {
read -r capacity </sys/class/power_supply/BAT1/capacity read -r capacity </sys/class/power_supply/$BATTERY_NAME/capacity
read -r status </sys/class/power_supply/BAT1/status read -r status </sys/class/power_supply/$BATTERY_NAME/status
if [ "$capacity" -ge "$BATTERY_FULL_THRESHOLD" ] && [ "$status" = "Not charging" ]; then if [ "$capacity" -ge "$BATTERY_FULL_THRESHOLD" ] && [ "$status" = "Not charging" ]; then
status="Full" status="Full"
fi fi
# TODO(frosty): Add more icons based on other statuses.
case $status in
"Full" | "Charging") icon="" ;;
"Not charging") icon="" ;;
*) icon="" ;;
esac
# TODO(frosty): Check for other statuses, and print a different color.
if [ "$status" = "Full" ]; then if [ "$status" = "Full" ]; then
printf '<fc=%s>%s%%</fc> (%s)\n' "$BATTERY_FULL_COLOR" "$capacity" "$status" printf '%s <fc=%s>%s%%</fc> (%s)\n' "$icon" "$STATUS_WELL_COLOR" "$capacity" "$status"
else else
printf '%s%% (%s)\n' "$capacity" "$status" printf '%s %s%% (%s)\n' "$icon" "$capacity" "$status"
fi fi
} }
monitor_cpu() { monitor_cpu() {
while read -r _ user _ system idle _; do while read -r _ user _ system idle _; do
initial_user_sys=$((user + system)) initial_user_sys="$((user + system))"
initial_total=$((user + system + idle)) initial_total="$((user + system + idle))"
break break
done </proc/stat done </proc/stat
# TODO(frosty): Allow the user to specify how long to wait via an argument.
sleep 1 sleep 1
while read -r _ user _ system idle _; do while read -r _ user _ system idle _; do
current_user_sys=$((user + system)) current_user_sys="$((user + system))"
current_total=$((user + system + idle)) current_total="$((user + system + idle))"
break break
done </proc/stat done </proc/stat
while read -r line; do while read -r line; do
case $line in case "$line" in
*Package\ id\ 0:*) temp=$line ;; *"Package id 0:"*) temp="$line" ;;
esac esac
done <<EOF done <<EOF
$(sensors) $(sensors)
EOF EOF
temp=${temp#Package id 0:*+} if [ -n "$temp" ]; then
temp=${temp%%°*} temp="${temp#Package id 0:*+}"
temp=${temp%.*} temp="${temp%%°*}"
temp="${temp%.*}"
fi
printf '%s°C (%.*f%%)\n' "$temp" 0 $(((current_user_sys - initial_user_sys) * 100 / (current_total - initial_total))) usage_percent="$(printf '%i\n' $(((current_user_sys - initial_user_sys) * 100 / (current_total - initial_total))))"
case "$usage_percent" in
[0-9] | [0-3][0-9]) color="$STATUS_WELL_COLOR" ;;
[4-5][0-9]) color="$STATUS_OK_COLOR" ;;
*) color="$STATUS_UNWELL_COLOR" ;;
esac
if [ -n "$temp" ]; then
printf '%s°C (<fc=%s>%i%%</fc>)\n' "$temp" "$color" "$usage_percent"
else
printf '<fc=%s>%i%%</fc>\n' "$color" "$usage_percent"
fi
} }
monitor_load() { monitor_load() {
while read -r one five fifteen _; do while read -r one five fifteen _; do
load_one=$one load_one="$one"
load_five=$five load_five="$five"
load_fifteen=$fifteen load_fifteen="$fifteen"
done </proc/loadavg done </proc/loadavg
printf '%s %s %s\n' "$load_one" "$load_five" "$load_fifteen" # TODO(frosty): Find a less cursed way to achieve this.
load_one_color=
load_five_color=
load_fifteen_color=
for load in load_one load_five load_fifteen; do
case "$(eval 'printf "%i\n" "\$$load"')" in
[0-3]) eval "${load}_color=#cccccc" ;;
[4-7]) eval "${load}_color=$STATUS_OK_COLOR" ;;
*) eval "${load}_color=$STATUS_UNWELL_COLOR" ;;
esac
done
printf '<fc=%s>%s</fc> <fc=%s>%s</fc> <fc=%s>%s</fc>\n' "$load_one_color" "$load_one" "$load_five_color" "$load_five" "$load_fifteen_color" "$load_fifteen"
} }
monitor_memory() { monitor_memory() {
while read -r label size _; do while read -r label size _; do
case $label in case "$label" in
MemTotal:) total_mem=$size ;; "MemTotal:") total_mem="$size" ;;
MemAvailable:) free_mem=$size ;; "MemAvailable:") free_mem="$size" ;;
esac esac
done </proc/meminfo done </proc/meminfo
used_mem=$((total_mem - free_mem)) used_mem="$((total_mem - free_mem))"
usage_percent="$((used_mem * 100 / total_mem))"
printf '%s (%s%%)\n' "$(readable_kib "$used_mem")" $((used_mem * 100 / total_mem)) # TODO(frosty): Add other colors for usage percentages.
if [ "$usage_percent" -lt 50 ]; then
printf '%s (<fc=%s>%s%%</fc>)\n' "$(readable_kib "$used_mem" 1)" "$STATUS_WELL_COLOR" "$usage_percent"
else
printf '%s (%s%%)\n' "$(readable_kib "$used_mem" 1)" "$usage_percent"
fi
} }
monitor_network() { monitor_ssid() {
address=$(ip route get 1)
address=${address#* via * dev * src }
address=${address% uid *}
ssid=
while read -r line; do while read -r line; do
case $line in case $line in
*Connected\ network*) *"Connected network"*) ssid="$line" ;;
ssid=${line##*Connected network}
ssid="${ssid#"${ssid%%[![:space:]]*}"}"
;;
esac esac
done <<EOF done <<EOF
$(iwctl station "$WIFI_ADAPTER" show) $(iwctl station "$WIFI_ADAPTER" show)
EOF EOF
[ -z "$ssid" ] && exit
ssid="${ssid##*Connected network}"
ssid="${ssid#"${ssid%%[![:space:]]*}"}"
printf '<fc=#fc7932>%s</fc> @ %s\n' "$address" "$ssid" # TODO(frosty): Add other icons based on the connection status.
icon=""
printf '%s %s\n' "$icon" "$ssid"
}
monitor_local_ip() {
# TODO(frosty): Add a fallback value when offline.
address="$(ip route get 1)"
address="${address#* via * dev * src }"
address="${address% uid *}"
printf '%s\n' "$address"
} }
monitor_volume() { monitor_volume() {
volume=$(wpctl get-volume @DEFAULT_AUDIO_SINK@) volume="$(wpctl get-volume @DEFAULT_AUDIO_SINK@)"
volume=${volume% \[MUTED\]} volume="${volume% \[MUTED\]}"
volume=${volume#Volume: } volume="${volume#Volume: }"
volume=${volume%.*}${volume#*.} volume="${volume%.*}${volume#*.}"
volume=${volume#0} volume="${volume#0}"
printf '%s%%\n' "$volume" # TODO(frosty): Add other icons based on the current device (headphones, speakers, etc.).
icon=""
printf '%s %s%%\n' "$icon" "$volume"
} }
monitor_uptime() { monitor_uptime() {
up=$(uptime -p) # TODO(frosty): Find the uptime without calling another command.
up=${up#up } up="$(uptime -p)"
up=${up//,} up="$(replace_substring "$up" 'up ' '')"
up=${up// day/d} up="$(replace_substring "$up" ',' '')"
up=${up// days/d} up="$(replace_substring "$up" ' days' 'd')"
up=${up// minutes/m} up="$(replace_substring "$up" ' day' 'd')"
up=${up// minute/m} up="$(replace_substring "$up" ' hours' 'h')"
up=${up// hours/h} up="$(replace_substring "$up" ' hour' 'h')"
up=${up// hour/h} up="$(replace_substring "$up" ' minutes' 'm')"
up="$(replace_substring "$up" ' minute' 'm')"
printf '%s\n' "$up" printf '%s\n' "$up"
} }
monitor_date() {
date '+%-I:%M:%S %p'
}
monitor_disk() {
disk="${2:-}"
use_icon="${3:-0}"
[ -z "$disk" ] || [ ! -d "$disk" ] && exit 1
# TODO(frosty): Allow more icons to be added using an rc file.
if [ "$use_icon" -eq 1 ]; then
case "$disk" in
"/") icon="" ;;
"/home") icon="" ;;
*) icon="" ;;
esac
else
case "$disk" in
"/home") icon="~" ;;
*) icon="$disk" ;;
esac
fi
i=0
while read -r _ size used _; do
case "$i" in
0) ;;
1)
size_amt="$size"
used_amt="$used"
;;
*) break ;;
esac
i="$((i + 1))"
done <<EOF
$(df "$disk")
EOF
[ -z "$size_amt" ] || [ -z "$used_amt" ] && exit 1
printf '<fc=%s>%s</fc> %s/%s (<fc=%s>%s%%</fc>)\n' "$DISK_ICON_COLOR" "$icon" "$(readable_kib "$used_amt" 0)" "$(readable_kib "$size_amt" 0)" "$STATUS_WELL_COLOR" "$((used_amt * 100 / size_amt))"
}
monitor_now_playing() {
mpc_output="$(mpc)"
while read -r line; do
now_playing="$line"
break
done <<EOF
$mpc_output
EOF
i=0
while read -r state _; do
case "$i" in
0) ;;
1)
case "$state" in
\[*\]) play_state="$state" ;;
esac
;;
*) break ;;
esac
i="$((i + 1))"
done <<EOF
$mpc_output
EOF
if [ -z "$now_playing" ] || [ -z "$play_state" ]; then
printf 'N/A\n'
exit
fi
play_state="${play_state#\[}"
play_state="${play_state%\]}"
# TODO(frosty): Split the artist and track names, and truncate each one separately.
if [ "${#now_playing}" -gt 47 ]; then
now_playing="${now_playing%"${now_playing#??????????????????????????????????????????????}"}..."
fi
printf '<fc=%s>%s</fc> <fc=%s>[%s]</fc>\n' "$NOW_PLAYING_COLOR" "$now_playing" "$NOW_PLAYING_STATE_COLOR" "$play_state"
}
if command -v monitor_"$1" >/dev/null 2>&1; then if command -v monitor_"$1" >/dev/null 2>&1; then
monitor_"$1" "monitor_$1" "$@"
else
exit 1
fi fi

6
.local/bin/pipe_now-playing Executable file
View file

@ -0,0 +1,6 @@
#!/bin/sh
PIPE_FILE="/tmp/pipe-now-playing"
[ -p "$PIPE_FILE" ] || mkfifo "$PIPE_FILE"
monitors now_playing >"$PIPE_FILE"