summaryrefslogtreecommitdiffstats
path: root/src/tiptop/_battery.py
blob: 6a3501776189e884ff73d1a3da3c8cc6c5ec8953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import psutil
from rich import box
from rich.panel import Panel
from textual.widget import Widget

from .braille_stream import BrailleStream


class Battery(Widget):
    def on_mount(self):
        self.bat_stream = BrailleStream(40, 4, 0.0, 100.0)

        self.panel = Panel(
            "",
            title="",
            title_align="left",
            border_style="yellow",
            box=box.SQUARE,
        )
        self.collect_data()
        # update frequency: 1 min
        self.set_interval(60.0, self.collect_data)

    def collect_data(self):
        bat = psutil.sensors_battery()

        self.bat_stream.add_value(bat.percent)

        self.panel.renderable = "[yellow]" + "\n".join(self.bat_stream.graph) + "[/]\n"

        if bat.power_plugged:
            status = "charging"
        else:
            mm = bat.secsleft // 60
            hh, mm = divmod(mm, 60)
            time_left_str = []
            if hh > 0:
                time_left_str.append(f"{hh}h")
            if mm > 0:
                time_left_str.append(f"{mm}min")
            status = " ".join(time_left_str) + " left"

        title = f"battery - {self.bat_stream.values[-1]:.1f}% - {status}"
        if bat.percent < 15 and not bat.power_plugged:
            title = "[red reverse bold]" + title + "[/]"

        self.panel.title = title

        self.refresh()

    def render(self) -> Panel:
        return self.panel

    async def on_resize(self, event):
        self.bat_stream.reset_width(event.width - 4)
        self.bat_stream.reset_height(event.height - 2)