Import
authorSiraaj Khandkar <siraaj@khandkar.net>
Tue, 12 Jun 2018 18:17:24 +0000 (14:17 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Tue, 12 Jun 2018 18:18:20 +0000 (14:18 -0400)
README.md [new file with mode: 0644]
bin/khatus_cpu_usage_from_proc_since_last_check [new file with mode: 0755]
bin/khatus_show [new file with mode: 0755]
bin/khatus_update_network [new file with mode: 0755]
bin/khatus_update_weather [new file with mode: 0755]
install [new file with mode: 0755]
screenshot.jpg [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..cb7f3e3
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+khatus
+======
+
+My ad-hoc, text-only status bar scripts I use with `dwm` on GNU/Linux.
+
+![screenshot](screenshot.jpg)
diff --git a/bin/khatus_cpu_usage_from_proc_since_last_check b/bin/khatus_cpu_usage_from_proc_since_last_check
new file mode 100755 (executable)
index 0000000..e76d21d
--- /dev/null
@@ -0,0 +1,99 @@
+#! /bin/sh
+
+proc_stat_parse() {
+    proc_stat="$1"
+    n='[0-9]\+'
+    echo "$proc_stat" \
+    | grep "^cpu$n $n $n $n $n $n $n $n $n $n $n$" \
+    | awk '
+        {
+            cpu  = $1;
+            user = $2;
+            sys  = $4;
+            idle = $5;
+
+            total = user + sys + idle;
+            busy  = user + sys;
+
+            if (NR > 1) {printf " "};
+
+            out = sprintf("%s %d %d", cpu, total, busy);
+            #print out >> "cpu_usage_debug.txt";
+            printf "%s", out;
+        }
+        END {
+            #print "" >> "cpu_usage_debug.txt";
+            print "";
+        }
+        '
+}
+
+calc_delta() {
+    for proc_stat in "$1" "$2"; do
+        proc_stat_parse "$proc_stat"
+    done \
+    | awk '
+        {
+            t = NR;
+            for (i = 1; i <= (NF - 2); i += 3) {
+                cpu_count[t]++;
+                cpu_id = $i;  # For occasional debugging
+                total  = $(i + 1);
+                busy   = $(i + 2);
+                cpu[cpu_count[t], "total", t] = total;
+                cpu[cpu_count[t], "busy" , t] = busy;
+            }
+        }
+
+        END {
+            for (c=1; c<=cpu_count[2]; c++) {
+                total_1 = cpu[c, "total", 1];
+                total_2 = cpu[c, "total", 2];
+                busy_1  = cpu[c, "busy" , 1];
+                busy_2  = cpu[c, "busy" , 2];
+                total_d = total_2 - total_1;
+                busy_d  = busy_2  - busy_1;
+                percent_busy = (busy_d / total_d) * 100;
+
+                #printf(\
+                #    "c: %d, total_1: %f total_2: %f, total_d: %f\n",
+                #     c,     total_1,    total_2,     total_d \
+                #) >> "cpu_usage_debug.txt";
+                #printf(\
+                #    "c: %d, busy_1: %f busy_2: %f, busy_d: %f\n",
+                #     c,     busy_1,    busy_2,     busy_d \
+                #) >> "cpu_usage_debug.txt";
+                #printf(\
+                #    "c: %d, percent_busy: %f\n",
+                #     c, percent_busy \
+                #) >> "cpu_usage_debug.txt";
+
+                if (c > 1) {printf " "};
+                out = sprintf("%3.0f%%", percent_busy)
+                #printf "c: %d, out: %s\n", c, out >> "cpu_usage_debug.txt";
+                printf "%s", out;
+            }
+            #print "" >> "cpu_usage_debug.txt";
+            print "";
+        }
+        '
+}
+
+main() {
+    last_proc_stat="$HOME/var/run/cpu_usage_from_proc_since_last_check/last_proc_stat"
+
+    if [ ! -f "$last_proc_stat" ]
+    then
+        mkdir -p `dirname "$last_proc_stat"`
+        cat /proc/stat > "$last_proc_stat"
+        sleep 0.1
+    fi
+
+    previous=`cat $last_proc_stat`;
+    cat /proc/stat > "$last_proc_stat"
+    current=`cat $last_proc_stat`;
+
+    calc_delta "$previous" "$current"
+}
+
+main $@
diff --git a/bin/khatus_show b/bin/khatus_show
new file mode 100755 (executable)
index 0000000..19004be
--- /dev/null
@@ -0,0 +1,271 @@
+#! /bin/bash
+
+set -e
+
+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
+
+#load=$(cat /proc/loadavg | awk '{printf "%4.2f %4.2f %4.2f", $1, $2, $3}')
+
+fan=$(awk '/^speed:/ {printf "%4d", $2}' /proc/acpi/ibm/fan)
+
+cpu=$($BIN/khatus_cpu_usage_from_proc_since_last_check)
+
+memory=$(
+    free \
+    | awk '
+        function round(n) {return int(n + 0.5)}
+
+        $1 == "Mem:" {
+            total=$2;
+            used=$3;
+            cache=$6;
+            file = "/home/siraaj/var/run/status/memory_used_percentage";
+            curr = round(used / total * 100);
+            getline prev < file;
+            print curr > file;
+            if (curr > prev) {
+                direction = ">";
+            } else if (curr < prev) {
+                direction = "<";
+            } else {
+                direction = "=";
+            }
+            printf("%s%d%%", direction, curr);
+        }')
+
+temp=$(awk 'NR == 1 {print $1 / 1000}' /sys/class/thermal/thermal_zone0/temp)
+
+disk=$(
+    df \
+    | awk '
+        function round(n) {return int(n + 0.5)}
+
+        $1 == "/dev/mapper/kubuntu--vg-root" {
+            total = $2;
+            used  = $3;
+            file = "/home/siraaj/var/run/status/disk_space_used_percentage";
+            curr = round(used / total * 100);
+            getline prev < file;
+            print curr > file;
+            if (curr > prev) {
+                direction = ">";
+            } else if (curr < prev) {
+                direction = "<";
+            } else {
+                direction = "=";
+            }
+            printf("%s%d%%", direction, curr);
+        }')
+
+energy=$(
+    upower -e \
+    | grep battery \
+    | xargs upower -i \
+    | awk '
+        /^ +percentage: +/ {percentage=$2}
+        /^ +state: +/      {state=$2}
+        END {
+            if (state == "discharging") {
+                direction_of_change = "<"
+            } else if (state == "charging") {
+                direction_of_change = ">"
+            } else {
+                direction_of_change = "="
+            };
+            printf("%s%s", direction_of_change, percentage)
+        }')
+
+datetime=$(date +'%a, %b %d, %H:%M:%S')
+
+#volume_amixer=$(
+#    amixer get Master \
+#    | tail -1 \
+#    | awk '
+#        {
+#            level = $4;
+#            sub("^\\[", "", level);
+#            sub("\\]$", "", level);
+#            print level;
+#        }' \
+#    )
+
+#volume_amixer=$(
+#    amixer get Master \
+#    | tail -n 1 \
+#    | awk '{print $4}' \
+#    | tr -d '[]'
+#)
+
+volume_pactl=$(
+    pactl list sinks \
+    | awk '
+        /^\tMute:/ {
+            printf("%s,", $0);
+        }
+        /^\tVolume:/ {
+            for (i=2; i<=NF; i++) printf(" %s", $i);
+        }' \
+    | awk -v RS=',' '
+        /^[ \t]*Mute:/        {mute  = $2}
+        /^[ \t]*front-left:/  {left  = $4}
+        /^[ \t]*front-right:/ {right = $4}
+        END {
+            if (mute == "yes") {
+                printf("x")
+            } else {
+                printf("%s %s", left, right)
+            }
+        }
+        '
+)
+
+volume="[$volume_pactl]"
+
+wifi=$(cat $STATUS_FILE__WIFI)
+
+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)}'
+)
+
+#bluetooth_status=$(
+#    grep '^status:' /proc/acpi/ibm/bluetooth \
+#    | awk '
+#        $2 == "disabled" {printf "off"}
+#        $2 == "enabled"  {printf "on"}
+#        '
+#)
+
+bluetooth_power=$(
+    echo -e 'show \n quit' \
+    | bluetoothctl \
+    | awk '
+        /^Controller / {
+            controller = $2;
+            controllers[++ctrl_count] = controller;
+        }
+        /^\t[A-Z][A-Za-z]+:/ {
+            key = $1;
+            sub(":$", "", key);
+            val = $2;
+            for (i=3; i<=NF; i++) {
+                val = val " " $i};
+                data[controller, key] = val;
+        }
+        END {
+            # Using the 1st seen controller. Should we select specific instead?
+            power_status = data[controllers[1], "Powered"];
+            if (ctrl_count > 0) {
+                if (power_status == "no") {
+                    power_status = "off"
+                } else if (power_status == "yes") {
+                    power_status = "on"
+                } else {
+                    printf("Unexpected bluetooth power status: %s\n", power_status)\
+                        > "/dev/stderr";
+                    power_status = "ERROR"
+                }
+            } else {
+                power_status = "off"  # TODO: Perhaps use differentiated marker?
+            }
+            printf("%s", power_status);
+        }'
+)
+
+#touchpad_status=$(
+#    xinput list-props 12 \
+#    | awk '
+#        /^\tDevice Enabled \([0-9]+\):/ {
+#            status = $4;
+#            printf("%s", status);
+#        }'
+#)
+
+#color_off='\033[0m'
+#color_on_bg_gray='\033[m\033[40m'
+
+energy_direction=$(echo "$energy" | cut -b 1)
+energy_percentage=$(echo "$energy" | tr -d '<>=%')
+if [[ "$energy_direction" = '<' ]]
+then
+    if [[ $energy_percentage -le 5 ]]
+    then
+        DISPLAY=:0.0 notify-send \
+            -u critical \
+            "Energy CRITICALLY low: $energy" \
+            'CHARGE NOW!!! GO GO GO!!!'
+    elif [[ $energy_percentage -le 10 ]]
+    then
+        DISPLAY=:0.0 notify-send \
+            -u critical \
+            "Energy VERY low: $energy" \
+            'Plug it in ASAP.'
+    elif [[ $energy_percentage -le 15 ]]
+    then
+        DISPLAY=:0.0 notify-send \
+            -u critical \
+            "Energy low: $energy" \
+            'Get the charger.'
+    elif [[ $energy_percentage -le 50 ]]
+    then
+        if [[ ! -a "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" ]]
+        then
+            DISPLAY=:0.0 notify-send \
+                -u normal \
+                "Energy bellow half: $energy" \
+                'Where is the charger?'
+            touch "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
+        fi
+    fi
+else
+    rm -f "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
+fi
+
+weather="$(cat ~/var/run/metar-KJFK-decoded-temp-fahrenheit)°F"
+
+signal_last_msg_age=$(
+    ls -lt --time-style=+%s $HOME/var/lib/signal/latest_message.json \
+    | awk -v now_seconds=$(date +%s) \
+    '{
+        mtime_seconds = $6;
+        seconds = now_seconds - mtime_seconds;
+        minutes = seconds / 60;
+        hours   = minutes / 60;
+        days    = hours / 24;
+        weeks   = days / 7;
+        months  = days / 30;
+        #fmt = "%.1f";
+        fmt = "%d";
+        #printf(fmt " s\n", seconds);
+        printf(fmt " m\n", minutes);
+        printf(fmt " h\n", hours);
+        printf(fmt " d\n", days);
+        printf(fmt " w\n", weeks);
+        printf(fmt " mo\n", months);
+    }' \
+    | awk '$1 >= 1' \
+    | sort -n -k 1 \
+    | head -1 \
+    | tr -d ' '
+)
+
+echo \
+"\
+ E$energy\
+ M$memory\
+ D$disk\
+ C=[$cpu  ${temp}°C ${fan}rpm]\
+ |\
+ S=$screen_brightness\
+ V=$volume\
+ B:$bluetooth_power\
+ W:$wifi\
+ |\
+ $weather\
+ \
+ $datetime \
+"
diff --git a/bin/khatus_update_network b/bin/khatus_update_network
new file mode 100755 (executable)
index 0000000..f3b562f
--- /dev/null
@@ -0,0 +1,37 @@
+#! /bin/bash
+
+set -e
+
+#TERM=xterm-256color  # To keep unicode charcters from BARS
+
+STATUS_DIR=$HOME/var/run/status
+STATUS_FILE_WIFI=$STATUS_DIR/wifi
+STATUS_FILE_ETH=$STATUS_DIR/eth
+
+mkdir -p $STATUS_DIR
+
+# nmcli d \
+# | awk \
+#     -v file_wifi="$STATUS_FILE_WIFI" \
+#     -v file_eth="$STATUS_FILE_ETH" \
+#     '
+#     $2 == "wifi"     {wifi = $4}
+#     $2 == "ethernet" {eth=$4}
+#     END {
+#         print(wifi) > file_wifi;
+#         print(eth)  > file_eth;
+#     }
+#     '
+
+nmcli \
+    -f ACTIVE,SSID,SIGNAL \
+    -t \
+    d wifi \
+| awk \
+    -F ':' \
+    -v file_wifi="$STATUS_FILE_WIFI" \
+    '
+    BEGIN       {wifi_status = "--"}
+    $1 == "yes" {wifi_status =  $2 ":" $3 "%"}
+    END         {print wifi_status > file_wifi}
+    '
diff --git a/bin/khatus_update_weather b/bin/khatus_update_weather
new file mode 100755 (executable)
index 0000000..1127141
--- /dev/null
@@ -0,0 +1,28 @@
+#! /bin/bash
+
+set -e
+
+FILE_METAR_DECODED="$HOME/var/run/metar-KJFK-decoded"
+FILE_TEMP_FAHRENHEIT="${FILE_METAR_DECODED}-temp-fahrenheit"
+
+(metar -d KJFK 2>&1) > $FILE_METAR_DECODED  # TODO: Better error handling
+
+awk '
+    /METAR pattern not found in NOAA data/ {
+        failures++
+    }
+
+    /^Temperature/ {
+        celsius = $3;
+        fahrenheit = (celsius * (9 / 5)) + 32;
+    }
+
+    END {
+        if (failures > 0) {
+            print "--";
+        } else {
+            print fahrenheit;
+        }
+    }' \
+    $FILE_METAR_DECODED \
+> $FILE_TEMP_FAHRENHEIT
diff --git a/install b/install
new file mode 100755 (executable)
index 0000000..44fa3fe
--- /dev/null
+++ b/install
@@ -0,0 +1,15 @@
+#! /bin/sh
+
+PREFIX="$HOME/bin"
+
+if [ $1 ]
+then
+    PREFIX=$1
+fi
+
+cd bin
+
+for filename in *
+do
+    ln -s "`pwd`/$filename" "$PREFIX/$filename"
+done
diff --git a/screenshot.jpg b/screenshot.jpg
new file mode 100644 (file)
index 0000000..e3ce042
Binary files /dev/null and b/screenshot.jpg differ
This page took 0.050145 seconds and 4 git commands to generate.