l 5 q: get battery & brightness stuff from tv

This commit is contained in:
lassulus 2016-11-10 22:41:01 +01:00
parent 46a27e5aa0
commit 18e9bfd04a

View File

@ -55,47 +55,143 @@ let
fi fi
''; '';
q-power_supply = '' q-intel_backlight = ''
for uevent in /sys/class/power_supply/*/uevent; do cd /sys/class/backlight/intel_backlight
if test -f $uevent; then </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
uevent=$1
eval "$(${pkgs.gnused}/bin/sed -n ' eval "$(${pkgs.gnused}/bin/sed -n '
s/^\([A-Z_]\+=\)\(.*\)/\1'\'''\2'\'''/p s/^\([A-Z_]\+=[0-9A-Za-z_-]*\)$/export \1/p
' $uevent)" ' $uevent)"
case $POWER_SUPPLY_NAME in
AC)
exit # not battery
;;
esac
exec </dev/null
exec ${pkgs.gawk}/bin/awk '
function die(s) {
printf "%s: %s\n", name, s
exit 1
}
if test "x''${POWER_SUPPLY_CHARGE_NOW-}" = x; then function print_hm(h, m) {
continue m = (h - int(h)) * 60
fi return sprintf("%dh%dm", h, m)
}
charge_percentage=$(echo " function print_bar(n, r, t1, t2, t_col) {
scale=2 t1 = int(r * n)
$POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL t2 = n - t1
" | ${pkgs.bc}/bin/bc) 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()
}
lfc=$POWER_SUPPLY_CHARGE_FULL function sgr(p) {
rc=$POWER_SUPPLY_CHARGE_NOW return "\x1b[" p "m"
#rc=2800 }
N=78; N=76
N=10 function strdup(s,n,t) {
n=$(echo $N-1 | ${pkgs.bc}/bin/bc) t = sprintf("%"n"s","")
centi=$(echo "$rc*100/$lfc" | ${pkgs.bc}/bin/bc) gsub(/ /,s,t)
deci=$(echo "$rc*$N/$lfc" | ${pkgs.bc}/bin/bc) return t
energy_evel=$( }
echo -n ' ' # TRIGRAM FOR THUNDER
if test $centi -ge 42; then echo -n '' END {
elif test $centi -ge 23; then echo -n '' name = ENVIRON["POWER_SUPPLY_NAME"]
elif test $centi -ge 11; then echo -n ''
else echo -n ''; fi charge_unit = "Ah"
for i in $(${pkgs.coreutils}/bin/seq 1 $deci); do charge_now = ENVIRON["POWER_SUPPLY_CHARGE_NOW"] / 10^6
echo -n charge_full = ENVIRON["POWER_SUPPLY_CHARGE_FULL"] / 10^6
done
echo -n '' current_unit = "A"
for i in $(${pkgs.coreutils}/bin/seq $deci $n); do current_now = ENVIRON["POWER_SUPPLY_CURRENT_NOW"] / 10^6
echo -n
done energy_unit = "Wh"
echo '' $rc #/ $lfc energy_now = ENVIRON["POWER_SUPPLY_ENERGY_NOW"] / 10^6
) energy_full = ENVIRON["POWER_SUPPLY_ENERGY_FULL"] / 10^6
echo "$energy_evel $charge_percentage"
fi power_unit = "W"
power_now = ENVIRON["POWER_SUPPLY_POWER_NOW"] / 10^6
voltage_unit = "V"
voltage_now = ENVIRON["POWER_SUPPLY_VOLTAGE_NOW"] / 10^6
voltage_min_design = ENVIRON["POWER_SUPPLY_VOLTAGE_MIN_DESIGN"] / 10^6
#printf "charge_now: %s\n", charge_now
#printf "charge_full: %s\n", charge_full
#printf "current_now: %s\n", current_now
#printf "energy_now: %s\n", energy_now
#printf "energy_full: %s\n", energy_full
#printf "energy_full: %s\n", ENVIRON["POWER_SUPPLY_ENERGY_FULL"]
#printf "energy_full: %s\n", ENVIRON["POWER_SUPPLY_ENERGY_FULL"] / 10^6
#printf "power_now: %s\n", power_now
#printf "voltage_now: %s\n", voltage_now
if (current_now == 0 && voltage_now != 0) {
current_now = power_now / voltage_now
}
if (power_now == 0) {
power_now = current_now * voltage_now
}
if (charge_now == 0 && voltage_min_design != 0) {
charge_now = energy_now / voltage_min_design
}
if (energy_now == 0) {
energy_now = charge_now * voltage_min_design
}
if (charge_full == 0 && voltage_min_design != 0) {
charge_full = energy_full / voltage_min_design
}
if (energy_full == 0) {
energy_full = charge_full * voltage_min_design
}
if (charge_now == 0 || charge_full == 0) {
die("unknown charge")
}
charge_ratio = charge_now / charge_full
out = out name
out = out sprintf(" %s", print_bar(10, charge_ratio))
out = out sprintf(" %d%", charge_ratio * 100)
out = out sprintf(" %.2f%s", charge_now, charge_unit)
if (current_now != 0) {
out = out sprintf("/%.1f%s", current_now, current_unit)
}
out = out sprintf(" %d%s", energy_full, energy_unit)
if (power_now != 0) {
out = out sprintf("/%.1f%s", power_now, power_unit)
}
if (current_now != 0) {
out = out sprintf(" %s", print_hm(charge_now / current_now))
}
print out
}
'
'';
in ''
for uevent in /sys/class/power_supply/*/uevent; do
${power_supply} "$uevent" || :
done done
''; '';
@ -171,15 +267,20 @@ in
pkgs.writeBashBin "q" '' pkgs.writeBashBin "q" ''
set -eu set -eu
export PATH=/var/empty export PATH=/var/empty
(${q-todo}) || :
if [ "$PWD" != "$HOME" ]; then
(HOME=$PWD; ${q-todo}) || :
fi
echo
${q-cal} ${q-cal}
echo echo
${q-isodate} ${q-isodate}
(${q-gitdir}) & (${q-gitdir}) &
(${q-intel_backlight}) &
(${q-power_supply}) & (${q-power_supply}) &
(${q-virtualization}) & (${q-virtualization}) &
(${q-wireless}) & (${q-wireless}) &
(${q-online}) & (${q-online}) &
(${q-thermal_zone}) & (${q-thermal_zone}) &
wait wait
${q-todo}
'' ''