diff options
Diffstat (limited to 'ar/.local/bin/statusbar')
| -rwxr-xr-x | ar/.local/bin/statusbar/sb-clock | 14 | ||||
| -rwxr-xr-x | ar/.local/bin/statusbar/sb-music | 86 | ||||
| -rwxr-xr-x | ar/.local/bin/statusbar/sb-queues | 4 |
3 files changed, 87 insertions, 17 deletions
diff --git a/ar/.local/bin/statusbar/sb-clock b/ar/.local/bin/statusbar/sb-clock index 6aa1bb4..ddfa28b 100755 --- a/ar/.local/bin/statusbar/sb-clock +++ b/ar/.local/bin/statusbar/sb-clock @@ -5,14 +5,14 @@ calendar="🗓" hour=$(date '+%I') minute=$(date '+%M') -# Cache crontab check for 5 minutes -crontab_cache="${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar/has_crontab" +# Cache cron daemon check for 5 minutes +crontab_cache="${XDG_CACHE_HOME:-${HOME}/.cache}/crontab/cronjobs" if [ ! -f "$crontab_cache" ] || [ $(($(date +%s) - $(stat -c %Y "$crontab_cache" 2>/dev/null || echo 0))) -gt 300 ]; then - mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar" - if crontab -l 2>/dev/null | grep -q '^[^#[:space:]]'; then - echo "1" > "$crontab_cache" + mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/crontab" + if crontab -l 2>/dev/null | grep -qv '^#\|^$'; then + echo "1" >"$crontab_cache" else - echo "0" > "$crontab_cache" + echo "0" >"$crontab_cache" fi fi @@ -60,7 +60,7 @@ moonfile="${XDG_DATA_HOME:-${HOME}/.local/share}/wallpapers/moonphase" # Cache location for 24 hours if [ ! -s "$locationfile" ] || [ $(($(date +%s) - $(stat -c %Y "$locationfile" 2>/dev/null || echo 0))) -gt 86400 ]; then mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar" - curl -s --max-time 2 http://ip-api.com/json | jq -r '[.regionName, .countryCode] | join(",")' > "$locationfile" 2>/dev/null & + curl -s --max-time 2 http://ip-api.com/json | jq -r '[.regionName, .countryCode] | join(",")' >"$locationfile" 2>/dev/null & fi location=$(cat "$locationfile" 2>/dev/null || echo "") diff --git a/ar/.local/bin/statusbar/sb-music b/ar/.local/bin/statusbar/sb-music index 5bcc209..36f9101 100755 --- a/ar/.local/bin/statusbar/sb-music +++ b/ar/.local/bin/statusbar/sb-music @@ -1,13 +1,16 @@ #!/bin/bash +shopt -s extglob + MPD_HOST="${MPD_HOST:-/tmp/mpd_socket}" export MPD_HOST truncate_string() { - input="$1" - max_length="$2" + local input="$1" + local max_length="$2" if [ "${#input}" -gt "$max_length" ]; then - echo "${input:0:$((max_length - 2))}.." + local truncated="${input:0:$((max_length - 2))}" + echo "${truncated%%+( )}.." else echo "$input" fi @@ -17,18 +20,23 @@ signal="$(awk -F', *' -v name="${0##*/}" '$2 ~ name {print $3+0}' "${XDG_SOURCES [ "$signal" -eq 0 ] && { pidof -x sb-mpdup >/dev/null 2>&1 || sb-mpdup >/dev/null 2>&1 & + pidof -x sb-playerctlup >/dev/null 2>&1 || sb-playerctlup >/dev/null 2>&1 & } +screen_width=$(xrandr | awk '/\*/ {split($1, res, "x"); print res[1]; exit}') +if [ "$screen_width" -le 2048 ]; then + length="$(grep 'MAX_BLOCK_OUTPUT_LENGTH' ~/.local/src/suckless/dwmblocks/config.def.h | awk '{print $3}')" + max_length="${length:+$((length / 3))}" + max_length="${max_length:-15}" +fi + status() { if ps -C mpd >/dev/null 2>&1; then - screen_width=$(xrandr | awk '/\*/ {split($1, res, "x"); print res[1]; exit}') + local artist title prefix indicators current_time total_time artist=$(mpc current -f %artist%) title=$(mpc current -f %title%) if [ "$screen_width" -le 2048 ]; then - length="$(grep 'MAX_BLOCK_OUTPUT_LENGTH' ~/.local/src/suckless/dwmblocks/config.def.h | awk '{print $3}')" - max_length="${length:+$((length / 3))}" - max_length="${max_length:-15}" artist=$(truncate_string "$artist" "$max_length") title=$(truncate_string "$title" "$max_length") fi @@ -58,7 +66,69 @@ status() { fi } -mpc status %state% | grep -q "stopped" || status +playerctl_status() { + command -v playerctl >/dev/null 2>&1 || return + + local output="" player + while IFS= read -r player; do + local player_status + player_status=$(playerctl --player="$player" status 2>/dev/null) || continue + + local play_icon + case "${player%%.*}" in + spotify | rhythmbox | audacious | clementine | strawberry) play_icon="🎵" ;; + vlc | mpv | totem | celluloid) play_icon="🎬" ;; + firefox | chromium | chrome) play_icon="📺" ;; + *) play_icon="▶️" ;; + esac + + local prefix + case "$player_status" in + "Playing") prefix="$play_icon" ;; + "Paused") prefix="⏸" ;; + *) continue ;; + esac + + if [[ "$player" == chromium.instance* ]]; then + local pid proc_name + pid="${player#chromium.instance}" + proc_name=$(ps -p "$pid" -o comm= 2>/dev/null) + [ "$proc_name" = "youtube-music" ] || continue + else + continue + fi + + local artist title + artist=$(playerctl --player="$player" metadata artist 2>/dev/null) + title=$(playerctl --player="$player" metadata title 2>/dev/null) + + [ -z "$artist" ] && [ -z "$title" ] && continue + + if [ -n "$max_length" ]; then + artist=$(truncate_string "$artist" "$max_length") + title=$(truncate_string "$title" "$max_length") + fi + + output="${output:+$output | }${prefix}${artist:+$artist - }${title}" + done < <(playerctl -l 2>/dev/null) + + echo "$output" +} + +mpd_output="" +if ps -C mpd >/dev/null 2>&1 && ! mpc status %state% 2>/dev/null | grep -q "stopped"; then + mpd_output=$(status) +fi + +playerctl_output=$(playerctl_status) + +if [ -n "$mpd_output" ] && [ -n "$playerctl_output" ]; then + echo "$mpd_output $playerctl_output" +elif [ -n "$mpd_output" ]; then + echo "$mpd_output" +elif [ -n "$playerctl_output" ]; then + echo "$playerctl_output" +fi case $BLOCK_BUTTON in 1) setsid -f "$TERMINAL" -e ncmpcpp ;; # left click, opens ncmpcpp diff --git a/ar/.local/bin/statusbar/sb-queues b/ar/.local/bin/statusbar/sb-queues index 477fa7f..ac7cb55 100755 --- a/ar/.local/bin/statusbar/sb-queues +++ b/ar/.local/bin/statusbar/sb-queues @@ -22,7 +22,7 @@ EOF } # This block displays the number of running and queued background tasks. Requires tsp. -num=$(tsp -l | awk -v numr=0 -v numq=0 '{if (!/notify-send/ && /running/) numr++; if (!/notify-send/ && /queued/) numq++} END{print numr"|"numr+numq}') +num=$(tsp -l | awk -v numr=0 -v numq=0 '{if (!/notify-send/ && /running/) numr++; if (!/notify-send/ && /queued/) numq++} END{print numr"/"numr+numq}') # Handle mouse clicks case $BLOCK_BUTTON in @@ -37,4 +37,4 @@ case $BLOCK_BUTTON in 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; esac -[ -f /tmp/qplaylist ] && cat /tmp/qplaylist 2>/dev/null || ([ "$num" != "0|0" ] && echo "🤖$num" || echo "") +[ -f /tmp/qplaylist ] && cat /tmp/qplaylist 2>/dev/null || ([ "$num" != "0/0" ] && echo "🤖$num" || echo "") |
