1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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}
|