diff mbox series

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

Message ID 20210713220734.26302-6-niteesh.gs@gmail.com
State New
Headers show
Series python: AQMP-TUI Prototype | expand

Commit Message

Niteesh G. S. July 13, 2021, 10:07 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 | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/python/qemu/aqmp/aqmp_tui.py b/python/qemu/aqmp/aqmp_tui.py
index f853efc1f5..9ee91f0e99 100644
--- a/python/qemu/aqmp/aqmp_tui.py
+++ b/python/qemu/aqmp/aqmp_tui.py
@@ -12,6 +12,8 @@ 
 from logging import Handler
 import signal
 
+from pygments import lexers
+from pygments import token as Token
 import urwid
 import urwid_readline
 
@@ -33,6 +35,11 @@ 
     (Token.Literal.Number.Integer, '', '', '', '#fa0', 'g7'),
     (Token.Literal.String.Double, '', '', '', '#6f6', 'g7'),
     (Token.Keyword.Constant, '', '', '', '#6af', 'g7'),
+    ('DEBUG', '', '', '', '#ddf', 'g7'),
+    ('INFO', '', '', '', 'g100', 'g7'),
+    ('WARNING', '', '', '', '#ff6', 'g7'),
+    ('ERROR', '', '', '', '#a00', 'g7'),
+    ('CRITICAL', '', '', '', '#a00', 'g7'),
     ('background', '', 'black', '', '', 'g7'),
 ]
 
@@ -133,7 +140,7 @@  def cb_add_to_history(self, msg, level=None):
         formatted = []
         if level:
             msg = f'[{level}]: {msg}'
-            formatted.append(msg)
+            formatted.append((level, msg))
         else:
             lexer = lexers.JsonLexer()  # pylint: disable=no-member
             for token in lexer.get_tokens(msg):
@@ -162,6 +169,7 @@  def __init__(self, address):
         self.address = address
         self.aloop = None
         self.loop = None
+        self.screen = urwid.raw_display.Screen()
         super().__init__()
 
     def add_to_history(self, msg, level=None):
@@ -249,8 +257,10 @@  def run(self, debug=False):
             self.aloop.add_signal_handler(sig, self.kill_app)
 
         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)