diff options
author | Toshio Kuratomi <a.badger@gmail.com> | 2017-09-23 22:16:30 -0700 |
---|---|---|
committer | Toshio Kuratomi <a.badger@gmail.com> | 2017-09-23 22:16:30 -0700 |
commit | 3d0b692517be185e825a4e55f485082235999a58 (patch) | |
tree | 4ca10dd48b31a34b9ca0da752ce723e61a7be414 | |
parent | 308a06134d7749638c7ba3afcc4031f31ba09930 (diff) | |
download | presentty-urwid-monkeypatch.tar.gz presentty-urwid-monkeypatch.tar.xz presentty-urwid-monkeypatch.zip |
Monkeypatch urwid to allow for italics and strikethrough formattingpresentty-urwid-monkeypatch
-rw-r--r-- | example/demo.rst | 12 | ||||
-rw-r--r-- | presentty/palette.py | 8 | ||||
-rw-r--r-- | presentty/presentty.py | 3 | ||||
-rw-r--r-- | presentty/rst.py | 7 | ||||
-rw-r--r-- | presentty/urwid_fixes.py | 146 |
5 files changed, 174 insertions, 2 deletions
diff --git a/example/demo.rst b/example/demo.rst index ad8ae77..7680cf4 100644 --- a/example/demo.rst +++ b/example/demo.rst @@ -87,6 +87,18 @@ Line Blocks (From "The Cloud", Percy Bysshe Shelley) +Formatting +========== + +.. role:: strike + +With the proper terminal types, you can format text in a variety of ways: + +* Normal +* *Italic* +* **Bold** +* :strike:`Strikethrough` + Dissolve Transition =================== Transitions may be "dissolve," where one slide cross-fades into the next... diff --git a/presentty/palette.py b/presentty/palette.py index 6079a95..933f19d 100644 --- a/presentty/palette.py +++ b/presentty/palette.py @@ -18,7 +18,9 @@ import urwid DARK_PALETTE = { '_default': urwid.AttrSpec('light gray', 'black'), - 'emphasis': urwid.AttrSpec('bold, light gray', 'black'), + 'emphasis': urwid.AttrSpec('italics, light gray', 'black'), + 'strong': urwid.AttrSpec('bold, light gray', 'black'), + 'strike': urwid.AttrSpec('strikethrough, light gray', 'black'), 'title': urwid.AttrSpec('bold, white', 'black'), 'progressive': urwid.AttrSpec('dark gray', 'black'), @@ -72,7 +74,9 @@ for k, v in DARK_PALETTE.items(): LIGHT_PALETTE.update({ '_default': urwid.AttrSpec('black', 'h15'), - 'emphasis': urwid.AttrSpec('bold, black', 'h15'), + 'emphasis': urwid.AttrSpec('italics, black', 'h15'), + 'strike': urwid.AttrSpec('strikethrough, black', 'h15'), + 'strong': urwid.AttrSpec('bold, black', 'h15'), 'title': urwid.AttrSpec('bold, #000', 'h15'), 'progressive': urwid.AttrSpec('light gray', 'h15'), }) diff --git a/presentty/presentty.py b/presentty/presentty.py index d54d640..7b9864c 100644 --- a/presentty/presentty.py +++ b/presentty/presentty.py @@ -20,6 +20,9 @@ import time import urwid +import urwid_fixes +urwid_fixes.patch_urwid() + import slide import server import rst diff --git a/presentty/rst.py b/presentty/rst.py index 5867ca1..53238e7 100644 --- a/presentty/rst.py +++ b/presentty/rst.py @@ -295,6 +295,12 @@ class UrwidTranslator(docutils.nodes.GenericNodeVisitor): def depart_emphasis(self, node): self.attr.pop() + def visit_strong(self, node): + self.attr.append(self.palette['strong']) + + def depart_strong(self, node): + self.attr.pop() + def visit_inline(self, node): cls = node.get('classes') if not cls: @@ -362,6 +368,7 @@ class UrwidTranslator(docutils.nodes.GenericNodeVisitor): if 'progressive' in node.get('classes'): self.progressives.pop() + class TransitionDirective(docutils.parsers.rst.Directive): required_arguments = 1 option_spec = {'duration': float} 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} |