From c8068a7c5027a94ca8480f94c6a31a5663c6106a Mon Sep 17 00:00:00 2001
From: tv <tv@krebsco.de>
Date: Mon, 7 Nov 2016 01:06:59 +0100
Subject: tv q: report charge, power, and remaining time

---
 tv/5pkgs/q/default.nix | 110 +++++++++++++++++++++++++++++++------------------
 1 file changed, 70 insertions(+), 40 deletions(-)

(limited to 'tv/5pkgs/q')

diff --git a/tv/5pkgs/q/default.nix b/tv/5pkgs/q/default.nix
index f923950f0..016da4405 100644
--- a/tv/5pkgs/q/default.nix
+++ b/tv/5pkgs/q/default.nix
@@ -62,47 +62,77 @@ let
     fi
   '';
 
-  q-power_supply = ''
-    for uevent in /sys/class/power_supply/*/uevent; do
-      if test -f $uevent; then
-        eval "$(${pkgs.gnused}/bin/sed -n '
-          s/^\([A-Z_]\+=\)\(.*\)/\1'\'''\2'\'''/p
-        ' $uevent)"
-
-        if test "x''${POWER_SUPPLY_CHARGE_NOW-}" = x; then
-          continue
-        fi
-
-        charge_percentage=$(echo "
-          scale=2
-          $POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL
-        " | ${pkgs.bc}/bin/bc)
-
-        lfc=$POWER_SUPPLY_CHARGE_FULL
-        rc=$POWER_SUPPLY_CHARGE_NOW
-        #rc=2800
-        N=78; N=76
-        N=10
-        n=$(echo $N-1 | ${pkgs.bc}/bin/bc)
-        centi=$(echo "$rc*100/$lfc" | ${pkgs.bc}/bin/bc)
-        deci=$(echo "$rc*$N/$lfc" | ${pkgs.bc}/bin/bc)
-        energy_evel=$(
-          echo -n '☳ ' # TRIGRAM FOR THUNDER
-          if   test $centi -ge 42; then echo -n ''
-          elif test $centi -ge 23; then echo -n ''
-          elif test $centi -ge 11; then echo -n ''
-          else                        echo -n ''; fi
-          for i in $(${pkgs.coreutils}/bin/seq 1 $deci); do
-            echo -n ■
-          done
-          echo -n ''
-          for i in $(${pkgs.coreutils}/bin/seq $deci $n); do
-            echo -n ■
-          done
-          echo '' $rc #/ $lfc
-        )
-        echo "$energy_evel $charge_percentage"
+  q-power_supply = let
+    power_supply = pkgs.writeBash "power_supply" ''
+      set -efu
+      uevent=$1
+      eval "$(${pkgs.gnused}/bin/sed -n '
+        s/^\([A-Z_]\+=[0-9A-Za-z_-]*\)$/export \1/p
+      ' $uevent)"
+      if test "x''${POWER_SUPPLY_CHARGE_NOW-}" = x; then
+        exit # not battery
       fi
+      exec </dev/null
+      exec ${pkgs.gawk}/bin/awk '
+        function print_hm(h, m) {
+          m = (h - int(h)) * 60
+          return sprintf("%dh%dm", h, m)
+        }
+
+        function print_bar(n, r, t1, t2, t_col) {
+          t1 = int(r * n)
+          t2 = n - t1
+          if (r >= .42)     t_col = "1;32"
+          else if (r >= 23) t_col = "1;33"
+          else if (r >= 11) t_col = "1;31"
+          else              t_col = "5;1;31"
+          return sgr(t_col) strdup("■", t1) sgr(";30") strdup("■", t2) sgr()
+        }
+
+        function sgr(p) {
+          return "\x1b[" p "m"
+        }
+
+        function strdup(s,n,t) {
+          t = sprintf("%"n"s","")
+          gsub(/ /,s,t)
+          return t
+        }
+
+        END {
+          voltage_unit = "V"
+          voltage_now = ENVIRON["POWER_SUPPLY_VOLTAGE_NOW"] / 10^6
+          voltage_min_design = ENVIRON["POWER_SUPPLY_VOLTAGE_MIN_DESIGN"] / 10^6
+
+          current_unit = "A"
+          current_now = ENVIRON["POWER_SUPPLY_CURRENT_NOW"] / 10^6
+
+          power_unit = "W"
+          power_now = current_now * voltage_now
+
+          charge_unit = "Ah"
+          charge_now = ENVIRON["POWER_SUPPLY_CHARGE_NOW"] / 10^6
+          charge_full = ENVIRON["POWER_SUPPLY_CHARGE_FULL"] / 10^6
+          charge_ratio = charge_now / charge_full
+
+          energy_unit = "Wh"
+          energy_full = charge_full * voltage_min_design
+
+          printf "%s %s %d%% %.2f%s/%.2f%s %d%s/%.1f%s %s\n" \
+            , ENVIRON["POWER_SUPPLY_NAME"] \
+            , print_bar(10, charge_ratio) \
+            , charge_ratio * 100 \
+            , charge_now, charge_unit \
+            , current_now, current_unit \
+            , energy_full, energy_unit \
+            , power_now, power_unit \
+            , print_hm(charge_now / current_now)
+        }
+      '
+    '';
+  in ''
+    for uevent in /sys/class/power_supply/*/uevent; do
+      ${power_supply} "$uevent"
     done
   '';
 
-- 
cgit v1.2.3


From 67f68fe7ea55b57c5a3e69470266386549709787 Mon Sep 17 00:00:00 2001
From: tv <tv@krebsco.de>
Date: Mon, 7 Nov 2016 01:16:11 +0100
Subject: tv q: add intel_backlight

---
 tv/5pkgs/q/default.nix | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

(limited to 'tv/5pkgs/q')

diff --git a/tv/5pkgs/q/default.nix b/tv/5pkgs/q/default.nix
index 016da4405..7e2a30c3e 100644
--- a/tv/5pkgs/q/default.nix
+++ b/tv/5pkgs/q/default.nix
@@ -62,6 +62,21 @@ let
     fi
   '';
 
+  q-intel_backlight = ''
+    cd /sys/class/backlight/intel_backlight
+    </dev/null exec ${pkgs.gawk}/bin/awk '
+      END {
+        getline actual_brightness < "actual_brightness"
+        getline max_brightness < "max_brightness"
+        getline brightness < "brightness"
+        printf "intel_backlight %d%% %d/%d\n" \
+            , actual_brightness / max_brightness * 100 \
+            , actual_brightness \
+            , max_brightness
+      }
+    '
+  '';
+
   q-power_supply = let
     power_supply = pkgs.writeBash "power_supply" ''
       set -efu
@@ -218,6 +233,7 @@ pkgs.writeBashBin "q" ''
   ${q-isodate}
   ${q-sgtdate}
   (${q-gitdir}) &
+  (${q-intel_backlight}) &
   (${q-power_supply}) &
   (${q-virtualization}) &
   (${q-wireless}) &
-- 
cgit v1.2.3