diff options
author | Nico Schlömer <nico.schloemer@gmail.com> | 2022-03-05 19:45:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-05 19:45:37 +0100 |
commit | 9727e7267fb6964709f42b1480e415dfbe879a2d (patch) | |
tree | cd973c6b04db62f5dd237bc872160dc448a574a0 | |
parent | 5d10ec15cc4077a521eae5b2dba032802e1bd17d (diff) | |
parent | 2190464bfdd1d3766a86f0c7247726d9653af010 (diff) |
Merge pull request #68 from nschloe/swapv0.1.8
Swap
-rw-r--r-- | .pre-commit-config.yaml | 2 | ||||
-rw-r--r-- | src/tiptop/__about__.py | 2 | ||||
-rw-r--r-- | src/tiptop/_app.py | 6 | ||||
-rw-r--r-- | src/tiptop/_battery.py | 2 | ||||
-rw-r--r-- | src/tiptop/_mem.py | 40 |
5 files changed, 35 insertions, 17 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d63326e..cea1f19 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: isort - repo: https://github.com/psf/black - rev: 21.12b0 + rev: 22.1.0 hooks: - id: black language_version: python3 diff --git a/src/tiptop/__about__.py b/src/tiptop/__about__.py index f1380ee..9cb17e7 100644 --- a/src/tiptop/__about__.py +++ b/src/tiptop/__about__.py @@ -1 +1 @@ -__version__ = "0.1.7" +__version__ = "0.1.8" diff --git a/src/tiptop/_app.py b/src/tiptop/_app.py index f349aaf..e0bfc1c 100644 --- a/src/tiptop/_app.py +++ b/src/tiptop/_app.py @@ -95,10 +95,10 @@ def run(argv=None): ) else: grid.add_row(size=1, name="topline") - grid.add_row(fraction=2, name="top") + grid.add_row(fraction=3, name="top") grid.add_row(fraction=1, name="center1") - grid.add_row(fraction=2, name="center2") - grid.add_row(fraction=2, name="bottom") + grid.add_row(fraction=3, name="center2") + grid.add_row(fraction=3, name="bottom") grid.add_areas( area0="left-start|right-end,topline", area1="left-start|right-end,top", diff --git a/src/tiptop/_battery.py b/src/tiptop/_battery.py index 6a35017..68dbaf0 100644 --- a/src/tiptop/_battery.py +++ b/src/tiptop/_battery.py @@ -8,7 +8,7 @@ from .braille_stream import BrailleStream class Battery(Widget): def on_mount(self): - self.bat_stream = BrailleStream(40, 4, 0.0, 100.0) + self.bat_stream = BrailleStream(40, 3, 0.0, 100.0) self.panel = Panel( "", diff --git a/src/tiptop/_mem.py b/src/tiptop/_mem.py index a1d9d42..c3bae8a 100644 --- a/src/tiptop/_mem.py +++ b/src/tiptop/_mem.py @@ -10,16 +10,20 @@ from .braille_stream import BrailleStream class Mem(Widget): def on_mount(self): - self.mem_total_bytes = psutil.virtual_memory().total + mem = psutil.virtual_memory() + self.mem_total_bytes = mem.total # check which mem sections are available on the machine self.attrs = [] - mem = psutil.virtual_memory() - self.colors = ["yellow", "green", "blue", "magenta"] + self.colors = ["yellow", "green", "blue", "magenta", "red"] for attr in ["free", "available", "cached", "used"]: if hasattr(mem, attr): self.attrs.append(attr) + swap = psutil.swap_memory() + if swap is not None: + self.attrs.append("swap") + # append spaces to make all names equally long maxlen = max(len(string) for string in self.attrs) maxlen = min(maxlen, 5) @@ -29,12 +33,13 @@ class Mem(Widget): # [BrailleStream(40, 4, 0.0, self.mem_total_bytes)] * len(self.names) # since that only creates one BrailleStream, references n times. self.mem_streams = [] - for _ in range(len(self.attrs)): - self.mem_streams.append(BrailleStream(40, 4, 0.0, self.mem_total_bytes)) + for attr in self.attrs: + total = swap.total if attr == "swap" else self.mem_total_bytes + self.mem_streams.append(BrailleStream(40, 4, 0.0, total)) self.table = Table(box=None, expand=True, padding=0, show_header=False) self.table.add_column(justify="left", no_wrap=True) - for k in range(len(self.attrs)): + for _ in range(len(self.attrs)): self.table.add_row("") mem_total_string = sizeof_fmt(self.mem_total_bytes, fmt=".2f") @@ -50,22 +55,33 @@ class Mem(Widget): def refresh_table(self): mem = psutil.virtual_memory() + swap = psutil.swap_memory() + for k, (attr, label, stream) in enumerate( zip(self.attrs, self.labels, self.mem_streams) ): - val = getattr(mem, attr) + if attr == "swap": + val = swap.used + total = swap.total + if total == 0: + total = 1 + else: + val = getattr(mem, attr) + total = self.mem_total_bytes + stream.add_value(val) val_string = " ".join( [ label, sizeof_fmt(val, fmt=".2f"), - f"({val / self.mem_total_bytes * 100:.0f}%)", + f"({val / total * 100:.0f}%)", ] ) graph = "\n".join( [val_string + stream.graph[0][len(val_string) :]] + stream.graph[1:] ) self.table.columns[0]._cells[k] = f"[{self.colors[k]}]{graph}[/]" + self.refresh() def render(self) -> Panel: @@ -76,12 +92,14 @@ class Mem(Widget): ms.reset_width(event.width - 4) # split the available event.height-2 into n even blocks, and if there's - # a rest, divide it up into the last, e.g., with n=4 - # 17 -> 4, 4, 4, 5 + # a rest, divide it up into the first, e.g., with n=4 + # 17 -> 5, 4, 4, 4 n = len(self.attrs) heights = [(event.height - 2) // n] * n for k in range((event.height - 2) % n): - heights[-(k + 1)] += 1 + heights[k] += 1 + # add to last: + # heights[-(k + 1)] += 1 for ms, h in zip(self.mem_streams, heights): ms.reset_height(h) |