Debian Codegraph @

s_tui/sturwid/summary_text_list.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Copyright (C) 2017-2025 Alex Manuskin, Gil Tsuker
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19 from collections import OrderedDict
20
21 import urwid
22
23
24 class SummaryTextList:
25 MAX_LABEL_L = 12
26
27 def __init__(self, source, visible_sensors_list):
28 self.source = source
29 self.visible_summaries = OrderedDict()
30 keys = list(self.source.get_summary().keys())
31 # The title is the first in the list
32 self.visible_summaries[keys[0]] = any(visible_sensors_list)
33 # All others according to initial visibility
34 for key, visible in zip(keys[1:], visible_sensors_list):
35 self.visible_summaries[key] = visible
36
37 # We keep a dict of all the items in the summary list
38 self.summary_text_items = OrderedDict()
39
40 @staticmethod
41 def _format_display_val(val, alerts, suffixes, sensor_idx):
42 """Return urwid text markup for a summary value with optional suffix/color."""
43 text = str(val)
44 if 0 <= sensor_idx < len(suffixes) and suffixes[sensor_idx]:
45 text += " " + suffixes[sensor_idx]
46 attr = alerts[sensor_idx] if 0 <= sensor_idx < len(alerts) else None
47 return (attr, text) if attr else text
48
49 def get_text_item_list(self):
50 summery_text_list = []
51 alerts = self.source.get_sensor_alerts()
52 suffixes = self.source.get_sensor_suffixes()
53 summary_items = list(self.source.get_summary().items())
54 for item_idx, (key, val) in enumerate(summary_items):
55 label_w = urwid.Text(str(key[0 : self.MAX_LABEL_L]))
56 # item_idx 0 is the source title row; sensor alerts start at index 1.
57 display_val = self._format_display_val(val, alerts, suffixes, item_idx - 1)
58 value_w = urwid.Text(display_val, align="right")
59 # This can be accessed by the update method
60 self.summary_text_items[key] = value_w
61 col_w = urwid.Columns([("weight", 1.5, label_w), value_w])
62 # Use setdefault for atomic check-and-set (faster than try/except)
63 is_visible = self.visible_summaries.setdefault(key, True)
64 if is_visible:
65 summery_text_list.append(col_w)
66
67 return summery_text_list
68
69 def update_visibility(self, visible_sensors):
70 keys = list(self.visible_summaries.keys())
71 self.visible_summaries[keys[0]] = any(visible_sensors)
72 # Do not change visibility of title
73 for sensor, visible in zip(keys[1:], visible_sensors):
74 self.visible_summaries[sensor] = visible
75
76 def update(self):
77 alerts = self.source.get_sensor_alerts()
78 suffixes = self.source.get_sensor_suffixes()
79 summary_items = list(self.source.get_summary().items())
80 for item_idx, (key, val) in enumerate(summary_items):
81 if key in self.summary_text_items:
82 display_val = self._format_display_val(
83 val, alerts, suffixes, item_idx - 1
84 )
85 self.summary_text_items[key].set_text(display_val)
86
87 def get_is_available(self):
88 return self.source.get_is_available()