diff options
Diffstat (limited to '.config/sway/status.sh')
| -rwxr-xr-x | .config/sway/status.sh | 99 |
1 files changed, 66 insertions, 33 deletions
diff --git a/.config/sway/status.sh b/.config/sway/status.sh index 2b8d43c..ef52efd 100755 --- a/.config/sway/status.sh +++ b/.config/sway/status.sh @@ -1,39 +1,72 @@ -# The Sway configuration file in ~/.config/sway/config calls this script. -# You should see changes to the status bar after saving this script. -# If not, do "killall swaybar" and $mod+Shift+c to reload the configuration. +#!/bin/sh + +CACHE="$HOME/.cache/sway-status" +mkdir -p "$CACHE" + +BAT_CACHE="$CACHE/battery" +NET_CACHE="$CACHE/network" +SYS_CACHE="$CACHE/system" + +now=$(date +%s) + +############################################ +# CLOCK (unchanged) +############################################ -# The abbreviated weekday (e.g., "Sat"), followed by the ISO-formatted date -# like 2018-10-06 and the time (e.g., 14:01). Check `man date` on how to format -# time and date. date_formatted=$(date "+%a %F %I:%M") date_prague=$(TZ=Europe/Prague date "+%a %F %I:%M") date_london=$(TZ=Europe/London date "+%a %F %I:%M") -# "upower --enumerate | grep 'BAT'" gets the battery name (e.g., -# "/org/freedesktop/UPower/devices/battery_BAT0") from all power devices. -# "upower --show-info" prints battery information from which we get -# the state (such as "charging" or "fully-charged") and the battery's -# charge percentage. With awk, we cut away the column containing -# identifiers. i3 and sway convert the newline between battery state and -# the charge percentage automatically to a space, producing a result like -# "charging 59%" or "fully-charged 100%". -battery_info=$(upower --show-info $(upower --enumerate |\ -grep 'BAT') |\ -egrep "state|percentage" |\ -awk '{print $2}') - -# Network -ip_addr=$(ip route get 1.1.1.1 | head -1 | cut -d' ' -f7) -network=$(ip route get 1.1.1.1 | grep -Po '(?<=dev\s)\w+' | cut -f1 -d ' ') -# interface_easyname grabs the "old" interface name before systemd renamed it -#interface_easyname=$(dmesg | grep $network | grep renamed | awk 'NF>1{print $NF}') -#ping=$(ping -c 1 www.google.es | tail -1| awk '{print $4}' | cut -d '/' -f 2 | cut -d '.' -f 1) - -# CPU temp -cpu_temp=$(sensors | grep -oP 'CPU.*?\+\K[0-9.]+') - -# RAM usage -ram_usage=$(free -h | awk '/^Mem:/ {print "RAM Usage: " $3 " / " $2}') - -# Additional emojis and characters for the status bar: +############################################ +# BATTERY (cache 30 sec) +############################################ + +if [ ! -f "$BAT_CACHE.ts" ] || [ $((now - $(cat "$BAT_CACHE.ts" 2>/dev/null))) -gt 30 ]; then + battery_info=$(upower --show-info \ + $(upower --enumerate | grep BAT) | + egrep "state|percentage" | + awk '{print $2}' | tr '\n' ' ') + + echo "$battery_info" > "$BAT_CACHE" + echo "$now" > "$BAT_CACHE.ts" +fi + +battery_info=$(cat "$BAT_CACHE") + +############################################ +# NETWORK (cache 15 sec) +############################################ + +if [ ! -f "$NET_CACHE.ts" ] || [ $((now - $(cat "$NET_CACHE.ts" 2>/dev/null))) -gt 15 ]; then + + route=$(ip route get 1.1.1.1 2>/dev/null) + + ip_addr=$(printf "%s" "$route" | awk '{print $7; exit}') + network=$(printf "%s" "$route" | grep -Po '(?<=dev\s)\w+' | cut -f1 -d ' ') + + echo "$network $ip_addr" > "$NET_CACHE" + echo "$now" > "$NET_CACHE.ts" +fi + +read network ip_addr < "$NET_CACHE" + +############################################ +# CPU TEMP + RAM (cache 10 sec) +############################################ + +if [ ! -f "$SYS_CACHE.ts" ] || [ $((now - $(cat "$SYS_CACHE.ts" 2>/dev/null))) -gt 10 ]; then + + cpu_temp=$(sensors | grep -oP 'CPU.*?\+\K[0-9.]+' | head -1) + ram_usage=$(free -h | awk '/^Mem:/ {print "RAM Usage: " $3 " / " $2}') + + echo "$cpu_temp|$ram_usage" > "$SYS_CACHE" + echo "$now" > "$SYS_CACHE.ts" +fi + +IFS="|" read cpu_temp ram_usage < "$SYS_CACHE" + +############################################ +# OUTPUT +############################################ + echo $network $ip_addr \| CPU: $cpu_temp \| $ram_usage \| $battery_info \| LON: $date_london \| PRG: $date_prague \| $date_formatted |
