statusbar (4467B)
1 #!/bin/sh 2 # Cada módulo tem seu próprio intervalo. O loop central acorda a cada 1s e só chama xsetroot quando algo mudou. 3 4 BAT_INT=30 5 VOL_INT=2 6 NET_INT=30 7 MAIL_CHECK_INT=5 # Checa o disco para ver se há e-mails não lidos a cada 5s 8 MAIL_SYNC_INT=120 # Baixa e-mails novos da internet (mbsync) a cada 3 minutos 9 10 last_bat=0; last_vol=0; last_net=0; last_mail_check=0; last_mail_sync=0 11 s_bat=""; s_vol=""; s_net=""; s_mail="" 12 old_status="" 13 14 while true; do 15 now=$(date +%s) 16 changed=0 17 18 # ── BATERIA ─────────────────────────────────────────────────────────────── 19 if [ $(( now - last_bat )) -ge $BAT_INT ]; then 20 if [ -d /sys/class/power_supply/BAT0 ]; then 21 cap=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null) 22 status_bat=$(cat /sys/class/power_supply/BAT0/status 2>/dev/null) 23 24 case "$status_bat" in 25 Charging) icon="⚡" ;; 26 *) 27 [ "${cap:-100}" -le 20 ] && icon="🪫" || icon="🔋" 28 ;; 29 esac 30 31 if [ "${cap:-100}" -lt 50 ] || [ "$status_bat" = "Charging" ]; then 32 new_bat="$icon $cap%" 33 else 34 new_bat="" 35 fi 36 else 37 new_bat="" 38 fi 39 40 [ "$new_bat" != "$s_bat" ] && s_bat="$new_bat" && changed=1 41 last_bat=$now 42 fi 43 44 # ── VOLUME ──────────────────────────────────────────────────────────────── 45 if [ $(( now - last_vol )) -ge $VOL_INT ]; then 46 vol_raw=$(pactl get-sink-volume @DEFAULT_SINK@ 2>/dev/null \ 47 | grep -oE '[0-9]+%' | head -n 1 | tr -d '%') 48 muted=$(pactl get-sink-mute @DEFAULT_SINK@ 2>/dev/null \ 49 | grep -c 'yes') 50 51 if [ "$muted" -gt 0 ] || [ "${vol_raw:-0}" -eq 0 ]; then 52 new_vol="🔇 mute" 53 else 54 [ "${vol_raw:-0}" -gt 60 ] && v_icon="🔊" || v_icon="🔉" 55 new_vol="$v_icon $vol_raw%" 56 fi 57 58 [ "$new_vol" != "$s_vol" ] && s_vol="$new_vol" && changed=1 59 last_vol=$now 60 fi 61 62 # ── REDE ────────────────────────────────────────────────────────────────── 63 if [ $(( now - last_net )) -ge $NET_INT ]; then 64 new_net="off" 65 for iface in eth0 wlan0 usb0; do 66 state_file="/sys/class/net/$iface/operstate" 67 if [ -f "$state_file" ]; then 68 state=$(cat "$state_file" 2>/dev/null) 69 [ "$state" = "up" ] && new_net="" && break 70 fi 71 done 72 73 [ "$new_net" != "$s_net" ] && s_net="$new_net" && changed=1 74 last_net=$now 75 fi 76 77 # ── E-MAIL: SINCRONIZAÇÃO (INTERNET) ────────────────────────────────────── 78 if [ $(( now - last_mail_sync )) -ge $MAIL_SYNC_INT ]; then 79 80 mbsync disroot-sync >/dev/null 2>&1 & 81 last_mail_sync=$now 82 fi 83 84 # ── E-MAIL: CONTADOR (DISCO LOCAL) ──────────────────────────────────────── 85 if [ $(( now - last_mail_check )) -ge $MAIL_CHECK_INT ]; then 86 87 unread_count=$(find "$HOME/.local/share/mail/INBOX/new" -type f 2>/dev/null | wc -l) 88 89 if [ "${unread_count:-0}" -gt 0 ]; then 90 new_mail="📩 $unread_count" 91 else 92 new_mail="" 93 fi 94 95 [ "$new_mail" != "$s_mail" ] && s_mail="$new_mail" && changed=1 96 last_mail_check=$now 97 fi 98 99 # ── ATUALIZA O XSETROOT ─────────────────────────────────────────────────── 100 if [ "$changed" -eq 1 ]; then 101 status="" 102 for seg in "$s_mail" "$s_bat" "$s_vol" "$s_net"; do 103 [ -n "$seg" ] || continue 104 [ -n "$status" ] && status="$status | " 105 status="$status$seg" 106 done 107 108 if [ "$status" != "$old_status" ]; then 109 xsetroot -name "$status" 110 old_status="$status" 111 fi 112 fi 113 114 sleep 1 115 done