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