dwm_update_status.sh (5983B)
1#!/bin/bash 2 3### Constants 4PROC_UPTIME='/proc/uptime' 5PROC_STAT='/proc/stat' 6PROC_MEMINFO='/proc/meminfo' 7#SYS_BAT_CAPACITY='/sys/class/power_supply/sbs-20-000b/capacity' 8#SYS_BAT_STATUS='/sys/class/power_supply/sbs-20-000b/status' 9#SYS_BAT_2EMPTY='/sys/class/power_supply/sbs-20-000b/time_to_empty_avg' 10#SYS_BAT_2FULL='/sys/class/power_supply/sbs-20-000b/time_to_full_avg' 11SYS_BRIGHTNESS='/sys/class/backlight/backlight/brightness' 12SYS_MAX_BRIGHTNESS='/sys/class/backlight/backlight/max_brightness' 13SYS_TEMPERATURE='/sys/class/thermal/thermal_zone3/temp' 14PROC_WIRELESS='/proc/net/wireless' 15#PROC_DPMS='/sys/devices/platform/display-subsystem/drm/card0/card0-eDP-1/dpms' 16PROC_DPMS='/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/dpms' 17PREV_TOTAL=0 18PREV_IDLE=0 19 20### Functions 21function sec2time () { 22 local T=$1 23 local D=$((T/60/60/24)) 24 local H=$((T/60/60%24)) 25 local M=$((T/60%60)) 26 local S=$((T%60)) 27 #[[ $D > 0 ]] && printf '%d days ' $D 28 #[[ $H > 0 ]] && printf '%d hours ' $H 29 #[[ $M > 0 ]] && printf '%d minutes ' $M 30 #![[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' 31 #printf '%d second\n' $S 32 33 [[ $D > 0 ]] && printf '%d days ' $D 34 [[ $H > 0 ]] && printf '%02d:' $H 35 [[ $M > 0 ]] && printf '%02d:' $M 36 #[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' 37 #printf '%d second\n' $S 38 printf '%02d\n' $S 39} 40function get-date () { 41 printf " $(date +'%F %T')" 42} 43function get-uptime () { 44 printf " [$(sec2time $(cat $PROC_UPTIME | cut -d'.' -f1))]" 45} 46#function get-kbd () { 47 #printf " $(setxkbmap -query | grep variant | colrm 1 12)" 48#} 49function get-cpu-average () { 50 printf " $(grep 'cpu ' $PROC_STAT | awk '{usage=(1000*($2+$4)/($2+$4+$5)+5)/10} END {print int(usage) "%%"}')" 51} 52function get-cpu-current () { 53 # Get the total CPU statistics, discarding the 'cpu ' prefix. 54 #CPU=(`sed -n 's/^cpu\s//p' /proc/stat`) 55 CPU=(`grep -oP '(?<=cpu ).*' $PROC_STAT`) 56 IDLE=${CPU[3]} # Just the idle CPU time. 57 # Calculate the total CPU time. 58 TOTAL=0 59 for VALUE in "${CPU[@]}"; do 60 let "TOTAL=$TOTAL+$VALUE" 61 done 62 # Calculate the CPU usage since we last checked. 63 let "DIFF_IDLE=$IDLE-$PREV_IDLE" 64 let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" 65 let "DIFF_USAGE=(100000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10" 66 #printf "%0.2f%%\\n" "${DIFF_USAGE}e-2" 67 # Remember the total and idle CPU times for the next check. 68 PREV_TOTAL="$TOTAL" 69 PREV_IDLE="$IDLE" 70 71 printf "%0.2f%%" "${DIFF_USAGE}e-2" 72} 73function get-ram () { 74 while IFS=":" read -r a b; do 75 case "$a" in 76 "MemTotal") mem_used="$((mem_used+=${b/kB}))"; mem_total="${b/kB}" ;; 77 "Shmem") mem_used="$((mem_used+=${b/kB}))" ;; 78 "MemFree" | "Buffers" | "Cached" | "SReclaimable") 79 mem_used="$((mem_used-=${b/kB}))" 80 ;; 81 esac 82 done < $PROC_MEMINFO 83 mem_used="$((mem_used / 1024))" 84 mem_total="$((mem_total / 1024))" 85 memory="${mem_used}${mem_label:-MiB} / ${mem_total}${mem_label:-MiB}" 86 #printf " $(bc <<<"scale=1;$mem_used/$mem_total*100" | sed -e 's/^\./0./')%%" 87 let "mem_usage=(100000*$mem_used/$mem_total+5)/10" 88 printf " %0.2f%%" "${mem_usage}e-2" 89} 90function get-storage () { 91 printf " $(df / | grep -v '^Filesystem' | awk '{ print $5 }')%" 92} 93function get-battery () { 94 BAT_PERCENT="$(<$SYS_BAT_CAPACITY)%%" 95 96 if [[ $(<$SYS_BAT_STATUS) == Discharging ]]; then 97 printf " $BAT_PERCENT [$(sec2time $(<$SYS_BAT_2EMPTY))]" 98 elif [[ $(<$SYS_BAT_STATUS) == Charging ]]; then 99 printf " $BAT_PERCENT $(sec2time $(<$SYS_BAT_2FULL))" 100 elif [[ $(<$SYS_BAT_STATUS) == Full ]]; then 101 printf "$BAT_PERCENT Full" 102 else 103 printf '[?]' 104 fi 105} 106function get-brightness () { 107 printf " $(( $(<$SYS_BRIGHTNESS)*100/$(<$SYS_MAX_BRIGHTNESS) ))%%" 108} 109function get-temperature () { 110 #printf " $(sensors | grep temp1 | awk '{print $2}' | sed 's/+//')" 111 printf " %.1f°C" "$(<$SYS_TEMPERATURE)e-3" 112} 113function get-ip () { 114 printf " $(ip route get 1 2>&1 | awk '{print $7}')" 115} 116function get-wifi () { 117 if [[ $(grep wlan0 $PROC_WIRELESS) ]]; then 118 printf " $(grep wlan0 $PROC_WIRELESS | awk '{ print int($3 * 100 / 70) }')%%" 119 else 120 echo -n ' [?]' 121 fi 122} 123function update-status () { 124 #xsetroot -name " $info_date $info_uptime $info_kbd $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " 125 #xsetroot -name " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " 126 #echo " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " 127 xsetroot -name " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_ip" 128 129} 130 131### start from here 132info_date=$(get-date) 133info_uptime=$(get-uptime) 134#info_kbd=$(get-kbd) 135info_cpu_average=$(get-cpu-average) 136info_cpu_current=$(get-cpu-current) 137info_ram=$(get-ram) 138info_storage=$(get-storage) 139#info_battery=$(get-battery) 140#info_brightness=$(get-brightness) 141#info_temperature=$(get-temperature) 142info_ip=$(get-ip) 143info_wifi=$(get-wifi) 144update-status 145sec=0 146while true; do 147 if [[ $(<$PROC_DPMS) == On ]] 148 then 149 # run commands when it's on 150 #echo On $(get-uptime) && sleep 1 151 if [ "$(($sec % 4))" == "0" ]; then 152 #echo "$(date +%M:%S) 2" 153 info_date=$(get-date) 154 info_uptime=$(get-uptime) 155 info_cpu_current=$(get-cpu-current) 156 info_ram=$(get-ram) 157 info_storage=$(get-storage) 158 #info_battery=$(get-battery) 159 #info_brightness=$(get-brightness) 160 #info_temperature=$(get-temperature) 161 #info_wifi=$(get-wifi) 162 update-status 163 fi 164 165 if [ "$(($sec % 40))" == "0" ]; then 166 info_cpu_average=$(get-cpu-average) 167 info_ip=$(get-ip) 168 update-status 169 170 fi 171 172 #if [ "$(($sec % 32))" == "0" ]; then 173 #info_kbd=$(get-kbd) 174 #update-status 175 #fi 176 177 sleep 2; 178 sec=$(( $sec + 2 )) 179 else 180 echo Off 181 sleep 4 182 fi 183done