robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
35ec60f73a
master
1# Register a gdb pretty printer for UGrid instances. Usage: 2# 3# - start gdb 4# - run `source contrib/gdb/nvim-gdb-pretty-printers.py` 5# - when a `UGrid` pointer can be evaluated in the current frame, just print 6# it's value normally: `p *grid` (assuming `grid` is the variable name 7# holding the pointer) 8# - highlighting can be activated by setting the NVIM_GDB_HIGHLIGHT_UGRID 9# environment variable(only xterm-compatible terminals supported). This 10# can be done while gdb is running through the python interface: 11# `python os.environ['NVIM_GDB_HIGHLIGHT_UGRID'] = '1'` 12import os 13import gdb 14import gdb .printing 15 16 17SGR0 = '\x1b(B\x1b[m' 18 19 20def get_color_code (bg ,color_num ): 21if color_num < 16 : 22prefix = 3 23if color_num > 7 : 24prefix = 9 25if bg : 26prefix += 1 27color_num %= 8 28else : 29prefix = '48;5;' if bg else '38;5;' 30return '\x1b[{0}{1}m' .format (prefix ,color_num ) 31 32 33def highlight (attrs ): 34fg ,bg = [int (attrs ['foreground' ]),int (attrs ['background' ])] 35rv = [SGR0 ]# start with sgr0 36if fg != - 1 : 37rv .append (get_color_code (False ,fg )) 38if bg != - 1 : 39rv .append (get_color_code (True ,bg )) 40if bool (attrs ['bold' ]): 41rv .append ('\x1b[1m' ) 42if bool (attrs ['italic' ]): 43rv .append ('\x1b[3m' ) 44if bool (attrs ['undercurl' ])or bool (attrs ['underline' ]): 45rv .append ('\x1b[4m' ) 46if bool (attrs ['reverse' ]): 47rv .append ('\x1b[7m' ) 48return '' .join (rv ) 49 50 51class UGridPrinter (object ): 52def __init__ (self ,val ): 53self .val = val 54 55def to_string (self ): 56do_hl = (os .getenv ('NVIM_GDB_HIGHLIGHT_UGRID' )and 57os .getenv ('NVIM_GDB_HIGHLIGHT_UGRID' )!= '0' ) 58grid = self .val 59height = int (grid ['height' ]) 60width = int (grid ['width' ]) 61delimiter = '-' * (width + 2 ) 62rows = [delimiter ] 63for row in range (height ): 64cols = [] 65if do_hl : 66cols .append (SGR0 ) 67curhl = None 68for col in range (width ): 69cell = grid ['cells' ][row ][col ] 70if do_hl : 71hl = highlight (cell ['attrs' ]) 72if hl != curhl : 73cols .append (hl ) 74curhl = hl 75cols .append (cell ['data' ].string ('utf-8' )) 76if do_hl : 77cols .append (SGR0 ) 78rows .append ('|' + '' .join (cols )+ '|' ) 79rows .append (delimiter ) 80return '\n' + '\n' .join (rows ) 81 82def display_hint (self ): 83return 'hint' 84 85 86def pretty_printers (): 87pp = gdb .printing .RegexpCollectionPrettyPrinter ('nvim' ) 88pp .add_printer ('UGrid' ,'^ugrid$' ,UGridPrinter ) 89return pp 90 91 92gdb .printing .register_pretty_printer (gdb ,pretty_printers (),replace = True )