]> code.delx.au - spectrwm/blob - baraction.sh
Update bar more often and remove ./ from baraction.sh
[spectrwm] / baraction.sh
1 #!/bin/sh
2
3 print_date() {
4 # The date is printed to the status bar by default.
5 # To print the date through this script, set clock_enabled to 0
6 # in scrotwm.conf. Uncomment "print_date" below.
7 FORMAT="%a %b %d %R %Z %Y"
8 DATE=`date "+${FORMAT}"`
9 echo -n "${DATE} "
10 }
11
12 _print_cpu() {
13 typeset -R4 _1=${1} _2=${2} _3=${3} _4=${4} _5=${5}
14 echo -n "CPU:${_1}% User${_2}% Nice${_3}% Sys${_4}% Int${_5}% Idle "
15 }
16
17 print_cpu() {
18 OUT=""
19 # iostat prints each column justified to 3 chars, so if one counter
20 # is 100, it jams up agains the preceeding one. sort this out.
21 while [ "${1}x" != "x" ]; do
22 if [ ${1} -gt 99 ]; then
23 OUT="$OUT ${1%100} 100"
24 else
25 OUT="$OUT ${1}"
26 fi
27 shift;
28 done
29 _print_cpu $OUT
30 }
31
32 print_apm() {
33 BAT_STATUS=$1
34 BAT_LEVEL=$2
35 AC_STATUS=$3
36
37 if [ $AC_STATUS -ne 255 -o $BAT_STATUS -lt 4 ]; then
38 if [ $AC_STATUS -eq 0 ]; then
39 echo -n "on battery (${BAT_LEVEL}%)"
40 else
41 case $AC_STATUS in
42 1)
43 AC_STRING="on AC: "
44 ;;
45 2)
46 AC_STRING="on backup AC: "
47 ;;
48 *)
49 AC_STRING=""
50 ;;
51 esac;
52 case $BAT_STATUS in
53 4)
54 BAT_STRING="(no battery)"
55 ;;
56 [0-3])
57 BAT_STRING="(battery ${BAT_LEVEL}%)"
58 ;;
59 *)
60 BAT_STRING="(battery unknown)"
61 ;;
62 esac;
63
64 FULL="${AC_STRING}${BAT_STRING}"
65 if [ "$FULL" != "" ]; then
66 echo -n "$FULL"
67 fi
68 fi
69 fi
70 }
71
72 while :; do
73 # instead of sleeping, use iostat as the update timer.
74 # cache the output of apm(8), no need to call that every second.
75 /usr/sbin/iostat -C -c 3600 |& # wish infinity was an option
76 APM_DATA=""
77 I=0
78 while read -p; do
79 if [ $(( ${I} % 1 )) -eq 0 ]; then
80 APM_DATA=`/usr/sbin/apm -alb`
81 fi
82 if [ $I -gt 2 ]; then
83 # print_date
84 print_cpu $REPLY
85 print_apm $APM_DATA
86 echo ""
87 fi
88 I=$(( ${I} + 1 ));
89 done
90 done