From c8ffdc6997c893da9d8afaa19467a4ae233612b4 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 23 Apr 2019 16:12:07 +0200 Subject: pkgs/profpatsch/display-infos: display remaining battery life The battery life is displayed as an [sft] timespan. --- pkgs/profpatsch/display-infos/default.nix | 42 +++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'pkgs/profpatsch/display-infos/default.nix') diff --git a/pkgs/profpatsch/display-infos/default.nix b/pkgs/profpatsch/display-infos/default.nix index 9dc510cd..97a13944 100644 --- a/pkgs/profpatsch/display-infos/default.nix +++ b/pkgs/profpatsch/display-infos/default.nix @@ -1,4 +1,4 @@ -{ lib, runCommand, writeText, python3, libnotify, sfttime }: +{ lib, runCommand, writeText, python3, libnotify, bc, sfttime }: let name = "display-infos-0.1.0"; @@ -15,18 +15,48 @@ let with open(fn, 'r') as f: return int(f.read()) + def seconds_to_sft(secs): + p = sub.Popen(["@bc@", "-l"], stdin=sub.PIPE, stdout=sub.PIPE) + (sft, _) = p.communicate(input="scale=2; obase=16; {} / 86400\n".format(secs).encode()) + p.terminate() + return str(sft.strip().decode()) + + charging = readint("/sys/class/power_supply/AC/online") + full = 0 now = 0 + # this is "to charged" if charging and "to empty" if not + seconds_remaining = 0 for bat in glob.iglob("/sys/class/power_supply/BAT*"): + # these files might be different for different ACPI/battery providers + # see the full list in acpi.c of the acpi(1) tool + # unit: who knows full += readint(path.join(bat, "energy_full")) now += readint(path.join(bat, "energy_now" )) + # in unit?/hours, hopefully the same unit as above + # ACPI is a garbage fire + current_rate = readint(path.join(bat, "power_now")) + + if current_rate == 0: + continue + elif charging: + seconds_remaining += 3600 * (full - now) / current_rate + else: + seconds_remaining += 3600 * now / current_rate bat = round( now/full, 2 ) - ac = "🗲" if readint("/sys/class/power_supply/AC/online") else "" + ac = "🗲 " if charging else "" + sft_remaining = seconds_to_sft(seconds_remaining) date = sub.run(["date", "+%d.%m. %a %T"], stdout=sub.PIPE).stdout.strip().decode() - sfttime = sub.run(["${sfttime}/bin/sfttime"], stdout=sub.PIPE).stdout.strip().decode() - notify = "BAT: {}% {} | {} | {}".format(int(bat*100), ac, date, sfttime) + sftdate = sub.run(["@sfttime@"], stdout=sub.PIPE).stdout.strip().decode() + notify = "BAT: {percent}% {ac}{charge}| {date} | {sftdate}".format( + percent = int(bat*100), + ac = ac, + charge = "{} ".format(sft_remaining) if seconds_remaining else "", + date = date, + sftdate = sftdate + ) sub.run(["@notify-send@", notify]) ''; @@ -36,6 +66,8 @@ in } '' substitute ${script} script \ --replace "@python3@" "${getBin python3}/bin/python3" \ - --replace "@notify-send@" "${getBin libnotify}/bin/notify-send" + --replace "@notify-send@" "${getBin libnotify}/bin/notify-send" \ + --replace "@bc@" "${getBin bc}/bin/bc" \ + --replace "@sfttime@" "${getBin sfttime}/bin/sfttime" install -D script $out/bin/display-infos '' -- cgit 1.4.1