diff mbox series

[v3,11/13] python/aqmp-tui: Add ability to highlight messages

Message ID 20210730201846.5147-12-niteesh.gs@gmail.com (mailing list archive)
State New, archived
Headers show
Series AQMP TUI Draft | expand

Commit Message

Niteesh G. S. July 30, 2021, 8:18 p.m. UTC
Adds ability to highlight messages in the history box. The messages
can be selected using up/down arrow keys.
This can be enhanced in the future to apply specific settings to
a particular message.

Signed-off-by: G S Niteesh Babu <niteesh.gs@gmail.com>
---
 python/qemu/aqmp/aqmp_tui.py | 50 ++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
diff mbox series

Patch

diff --git a/python/qemu/aqmp/aqmp_tui.py b/python/qemu/aqmp/aqmp_tui.py
index fb828b1a27..4bae0d4e89 100644
--- a/python/qemu/aqmp/aqmp_tui.py
+++ b/python/qemu/aqmp/aqmp_tui.py
@@ -344,6 +344,7 @@  def __init__(self, master: App) -> None:
         self.master = master
         self.history = urwid.SimpleFocusListWalker([])
         super().__init__(self.history)
+        self.highlighting = -1
 
     def add_to_history(self,
                        history: Union[str, List[Tuple[str, str]]]) -> None:
@@ -351,8 +352,57 @@  def add_to_history(self,
         if self.history:
             self.history.set_focus(len(self.history) - 1)
 
+    def _remove_highlighting(self) -> None:
+        assert self.highlighting != -1
+        pos = self.highlighting
+        widget = self.history[pos]
+        widget = widget.original_widget
+        self.history[pos] = widget
+
+    def _update_highlighting(self) -> None:
+        assert self.highlighting != -1
+        pos = self.highlighting
+        widget = self.history[pos]
+        self.history[pos] = urwid.LineBox(widget)
+
+    def keypress(self, size: Tuple[int, int], key: str) -> Optional[str]:
+        if key == 'up':
+            if self.highlighting != -1:
+                pos = self.highlighting
+                self._remove_highlighting()
+                pos = max(pos - 1, 0)
+                self.highlighting = pos
+            else:
+                self.highlighting = len(self.history) - 1
+            self._update_highlighting()
+            self.change_focus(size, self.highlighting)
+            return None
+        if key == 'down':
+            pos = self.highlighting
+            if pos == -1:
+                return None
+
+            self._remove_highlighting()
+            if pos == len(self.history) - 1:
+                self.highlighting = -1
+            else:
+                self.highlighting = pos + 1
+                self._update_highlighting()
+                self.change_focus(size, self.highlighting)
+            return None
+
+        # Remove highlighting if someother key is pressed
+        if self.highlighting != -1:
+            self._remove_highlighting()
+            self.highlighting = -1
+        return super().keypress(size, key)  # type: ignore
+
     def mouse_event(self, size: Tuple[int, int], _event: str, button: float,
                     _x: int, _y: int, focus: bool) -> None:
+        if self.highlighting != -1:
+            self._remove_highlighting()
+            self.highlighting = -1
+
         # Scroll only on focus. Therefore it is required to
         # click on the widget to enable scrolling.
         if not focus: