diff --git a/.local/bin/initialize_pipes b/.local/bin/initialize_pipes index 2b75f7b..597e495 100755 --- a/.local/bin/initialize_pipes +++ b/.local/bin/initialize_pipes @@ -1,6 +1,6 @@ #!/bin/sh -PIPES="volume" +PIPES="volume now-playing" for pipe in $PIPES; do echo "" >"/tmp/pipe-$pipe" diff --git a/.local/bin/monitors b/.local/bin/monitors index 2f3ad1f..b1f956f 100755 --- a/.local/bin/monitors +++ b/.local/bin/monitors @@ -1,130 +1,304 @@ #!/bin/sh +STATUS_WELL_COLOR="#31f53e" +STATUS_OK_COLOR="#f58c31" +STATUS_UNWELL_COLOR="#f53131" + +BATTERY_NAME="BAT1" BATTERY_FULL_THRESHOLD=80 -BATTERY_FULL_COLOR="#31f53e" WIFI_ADAPTER="wlan0" +DISK_ICON_COLOR="#48a3e8" + +NOW_PLAYING_COLOR="#e647a1" +NOW_PLAYING_STATE_COLOR="#ccca4e" + readable_kib() { - if [ "$1" -lt 1024 ]; then - printf "%.1fK\n" "$1" - elif [ "$1" -lt 1048576 ]; then - printf "%.1fM\n" $(($1 / 1024)) + data="${1:-}" + decimals="${2:-}" + [ -z "$data" ] || [ -z "$decimals" ] && exit + + if [ "$data" -lt 1024 ]; then + amt="$1" + letter="K" + elif [ "$data" -lt 1048576 ]; then + amt="$(($1 / 1024))" + letter="M" else - printf "%.1fG\n" $(($1 / 1048576)) + amt="$(($1 / 1048576))" + letter="G" 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() { - read -r capacity %s%% (%s)\n' "$BATTERY_FULL_COLOR" "$capacity" "$status" + printf '%s %s%% (%s)\n' "$icon" "$STATUS_WELL_COLOR" "$capacity" "$status" else - printf '%s%% (%s)\n' "$capacity" "$status" + printf '%s %s%% (%s)\n' "$icon" "$capacity" "$status" fi } monitor_cpu() { while read -r _ user _ system idle _; do - initial_user_sys=$((user + system)) - initial_total=$((user + system + idle)) + initial_user_sys="$((user + system))" + initial_total="$((user + system + idle))" break done %i%%)\n' "$temp" "$color" "$usage_percent" + else + printf '%i%%\n' "$color" "$usage_percent" + fi } monitor_load() { while read -r one five fifteen _; do - load_one=$one - load_five=$five - load_fifteen=$fifteen + load_one="$one" + load_five="$five" + load_fifteen="$fifteen" done %s %s %s\n' "$load_one_color" "$load_one" "$load_five_color" "$load_five" "$load_fifteen_color" "$load_fifteen" } monitor_memory() { while read -r label size _; do - case $label in - MemTotal:) total_mem=$size ;; - MemAvailable:) free_mem=$size ;; + case "$label" in + "MemTotal:") total_mem="$size" ;; + "MemAvailable:") free_mem="$size" ;; esac done %s%%)\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() { - address=$(ip route get 1) - address=${address#* via * dev * src } - address=${address% uid *} - - ssid= +monitor_ssid() { while read -r line; do case $line in - *Connected\ network*) - ssid=${line##*Connected network} - ssid="${ssid#"${ssid%%[![:space:]]*}"}" - ;; + *"Connected network"*) ssid="$line" ;; esac done <%s @ %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() { - volume=$(wpctl get-volume @DEFAULT_AUDIO_SINK@) - volume=${volume% \[MUTED\]} - volume=${volume#Volume: } - volume=${volume%.*}${volume#*.} - volume=${volume#0} + volume="$(wpctl get-volume @DEFAULT_AUDIO_SINK@)" + volume="${volume% \[MUTED\]}" + volume="${volume#Volume: }" + volume="${volume%.*}${volume#*.}" + 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() { - up=$(uptime -p) - up=${up#up } - up=${up//,} - up=${up// day/d} - up=${up// days/d} - up=${up// minutes/m} - up=${up// minute/m} - up=${up// hours/h} - up=${up// hour/h} + # TODO(frosty): Find the uptime without calling another command. + up="$(uptime -p)" + up="$(replace_substring "$up" 'up ' '')" + up="$(replace_substring "$up" ',' '')" + up="$(replace_substring "$up" ' days' 'd')" + up="$(replace_substring "$up" ' day' 'd')" + up="$(replace_substring "$up" ' hours' 'h')" + up="$(replace_substring "$up" ' hour' 'h')" + up="$(replace_substring "$up" ' minutes' 'm')" + up="$(replace_substring "$up" ' minute' 'm')" 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 <%s %s/%s (%s%%)\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 <%s [%s]\n' "$NOW_PLAYING_COLOR" "$now_playing" "$NOW_PLAYING_STATE_COLOR" "$play_state" +} + if command -v monitor_"$1" >/dev/null 2>&1; then - monitor_"$1" + "monitor_$1" "$@" +else + exit 1 fi diff --git a/.local/bin/pipe_now-playing b/.local/bin/pipe_now-playing new file mode 100755 index 0000000..09f95fb --- /dev/null +++ b/.local/bin/pipe_now-playing @@ -0,0 +1,6 @@ +#!/bin/sh + +PIPE_FILE="/tmp/pipe-now-playing" +[ -p "$PIPE_FILE" ] || mkfifo "$PIPE_FILE" + +monitors now_playing >"$PIPE_FILE"