From 08b4abda1aa520600013c8e47d6b1d5af5630902 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Wed, 14 Dec 2011 19:32:07 -0500 Subject: [PATCH] Add color decoding by dmazary. Thanks! --- xonstat/util.py | 72 +++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/xonstat/util.py b/xonstat/util.py index 15565db..b842dc0 100755 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,8 +1,8 @@ import re from datetime import datetime -# Map of special chars to ascii from Quake's console.c. -qfont_table = [ +# Map of special chars to ascii from Darkplace's console.c. +_qfont_table = [ '\0', '#', '#', '#', '#', '.', '#', '#', '#', '\t', '\n', '#', ' ', '\r', '.', '.', '[', ']', '0', '1', '2', '3', '4', '5', @@ -38,57 +38,47 @@ qfont_table = [ 'x', 'y', 'z', '{', '|', '}', '~', '<' ] +# Hex-colored spans for decimal color codes ^0 - ^9 +_dec_spans = [ + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" +] + +# Color code patterns +_all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})') +_dec_colors = re.compile(r'\^(\d)') +_hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])') + def qfont_decode(qstr=''): + """ Convert the qfont characters in a string to ascii. """ - Convert the qfont characters in a string to ascii. - """ - if qstr is None: - qstr = '' - chars = [] for c in qstr: if c >= u'\ue000' and c <= u'\ue0ff': - c = qfont_table[ord(c) - 0xe000] + c = _qfont_table[ord(c) - 0xe000] chars.append(c) return ''.join(chars) -def strip_colors(str=''): - if str is None: - str = '' - - str = re.sub(r'\^x\w\w\w', '', str) - str = re.sub(r'\^\d', '', str) - return str - - -def html_colors(str=''): - if str is None: - str = '' - - orig = str - - # "downsample" the given UTF-8 characters to ASCII - str = qfont_decode(str) - - str = re.sub(r'\^x(\w)(\w)(\w)', - "", str) - str = re.sub(r'\^1', "", str) - str = re.sub(r'\^2', "", str) - str = re.sub(r'\^3', "", str) - str = re.sub(r'\^4', "", str) - str = re.sub(r'\^5', "", str) - str = re.sub(r'\^6', "", str) - str = re.sub(r'\^7', "", str) - str = re.sub(r'\^8', "", str) - str = re.sub(r'\^9', "", str) - str = re.sub(r'\^0', "", str) +def strip_colors(qstr=''): + return _all_colors.sub('', qstr) - for span in range(len(re.findall(r'\^x\w\w\w|\^\d', orig))): - str += "" - return str +def html_colors(qstr=''): + def dec_repl(match): + return _dec_spans[int(match.group(1))] + html = _dec_colors.sub(dec_repl, qstr) + html = _hex_colors.sub(r"", html) + return html + "" * len(_all_colors.findall(qstr)) def page_url(page): -- 2.39.2