Take prefixes of net interfaces from CLI options
[khatus.git] / bin / khatus_show
index dc9a976..b0b87a9 100755 (executable)
@@ -6,6 +6,36 @@ BIN=$HOME/bin
 STATUS_DIR=$HOME/var/run/status
 STATUS_FILE__WIFI=$STATUS_DIR/wifi
 STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF=$STATUS_DIR/notified_energy_bellow_half
+while getopts ":b:d:s:i:" opt
+do
+    case "$opt" in
+        b)
+            SCREEN_BRIGHTNESS_DEVICE_NAME="$OPTARG"
+            ;;
+        d)
+            DISK_IO_DEVICE="$OPTARG"
+            ;;
+        s)
+            DISK_SPACE_DEVICE="$OPTARG"
+            ;;
+        i)
+            PREFIXES_OF_INTERFACES_TO_SHOW="$OPTARG"
+            ;;
+        \?)
+            echo "Unknown option: $OPTARG" >&2
+            exit 1
+            ;;
+        :)
+            echo "Missing argument for option: $OPTARG" >&2
+            exit 1
+            ;;
+    esac
+done
+if [ "$SCREEN_BRIGHTNESS_DEVICE_NAME" == '' ]; then echo 'Missing -b' >&2; exit 1; fi
+if [ "$DISK_IO_DEVICE" == '' ];                then echo 'Missing -d' >&2; exit 1; fi
+if [ "$DISK_SPACE_DEVICE" == '' ];             then echo 'Missing -s' >&2; exit 1; fi
+SCREEN_BRIGHTNESS_DEVICE_PATH="/sys/class/backlight/$SCREEN_BRIGHTNESS_DEVICE_NAME"
+
 
 load=$(awk '{printf("%4.2f", $1)}' /proc/loadavg)
 
@@ -22,7 +52,7 @@ memory=$(
             total=$2;
             used=$3;
             cache=$6;
-            prev_file = "/home/siraaj/var/run/status/memory_used_percentage";
+            prev_file = ENVIRON["HOME"] "/var/run/status/memory_used_percentage";
             curr = round(used / total * 100);
             getline prev < prev_file;
             print curr > prev_file;
@@ -47,7 +77,7 @@ disk_io=$(
             curr_sectors_read  = $3
             curr_sectors_write = $7
 
-            prev_file_prefix        = "/home/siraaj/var/run/status/disk_io"
+            prev_file_prefix        = ENVIRON["HOME"] "/var/run/status/disk_io"
             prev_sectors_read_file  = prev_file_prefix "_sectors_read"
             prev_sectors_write_file = prev_file_prefix "_sectors_write"
 
@@ -69,17 +99,20 @@ disk_io=$(
             printf("%0.3f▲ %0.3f▼\n", diff_write, diff_read);
 
         }
-        ' /sys/block/dm-1/stat
+        ' "/sys/block/$DISK_IO_DEVICE/stat"
 )
 
 disk=$(
     df \
-    | awk -v disk_io="$disk_io" '
+    | awk \
+        -v disk_io="$disk_io" \
+        -v device="$DISK_SPACE_DEVICE" \
+        '
         function round(n) {return int(n + 0.5)}
 
-        $1 == "/dev/mapper/kubuntu--vg-root" {
+        $1 == device {
             curr_perc = $5; sub("%$", "", curr_perc);
-            prev_perc_file = "/home/siraaj/var/run/status/disk_space_used";
+            prev_perc_file = ENVIRON["HOME"] "/var/run/status/disk_space_used";
             getline prev_perc   < prev_perc_file;
             print curr_perc > prev_perc_file;
             if (curr_perc > prev_perc) {
@@ -92,9 +125,13 @@ disk=$(
             printf("%s[%d%% %s]", direction, curr_perc, disk_io);
         }')
 
+# TODO: Wi-Fi status file should be a file per-wifi-device
 network=$(
     ip -s addr \
-    | awk -v wifi_conn="$(cat $STATUS_FILE__WIFI)" '
+    | awk \
+        -v wifi_conn="$(cat $STATUS_FILE__WIFI)" \
+        -v prefixes_of_interfaces_to_show="$PREFIXES_OF_INTERFACES_TO_SHOW" \
+        '
         BEGIN {
             bytes_per_unit = 1024 * 1024
         }
@@ -121,17 +158,14 @@ network=$(
         }
 
         END {
-            excluded["lo"]++
-            excluded["enp0s25"]++
-
-            for (i=1; i<=sequence; i++) {
-                interface = interfaces[i]
+            for (seq=1; seq<=sequence; seq++) {
+                interface = interfaces[seq]
                 label = substr(interface, 1, 1)
                 if (addrs[interface]) {
                     curr_read  = io[interface, "r"]
                     curr_write = io[interface, "w"]
 
-                    prefix = "/home/siraaj/var/run/status/io_net_" interface
+                    prefix = ENVIRON["HOME"] "/var/run/status/io_net_" interface
                     prev_read_file  = prefix "_read"
                     prev_write_file = prefix "_write"
 
@@ -145,19 +179,28 @@ network=$(
                     print curr_write > prev_write_file
 
                     io_stat = sprintf("%0.3f▲ %0.3f▼", diff_written, diff_read)
-                    if (interface == "wlp3s0") {
+                    if (interface ~ "^w") {
                         label = label ":" wifi_conn
                     }
                 } else {
                     io_stat = "--"
                 }
-                if (!(interface in excluded)) {
-                    if (++count_printed > 1) {
-                        sep = "  "
-                    } else {
-                        sep = ""
+                number_of_interfaces_to_show = \
+                    split(\
+                        prefixes_of_interfaces_to_show,\
+                        array_of_prefixes_of_interfaces_to_show,\
+                        " +"\
+                    )
+                for (n = 1; n <= number_of_interfaces_to_show; n++) {
+                    prefix = array_of_prefixes_of_interfaces_to_show[n]
+                    if (interface ~ ("^" prefix)) {
+                        if (++count_printed > 1) {
+                            sep = "  "
+                        } else {
+                            sep = ""
+                        }
+                        printf("%s%s:%s", sep, label, io_stat)
                     }
-                    printf("%s%s:%s", sep, label, io_stat)
                 }
             }
         }'
@@ -261,9 +304,11 @@ volume_pactl=$(
 volume="($volume_pactl)"
 
 screen_brightness=$(
-    max=$(cat /sys/class/backlight/acpi_video0/max_brightness)
-    cur=$(cat /sys/class/backlight/acpi_video0/brightness)
-    awk -v max=$max -v cur=$cur 'BEGIN {printf("%d%%", cur/max*100)}'
+    echo -n $((
+          $(cat $SCREEN_BRIGHTNESS_DEVICE_PATH/brightness)
+        / $(cat $SCREEN_BRIGHTNESS_DEVICE_PATH/max_brightness)
+        * 100
+    ))
 )
 
 #bluetooth_status=$(
@@ -512,7 +557,7 @@ echo \
  \
  B:$bluetooth_power\
  \
- *$screen_brightness\
+ *$screen_brightness%\
  \
  $volume\
  \
This page took 0.03679 seconds and 4 git commands to generate.