colorize_tokens#
- hexrec.base.colorize_tokens(tokens, altdata=True)[source]#
Prepends ANSI color codes to record field tokens.
For each token within tokens, its key is used to look up the ANSI color code from
TOKEN_COLOR_CODES
. The retrieved code (byte string) is prepended to the token. All the modified tokens are then collected and returned.- Parameters:
tokens (dict) – A mapping of each token key name to token byte string.
altdata (bool) – If true, it alternates each byte (two hex digits) between the ANSI color codes mapped with keys
data
(even byte index) anddataalt
(odd byte index). If false, only thedata
code is prepended.
- Returns:
dict – tokens with prepended ANSI color codes.
Examples
>>> from hexrec.base import colorize_tokens >>> from hexrec import IhexFile >>> from pprint import pprint
>>> record = IhexFile.Record.create_end_of_file() >>> tokens = record.to_tokens() >>> pprint(tokens) {'address': b'0000', 'after': b'', 'before': b'', 'begin': b':', 'checksum': b'FF', 'count': b'00', 'data': b'', 'end': b'\r\n', 'tag': b'01'} >>> colorized = colorize_tokens(tokens) >>> pprint(colorized) {'<': b'\x1b[0m', '>': b'\x1b[0m', 'address': b'\x1b[31m0000', 'begin': b'\x1b[33m:', 'checksum': b'\x1b[35mFF', 'count': b'\x1b[34m00', 'end': b'\x1b[0m\r\n', 'tag': b'\x1b[32m01'}