Clarify comment
[khatus.git] / bin / khatus_loop
index f707a00..99e1628 100755 (executable)
@@ -2,6 +2,27 @@
 
 set -e
 
+produce_energy() {
+    upower -e \
+    | grep battery \
+    | xargs upower -i \
+    | awk '
+        /^ +percentage: +/ { percentage=$2 }
+        /^ +state: +/      { state=$2 }
+        END                { print(state, percentage) }
+        '
+}
+
+
+produce_memory() {
+    free | awk '$1 == "Mem:" {print $2, $3}'
+}
+
+produce_fan() {
+    fan_path="$1"
+    cat "$fan_path"
+}
+
 produce_temperature() {
     thermal_zone="$1"
     cat "/sys/class/thermal/thermal_zone${thermal_zone}/temp"
@@ -274,6 +295,38 @@ consume() {
         -v opt_mpd_song_max_chars=10 \
         -v opt_prefixes_of_net_interfaces_to_show="$prefixes_of_net_interfaces_to_show" \
         '
+            /^in:ENERGY/\
+            {
+                split_msg_parts()
+                db["energy_state"]      = $1
+                db["energy_percentage"] = $2
+            }
+
+            /^in:MEMORY/\
+            {
+                split_msg_parts()
+                db["memory_total"] = $1
+                db["memory_used"]  = $2
+            }
+
+            /^in:FAN +status:/\
+            {
+                split_msg_parts()
+                db["fan_status"] = $2
+            }
+
+            /^in:FAN +speed:/\
+            {
+                split_msg_parts()
+                db["fan_speed"] = $2
+            }
+
+            /^in:FAN +level:/\
+            {
+                split_msg_parts()
+                db["fan_level"] = $2
+            }
+
             /^in:TEMPERATURE/\
             {
                 split_msg_parts()
@@ -424,6 +477,8 @@ consume() {
             }
 
             function make_bar(    position, bar, sep, i, j) {
+                position[++i] = make_status_energy()
+                position[++i] = make_status_mem()
                 position[++i] = make_status_cpu()
                 position[++i] = make_status_disk()
                 position[++i] = make_status_net()
@@ -442,10 +497,36 @@ consume() {
                 return bar
             }
 
-            function make_status_cpu(    load, temp) {
+            function make_status_energy(    state, direction_of_change) {
+                state = db["energy_state"]
+                if (state == "discharging") {
+                    direction_of_change = "<"
+                } else if (state == "charging") {
+                    direction_of_change = ">"
+                } else {
+                    direction_of_change = "="
+                };
+                printf("E%s%s", direction_of_change, db["energy_percentage"])
+            }
+
+            function make_status_mem(    total, used, percent, status) {
+                total = db["memory_total"]
+                used  = db["memory_used"]
+                # To avoid division by zero when data is missing
+                if (total && used) {
+                    percent = round((used / total) * 100)
+                    status = sprintf("%d%%", percent)
+                } else {
+                    status = "__"
+                }
+                return sprintf("M=%s", status)
+            }
+
+            function make_status_cpu(    load, temp, fan) {
                 load = db["load_avg_1min"]
                 temp = db["temperature"] / 1000
-                return sprintf("C=[%4.2f %d°C]", load, temp)
+                fan  = db["fan_speed"]
+                return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan)
             }
 
             function make_status_disk(    bytes_per_sector, bytes_per_mb, w, r) {
@@ -536,6 +617,10 @@ consume() {
                 return sprintf("%s", symbol)
             }
 
+            function round(n) {
+                return int(n + 0.5)
+            }
+
             function debug(location, msg) {
                 if (opt_debug) {
                     print_error(location, msg)
@@ -575,6 +660,7 @@ main() {
     disk_space_device='/'
     disk_io_device='sda'
     thermal_zone=0
+    fan_path='/proc/acpi/ibm/fan'
 
     # User-overrides
     long_options=''
@@ -585,6 +671,7 @@ main() {
     long_options+=',prefixes_of_net_interfaces_to_show:'
     long_options+=',disk_space_device:'
     long_options+=',thermal_zone:'
+    long_options+=',fan_path:'
     OPTS=$(
         getopt \
             -o 'd' \
@@ -627,6 +714,10 @@ main() {
                 thermal_zone="$2"
                 shift 2
                 ;;
+            --fan_path)
+                fan_path="$2"
+                shift 2
+                ;;
             --)
                 shift
                 break
@@ -649,6 +740,7 @@ main() {
         echo "    disk_space_device|= $disk_space_device"
         echo "    disk_io_device|= $disk_io_device"
         echo "    thermal_zone|= $thermal_zone"
+        echo "    fan_path|= $fan_path"
       ) | column -ts\|
       echo ''
     ) >&2
@@ -668,6 +760,8 @@ main() {
 
     cmd_produce_temperature="produce_temperature $thermal_zone"
 
+    cmd_produce_fan="produce_fan $fan_path"
+
     # TODO: Redirect each worker's stderr to a dedicated log file
     spawn produce_datetime                 "$pipe" 'in:DATE_TIME' 1
     spawn "$cmd_produce_screen_brightness" "$pipe" 'in:SCREEN_BRIGHTNESS' 1
@@ -682,6 +776,9 @@ main() {
     spawn "$cmd_produce_disk_io"           "$pipe" 'in:DISK_IO' 1
     spawn produce_loadavg                  "$pipe" 'in:LOAD_AVG' 1
     spawn "$cmd_produce_temperature"       "$pipe" 'in:TEMPERATURE' 1
+    spawn "$cmd_produce_fan"               "$pipe" 'in:FAN' 1
+    spawn produce_memory                   "$pipe" 'in:MEMORY' 1
+    spawn produce_energy                   "$pipe" 'in:ENERGY' 1
     spawn produce_bar_req                  "$pipe" 'out:BAR'      1
 
     consume \
This page took 0.028625 seconds and 4 git commands to generate.