sane-sysload: make the format CLI args friendlier

This commit is contained in:
2024-06-19 01:20:36 +00:00
parent 27efb10a27
commit 91c2b04ab4
3 changed files with 42 additions and 11 deletions

View File

@@ -66,6 +66,7 @@ end
if vars.percent ~= nil then
bat_args = bat_args .. " --percent-suffix '" .. vars.percent .. "'"
end
bat_args = bat_args .. " {bat}"
-- N.B.: `[[ <text> ]]` is Lua's multiline string literal
conky.text = [[
@@ -73,8 +74,8 @@ ${color1}${shadecolor 707070}${font sans-serif:size=50:style=Bold}${alignc}${exe
${color2}${shadecolor a4d7d0}${font sans-serif:size=20}${alignc}${exec date +"%a %d %b"}${font}
${color1}${shadecolor}${font sans-serif:size=22:style=Bold}${alignc}${execp @bat@ ]] .. bat_args .. [[ }${font}
${color1}${shadecolor}${font sans-serif:size=20:style=Bold}${alignc}${texeci 600 @weather@ }${font}
${color1}${shadecolor}${font sans-serif:size=22:style=Bold}${alignc}${execp sane-sysload ]] .. bat_args .. [[ }${font}
${color1}${shadecolor}${font sans-serif:size=20:style=Bold}${alignc}${texeci 600 timeout 20 sane-weather }${font}
${color2}${shadecolor a4d7d0}${font sans-serif:size=16}${alignc}⇅ ${downspeedf wlan0}]] .. vars.kBps .. [[${font}

View File

@@ -16,11 +16,7 @@
"sane-weather"
];
fs.".config/conky/conky.conf".symlink.target = pkgs.substituteAll {
src = ./conky.conf;
bat = "sane-sysload";
weather = "timeout 20 sane-weather";
};
fs.".config/conky/conky.conf".symlink.target = ./conky.conf;
services.conky = {
description = "conky dynamic desktop background";

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ])"
"""
usage: sane-sysload [options...]
usage: sane-sysload [options...] formatstr
pretty-prints a battery estimate (icon to indicate state, and a duration estimate)
@@ -11,6 +11,16 @@ options:
--hour-suffix <string>: use the provided string as an hours suffix
--icon-suffix <string>: use the provided string as an icon suffix
--percent-suffix <string>: use the provided string when displaying percents
formatstr is a Python format string.
variables available for formatting:
- {bat_icon}
- {bat_time}
- {mem_icon}
- {mem_pct}
and some presets, encapsulating the above:
- {bat}
- {mem}
"""
import argparse
@@ -266,6 +276,13 @@ class AllInfo:
_mem: MemInfo | None
_bat: BatteryInfo | None
@property
def bat(self) -> str:
return f"{self.bat_icon}{self.bat_time}"
@property
def mem(self) -> str:
return f"{self.mem_icon}{self.mem_pct}"
@property
def mem_icon(self) -> str:
if self._mem is None: return ""
@@ -307,6 +324,17 @@ class AllInfo:
else:
return self._fmt.render_percent(self._bat.percent_charged)
class LazyFormatter:
def __init__(self, obj: object, attr: str):
self.obj = obj
self.attr = attr
def __repr__(self) -> str:
return repr(getattr(self.obj, self.attr))
def __str__(self) -> str:
return str(getattr(self.obj, self.attr))
def main() -> None:
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
@@ -317,8 +345,7 @@ def main() -> None:
parser.add_argument("--hour-suffix", default=SUFFIX_HR)
parser.add_argument("--minute-suffix", default=SUFFIX_MIN)
parser.add_argument("--percent-suffix", default=SUFFIX_PERCENT)
parser.add_argument("--template", default="{_.bat_icon}{_.bat_time}")
# parser.add_argument("--template", default="{_.mem_icon}{_.mem_pct}")
parser.add_argument("formatstr")
args = parser.parse_args()
if args.debug:
@@ -335,7 +362,14 @@ def main() -> None:
MemInfo(),
try_all_batteries(),
)
print(args.template.format(_=info))
print(args.formatstr.format(
bat=LazyFormatter(info, "bat"),
bat_icon=LazyFormatter(info, "bat_icon"),
bat_time=LazyFormatter(info, "bat_time"),
mem=LazyFormatter(info, "mem"),
mem_icon=LazyFormatter(info, "mem_icon"),
mem_pct=LazyFormatter(info, "mem_pct"),
))
if __name__ == "__main__":
main()