refactor: sane-sysinfo: more cleanups
This commit is contained in:
parent
a63f6281c5
commit
6963998519
|
@ -68,6 +68,31 @@ class Formatter:
|
||||||
suffix_hr: str = SUFFIX_HR
|
suffix_hr: str = SUFFIX_HR
|
||||||
suffix_min: str = SUFFIX_MIN
|
suffix_min: str = SUFFIX_MIN
|
||||||
|
|
||||||
|
def render_icon(self, direction: ChargeDirection, percentage: float) -> str:
|
||||||
|
return f"{self._choose_icon(direction, percentage)}{self.suffix_icon}"
|
||||||
|
|
||||||
|
def render_hours_minutes(self, minutes: int) -> str:
|
||||||
|
hr = minutes // 60
|
||||||
|
min = minutes % 60
|
||||||
|
return f"{hr}{self.suffix_hr}{min:02}{self.suffix_min}"
|
||||||
|
|
||||||
|
def render_percent(self, pct: int) -> str:
|
||||||
|
return f"{pct}{self.suffix_percent}"
|
||||||
|
|
||||||
|
def _choose_icon(self, direction: ChargeDirection, percentage: float) -> str:
|
||||||
|
level = percentage / 25
|
||||||
|
level = max(0, min(3, level))
|
||||||
|
level = int(round(level))
|
||||||
|
|
||||||
|
logger.debug(f"render_icon: direction={direction} level={level}")
|
||||||
|
|
||||||
|
if direction == ChargeDirection.Charging:
|
||||||
|
return ICON_BAT_CHG[level]
|
||||||
|
elif direction == ChargeDirection.Discharging:
|
||||||
|
return ICON_BAT_DIS[level]
|
||||||
|
|
||||||
|
raise RuntimeError(f"invalid ChargeDirection {direction}")
|
||||||
|
|
||||||
class PowerSupply:
|
class PowerSupply:
|
||||||
"""
|
"""
|
||||||
reads values from /sys/class/power_supply/$dev/ API
|
reads values from /sys/class/power_supply/$dev/ API
|
||||||
|
@ -85,7 +110,7 @@ class PowerSupply:
|
||||||
def try_read_uncached(self, rel_path: str) -> str | None:
|
def try_read_uncached(self, rel_path: str) -> str | None:
|
||||||
try:
|
try:
|
||||||
v = open(f"{self.sysfs_node}/{rel_path}").read()
|
v = open(f"{self.sysfs_node}/{rel_path}").read()
|
||||||
logger.debug(f"${self.sysfs_node}/{rel_path}: {v}")
|
logger.debug(f"{self.sysfs_node}/{rel_path}: {v}")
|
||||||
return v
|
return v
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
@ -161,20 +186,6 @@ class BatteryInfo:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def render_icon(direction: ChargeDirection, percentage: float) -> str:
|
|
||||||
level = percentage / 25
|
|
||||||
level = max(0, min(3, level))
|
|
||||||
level = int(round(level))
|
|
||||||
|
|
||||||
logger.debug(f"render_icon: direction={direction} level={level}")
|
|
||||||
|
|
||||||
if direction == ChargeDirection.Charging:
|
|
||||||
return ICON_BAT_CHG[level]
|
|
||||||
elif direction == ChargeDirection.Discharging:
|
|
||||||
return ICON_BAT_DIS[level]
|
|
||||||
|
|
||||||
raise RuntimeError(f"invalid ChargeDirection {direction}")
|
|
||||||
|
|
||||||
def try_path(p: str) -> BatteryInfo | None:
|
def try_path(p: str) -> BatteryInfo | None:
|
||||||
"""
|
"""
|
||||||
try to read battery information from some p = "/sys/class/power_supply/$node" path
|
try to read battery information from some p = "/sys/class/power_supply/$node" path
|
||||||
|
@ -205,28 +216,22 @@ def try_all_paths() -> BatteryInfo | None:
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def fmt_minutes(f: Formatter, icon: str, if_indefinite: str, minutes: int | None) -> str:
|
|
||||||
logger.debug(f"charge/discharge time: {minutes} min")
|
|
||||||
|
|
||||||
if minutes < 1440:
|
|
||||||
hr = minutes // 60
|
|
||||||
min = minutes % 60
|
|
||||||
return f"{icon}{f.suffix_icon}{hr}{f.suffix_hr}{min:02}{f.suffix_min}"
|
|
||||||
else:
|
|
||||||
logger.debug("charge/discharge duration > 1d")
|
|
||||||
return f"{icon}{f.suffix_icon}{if_indefinite}"
|
|
||||||
|
|
||||||
def pretty_output(f: Formatter, p: BatteryInfo) -> str:
|
def pretty_output(f: Formatter, p: BatteryInfo) -> str:
|
||||||
if p.minutes_to_charged != None:
|
if p.minutes_to_charged != None:
|
||||||
logger.debug("charging")
|
logger.debug("charging")
|
||||||
icon = render_icon(ChargeDirection.Charging, p.percent_charged)
|
icon = f.render_icon(ChargeDirection.Charging, p.percent_charged)
|
||||||
duration = p.minutes_to_charged
|
duration = p.minutes_to_charged
|
||||||
else:
|
else:
|
||||||
logger.debug("discharging")
|
logger.debug("discharging")
|
||||||
icon = render_icon(ChargeDirection.Discharging, p.percent_charged)
|
icon = f.render_icon(ChargeDirection.Discharging, p.percent_charged)
|
||||||
duration = p.minutes_to_discharged
|
duration = p.minutes_to_discharged
|
||||||
|
|
||||||
return fmt_minutes(f, icon, f"{p.percent_charged}{f.suffix_percent}", duration)
|
if duration is not None and duration < 1440:
|
||||||
|
details = f.render_hours_minutes(duration)
|
||||||
|
else:
|
||||||
|
details = f.render_percent(p.percent_charged)
|
||||||
|
|
||||||
|
return f"{icon}{details}"
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user