diff mbox series

[5/6] python/aqmp-tui: add syntax highlighting

Message ID 20210702212603.26465-6-niteesh.gs@gmail.com (mailing list archive)
State New, archived
Headers show
Series python: AQMP-TUI Prototype | expand

Commit Message

Niteesh G. S. July 2, 2021, 9:26 p.m. UTC
Add syntax highlighting for the incoming and outgoing QMP messages.
This is achieved using the pygments module which was added in a
previous commit.

The current implementation is a really simple one which doesn't
allow for any configuration. In future this has to be improved
to allow for easier theme config using an external config of
some sort.

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

Patch

diff --git a/python/qemu/aqmp/aqmp_tui.py b/python/qemu/aqmp/aqmp_tui.py
index 8e9e8ac8ff..03cb70a523 100644
--- a/python/qemu/aqmp/aqmp_tui.py
+++ b/python/qemu/aqmp/aqmp_tui.py
@@ -11,6 +11,8 @@ 
 import logging
 import signal
 
+from pygments import lexers
+from pygments import token as Token
 import urwid
 import urwid_readline
 
@@ -21,6 +23,16 @@ 
 
 UPDATE_MSG = 'UPDATE_MSG'
 
+palette = [
+    (Token.Punctuation, '', '', '', 'h15,bold', 'g7'),
+    (Token.Text, '', '', '', '', 'g7'),
+    (Token.Name.Tag, '', '', '', 'bold,#f88', 'g7'),
+    (Token.Literal.Number.Integer, '', '', '', '#fa0', 'g7'),
+    (Token.Literal.String.Double, '', '', '', '#6f6', 'g7'),
+    (Token.Keyword.Constant, '', '', '', '#6af', 'g7'),
+    ('background', '', 'black', '', '', 'g7'),
+]
+
 
 class StatusBar(urwid.Text):
     """
@@ -115,7 +127,11 @@  def __init__(self, master):
         urwid.connect_signal(self.master, UPDATE_MSG, self.cb_add_to_history)
 
     def cb_add_to_history(self, msg):
-        self.history.add_to_history(msg)
+        formatted = []
+        lexer = lexers.JsonLexer()  # pylint: disable=no-member
+        for token in lexer.get_tokens(msg):
+            formatted.append(token)
+        self.history.add_to_history(formatted)
 
 
 class Window(urwid.Frame):
@@ -139,6 +155,7 @@  def __init__(self, address):
         self.address = address
         self.aloop = asyncio.get_event_loop()
         self.loop = None
+        self.screen = urwid.raw_display.Screen()
         super().__init__()
 
         # Gracefully handle SIGTERM and SIGINT signals
@@ -210,10 +227,14 @@  def handle_event(self, event):
             self.window.footer.set_text('Server shutdown')
 
     def run(self):
+        self.screen.set_terminal_properties(256)
+
         self.aloop.set_debug(True)
         event_loop = urwid.AsyncioEventLoop(loop=self.aloop)
-        self.loop = urwid.MainLoop(self.window,
+        self.loop = urwid.MainLoop(urwid.AttrMap(self.window, 'background'),
                                    unhandled_input=self.unhandled_input,
+                                   screen=self.screen,
+                                   palette=palette,
                                    handle_mouse=True,
                                    event_loop=event_loop)