diff options
Diffstat (limited to 'presentty/urwid_fixes.py')
-rw-r--r-- | presentty/urwid_fixes.py | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/presentty/urwid_fixes.py b/presentty/urwid_fixes.py new file mode 100644 index 0000000..c927896 --- /dev/null +++ b/presentty/urwid_fixes.py @@ -0,0 +1,146 @@ + +import urwid +import urwid.display_common +from urwid import version + + +def _attrspec_to_escape_1_3_2(self, a): + """ + Convert AttrSpec instance a to an escape sequence for the terminal + + >>> s = Screen() + >>> s.set_terminal_properties(colors=256) + >>> a2e = s._attrspec_to_escape + >>> a2e(s.AttrSpec('brown', 'dark green')) + '\\x1b[0;33;42m' + >>> a2e(s.AttrSpec('#fea,underline', '#d0d')) + '\\x1b[0;38;5;229;4;48;5;164m' + """ + if self.term == 'fbterm': + fg = escape.ESC + '[1;%d}' % (a.foreground_number,) + bg = escape.ESC + '[2;%d}' % (a.background_number,) + return fg + bg + + if a.foreground_high: + fg = "38;5;%d" % a.foreground_number + elif a.foreground_basic: + if a.foreground_number > 7: + if self.fg_bright_is_bold: + fg = "1;%d" % (a.foreground_number - 8 + 30) + else: + fg = "%d" % (a.foreground_number - 8 + 90) + else: + fg = "%d" % (a.foreground_number + 30) + else: + fg = "39" + st = ("1;" * a.bold + "3;" * a.italics + + "4;" * a.underline + "5;" * a.blink + + "7;" * a.standout + "9;" * a.strikethrough) + if a.background_high: + bg = "48;5;%d" % a.background_number + elif a.background_basic: + if a.background_number > 7: + if self.bg_bright_is_blink: + bg = "5;%d" % (a.background_number - 8 + 40) + else: + # this doesn't work on most terminals + bg = "%d" % (a.background_number - 8 + 100) + else: + bg = "%d" % (a.background_number + 40) + else: + bg = "49" + return urwid.escape.ESC + "[0;%s;%s%sm" % (fg, st, bg) + + +def _attrspec_to_escape_1_3_1(self, a): + """ + Convert AttrSpec instance a to an escape sequence for the terminal + + >>> s = Screen() + >>> s.set_terminal_properties(colors=256) + >>> a2e = s._attrspec_to_escape + >>> a2e(s.AttrSpec('brown', 'dark green')) + '\\x1b[0;33;42m' + >>> a2e(s.AttrSpec('#fea,underline', '#d0d')) + '\\x1b[0;38;5;229;4;48;5;164m' + """ + if a.foreground_high: + fg = "38;5;%d" % a.foreground_number + elif a.foreground_basic: + if a.foreground_number > 7: + if self.fg_bright_is_bold: + fg = "1;%d" % (a.foreground_number - 8 + 30) + else: + fg = "%d" % (a.foreground_number - 8 + 90) + else: + fg = "%d" % (a.foreground_number + 30) + else: + fg = "39" + st = ("1;" * a.bold + "3;" * a.italics + + "4;" * a.underline + "5;" * a.blink + + "7;" * a.standout + "9;" * a.strikethrough) + if a.background_high: + bg = "48;5;%d" % a.background_number + elif a.background_basic: + if a.background_number > 7: + if self.bg_bright_is_blink: + bg = "5;%d" % (a.background_number - 8 + 40) + else: + # this doesn't work on most terminals + bg = "%d" % (a.background_number - 8 + 100) + else: + bg = "%d" % (a.background_number + 40) + else: + bg = "49" + return urwid.escape.ESC + "[0;%s;%s%sm" % (fg, st, bg) + + +def _add_italics(version): + def _foreground(self): + return urwid.display_common.AttrSpec._foreground(self) + (',italics' * urwid.display_common.AttrSpec.italics,) + + urwid.display_common._ITALICS = 0x20000000 + urwid.display_common._FG_MASK |= urwid.display_common._ITALICS + urwid.display_common._ATTRIBUTES['italics'] = urwid.display_common._ITALICS + urwid.display_common.AttrSpec.italics = property(lambda s: s._value & urwid.display_common._ITALICS != 0) + + if version <= (1, 3, 1): + urwid.raw_display.Screen._attrspec_to_escape = _VERSIONED_REPLACEMENTS['_attrspec_to_escape']['1.3.1'] + else: + urwid.raw_display.Screen._attrspec_to_escape = _VERSIONED_REPLACEMENTS['_attrspec_to_escape']['1.3.2'] + + +def _add_strikethrough(version): + def _foreground(self): + return urwid.display_common.AttrSpec._foreground(self) + (',strikethrough' * urwid.display_common.AttrSpec.strikethrough,) + + urwid.display_common._STRIKETHROUGH = 0x40000000 + urwid.display_common._FG_MASK |= urwid.display_common._STRIKETHROUGH + urwid.display_common._ATTRIBUTES['strikethrough'] = urwid.display_common._STRIKETHROUGH + urwid.display_common.AttrSpec.strikethrough = property(lambda s: s._value & urwid.display_common._STRIKETHROUGH != 0) + urwid.display_common.AttrSpec._foreground = _foreground + + if version <= (1, 3, 1): + urwid.raw_display.Screen._attrspec_to_escape = _VERSIONED_REPLACEMENTS['_attrspec_to_escape']['1.3.1'] + else: + urwid.raw_display.Screen._attrspec_to_escape = _VERSIONED_REPLACEMENTS['_attrspec_to_escape']['1.3.2'] + + +def patch_urwid(): + need_features = set() + + # Not in the urwid upstream tree + need_features.add('strikethrough') + + # Currently in the urwid tree but not yet released + if version.VERSION < (1, 3, 1): + need_features.add('italics') + + for feature in need_features: + _FEATURE_DISPATCH[feature](version.VERSION) + + +_VERSIONED_REPLACEMENTS = {'_attrspec_to_escape': {'1.3.1': _attrspec_to_escape_1_3_1, + '1.3.2': _attrspec_to_escape_1_3_2,}} +_FEATURE_DISPATCH = {'strikethrough': _add_strikethrough, + 'italics': _add_italics} |