From: Rick van Rein Date: Sat, 23 Feb 2019 10:49:49 +0000 (+0100) Subject: Color output for derdump (after "pip install colored") X-Git-Url: http://git.arpa2.org/?p=hexio;a=commitdiff_plain;h=031d7dc698745c3a5efc71e9929c0a51c1b59009 Color output for derdump (after "pip install colored") Thanks to Adriaan for the suggestion! (You may need to use "less -R" for paging to see the colours) --- diff --git a/README.MD b/README.MD index 56e0f98..51411a4 100644 --- a/README.MD +++ b/README.MD @@ -24,3 +24,7 @@ simplicity, they can be true life savers. full of errors; the utility will complain loudly, but continue instead of failing fatally; this makes it a very useful developer tool. + Use the option `--colour` to get output with helpful colouring; the + most important information will stand out in bold face. You can pipe + it into `less -R` if you need a colour-friendly pager. + diff --git a/derdump b/derdump index 46fd63f..d841ab8 100755 --- a/derdump +++ b/derdump @@ -1,29 +1,28 @@ -#!/usr/bin/python +#!/usr/bin/env python import sys -if len (sys.argv) != 2: - print 'Usage: ' + sys.argv [0] + 'file.der' - print 'Output: MEANING: TAG ###CONTLEN @TAGOFS ^NESTING, CLASS, PRIMCONSTR' - sys.exit (1) +import optparse -der = open (sys.argv [1], 'r').read (65537) -ofs = 0 +opts = optparse.OptionParser () +opts.add_option ('-c', '--colour', '--color', + action='store_true', default=False, dest='colour', + help='Print colourful output (loads Python module "colored", page with "less -R")') +(options,args) = opts.parse_args () -def eof (): - global ofs, der - return ofs >= len (der) +if len (args) != 1: + print 'Usage: ' + attr ('bold') + sys.argv [0] + '[--colour] file.der' + attr (0) + print 'Output: ' + nicemeaning ('MEANING') + ': ' + nicetag ('TAG') + ' ' + nicecontlen ('CONTLEN', 3) + ' ' + nicetagofs ('TAGOFS') + ' ' + nicenesting ('NESTING') + ', ' + niceclass ('CLASS') + ', ' + niceprimconstr ('PRIMCONSTR') + sys.exit (1) -def read1 (): - global ofs, der - if eof (): - print 'ATTEMPTED READ BEYOND EOF (RETURNING 0x00)' - return 0 - else: - ofs = ofs + 1 - return ord (der [ofs-1]) +if options.colour: + from colored import fg, bg, attr +else: + def attr (_col): + return '' + fg = attr + bg = attr -nesting = [] class2str = { 0: 'Universal', @@ -72,6 +71,63 @@ universal2str = { 31: '*****' } + +def niceerror (s): + return bg ('yellow') + fg ('red') + s + attr (0) + +def nicemeaning (s): + return attr ('bold') + s + attr (0) + +def nicetag (ds): + if type (ds) == int: + ds = 'tag 0x%02x' % ds + return fg ('green') + ds + attr (0) + +def nicecontlen (ds, depth): + if type (ds) == int: + ds = str (ds) + return fg ('magenta') + '#' * depth + ds + attr (0) + +def nicetagofs (ds): + if type (ds) == int: + ds = str (ds) + return fg ('cyan') + '@' + ds + attr (0) + +def nicenesting (ad): + if type (ad) == type ([]): + ad = len (ad) + return fg ('light_blue') + '^' + str (ad) + attr (0) + +def niceclass (ds): + if type (ds) == int: + ds = class2str [ds] + return attr ('dim') + ds + attr (0) + +def niceprimconstr (ds): + if type (ds) == bool: + ds = int (ds) + if type (ds) == int: + ds = pc2str [ds] + return attr ('dim') + ds + attr (0) + +der = open (args [0], 'r').read (65537) +ofs = 0 + +def eof (): + global ofs, der + return ofs >= len (der) + +def read1 (): + global ofs, der + if eof (): + print 'ATTEMPTED READ BEYOND EOF (RETURNING 0x00)' + return 0 + else: + ofs = ofs + 1 + return ord (der [ofs-1]) + +nesting = [] + while not eof (): while nesting != [] and ofs >= nesting [-1]: @@ -106,18 +162,18 @@ while not eof (): else: meaning = '[PRIVATE ' + str (tag_num) + ']' - print '%s%s: tag 0x%02x %s%d @%d ^%d, %s, %s' % ( + print ('%s%s: %s %s %s %s, %s, %s' % ( ' ' * len (nesting), - meaning, tag, - '#' * lenlen, leng, - ofs - lenlen - 1, - len (nesting), - class2str [tag_class], - pc2str [tag_pc] ) + nicemeaning (meaning), nicetag (tag), + nicecontlen (leng, lenlen), + nicetagofs (ofs - lenlen - 1), + nicenesting (nesting), + niceclass (tag_class), + niceprimconstr (tag_pc) ) ) if tag_pc == 0 and leng > 0: print ' ' * ( len (nesting) + 1 ), - cstr = '"' + cstr = '' ival = None ostr = '' oval = None @@ -141,11 +197,12 @@ while not eof (): ostr = ostr + '.' + str (oval) oval = 0 leng = leng - 1 - cstr = cstr + '"' if tag == 0x06: - cstr = ostr + cstr = attr ('bold') + ostr + attr (0) elif tag == 0x02: - cstr = str (ival) + cstr = attr ('bold') + str (ival) + attr (0) + else: + cstr = '"' + attr ('bold') + cstr + attr (0) + '"' print '==', cstr if tag_pc != 0: @@ -154,6 +211,6 @@ while not eof (): while nesting != []: if ofs != nesting [-1]: - print 'NESTING NOT ENDED CORRECTLY, OFFSET IS %d INSTEAD OF %d (CONTINUING)' % (ofs, nesting [-1]) + print niceerror ('NESTING NOT ENDED CORRECTLY, OFFSET IS %d INSTEAD OF %d (CONTINUING)' % (ofs, nesting [-1])) nesting.pop () diff --git a/derdump-screenshot.png b/derdump-screenshot.png new file mode 100644 index 0000000..1ba3bc8 Binary files /dev/null and b/derdump-screenshot.png differ