diff options
author | Nico Schlömer <nico.schloemer@gmail.com> | 2021-11-15 18:02:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-15 18:02:05 +0100 |
commit | 12e03f783aa8c4f9f26b2207466c76633e6b0e6a (patch) | |
tree | 2a05c8e626d06d7350b194f9cd93a710c5c8ad00 | |
parent | 030e5032ff1889b86abe980028bde3a15f20e226 (diff) | |
parent | 1266c29e7d019064331c2e39d43dd2e58efa0eaf (diff) |
Merge pull request #18 from nschloe/mem-fixv0.0.7
mem fix
-rw-r--r-- | setup.cfg | 2 | ||||
-rw-r--r-- | src/tiptop/_mem.py | 30 |
2 files changed, 14 insertions, 18 deletions
@@ -1,6 +1,6 @@ [metadata] name = tiptop -version = 0.0.6 +version = 0.0.7 author = Nico Schlömer author_email = nico.schloemer@gmail.com description = A better top diff --git a/src/tiptop/_mem.py b/src/tiptop/_mem.py index b33f911..6191b8a 100644 --- a/src/tiptop/_mem.py +++ b/src/tiptop/_mem.py @@ -15,40 +15,36 @@ class Mem(Widget): self.mem_total_bytes = psutil.virtual_memory().total self.mem_total_string = sizeof_fmt(self.mem_total_bytes, fmt=".2f") + # check which mem sections are available on the machine + self.attrs = [] mem = psutil.virtual_memory() - - self.names = [] - if hasattr(mem, "used"): - self.names.append("used") - if hasattr(mem, "available"): - self.names.append("avail") - if hasattr(mem, "cached"): - self.names.append("cached") - if hasattr(mem, "free"): - self.names.append("free") + for attr in ["used", "available", "cached", "free"]: + if hasattr(mem, attr): + self.attrs.append(attr) # append spaces to make all names equally long - maxlen = max(len(string) for string in self.names) - self.names = [name + " " * (maxlen - len(name)) for name in self.names] + maxlen = max(len(string) for string in self.attrs) + maxlen = min(maxlen, 5) + self.labels = [attr[:maxlen].ljust(maxlen) for attr in self.attrs] # can't use # [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.names)): + for _ in range(len(self.attrs)): self.mem_streams.append(BrailleStream(40, 4, 0.0, self.mem_total_bytes)) self.set_interval(2.0, self.collect_data) def collect_data(self): mem = psutil.virtual_memory() - values = [mem.used, mem.available, mem.cached, mem.free] graphs = [] - for name, stream, val in zip(self.names, self.mem_streams, values): + for attr, label, stream in zip(self.attrs, self.labels, self.mem_streams): + val = getattr(mem, attr) stream.add_value(val) val_string = " ".join( [ - name, + label, sizeof_fmt(val, fmt=".2f"), f"({val / self.mem_total_bytes * 100:.0f}%)", ] @@ -86,7 +82,7 @@ class Mem(Widget): # split the available event.height-2 into n even blocks, and if there's # a rest, divide it up into the first, e.g., with n=4 # 17 -> 5, 4, 4, 4 - n = len(self.names) + n = len(self.attrs) heights = [(event.height - 2) // n] * n for k in range((event.height - 2) % n): heights[k] += 1 |