Coding style.
- Increase maximum line length from 80 to 100.
This commit is contained in:
parent
75a028272d
commit
71b9da128b
15 changed files with 404 additions and 696 deletions
|
|
@ -50,16 +50,12 @@ class Color:
|
|||
|
||||
|
||||
def _xterm_colors():
|
||||
result = [
|
||||
(0x00, 0x00, 0x00), (0xcd, 0x00, 0x00), (0x00, 0xcd, 0x00),
|
||||
(0xcd, 0xcd, 0x00), (0x00, 0x00, 0xee), (0xcd, 0x00, 0xcd),
|
||||
(0x00, 0xcd, 0xcd), (0xe5, 0xe5, 0xe5), (0x7f, 0x7f, 0x7f),
|
||||
(0xff, 0x00, 0x00), (0x00, 0xff, 0x00), (0xff, 0xff, 0x00),
|
||||
(0x5c, 0x5c, 0xff), (0xff, 0x00, 0xff), (0x00, 0xff, 0xff),
|
||||
(0xff, 0xff, 0xff)]
|
||||
intensities = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
|
||||
result.extend([(intensities[(i // 36) % 6], intensities[(i // 6) % 6],
|
||||
intensities[i % 6]) for i in range(216)])
|
||||
result = [(0x00, 0x00, 0x00), (0xcd, 0x00, 0x00), (0x00, 0xcd, 0x00), (0xcd, 0xcd, 0x00),
|
||||
(0x00, 0x00, 0xee), (0xcd, 0x00, 0xcd), (0x00, 0xcd, 0xcd), (0xe5, 0xe5, 0xe5),
|
||||
(0x7f, 0x7f, 0x7f), (0xff, 0x00, 0x00), (0x00, 0xff, 0x00), (0xff, 0xff, 0x00),
|
||||
(0x5c, 0x5c, 0xff), (0xff, 0x00, 0xff), (0x00, 0xff, 0xff), (0xff, 0xff, 0xff)]
|
||||
grad = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
|
||||
result.extend([(grad[(i // 36) % 6], grad[(i // 6) % 6], grad[i % 6]) for i in range(216)])
|
||||
result.extend([(8 + i * 10, 8 + i * 10, 8 + i * 10) for i in range(24)])
|
||||
return result
|
||||
|
||||
|
|
@ -93,13 +89,11 @@ class CharStyle:
|
|||
return CharStyle._POOL[key]
|
||||
except KeyError:
|
||||
obj = object.__new__(cls)
|
||||
obj.fg_color, obj.bg_color, obj.is_bold, obj.is_italic, \
|
||||
obj.is_underlined = key
|
||||
obj.fg_color, obj.bg_color, obj.is_bold, obj.is_italic, obj.is_underlined = key
|
||||
return CharStyle._POOL.setdefault(key, obj)
|
||||
|
||||
def __getnewargs__(self):
|
||||
return (self.fg_color, self.bg_color, self.is_bold, self.is_italic,
|
||||
self.is_underlined)
|
||||
return self.fg_color, self.bg_color, self.is_bold, self.is_italic, self.is_underlined
|
||||
|
||||
def __getstate__(self):
|
||||
state = self.__dict__.copy()
|
||||
|
|
@ -118,8 +112,7 @@ class CharStyle:
|
|||
attributes.append("i")
|
||||
if self.is_underlined:
|
||||
attributes.append("u")
|
||||
return (f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color}"
|
||||
f" attr:{','.join(attributes)}>")
|
||||
return f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color} attr:{','.join(attributes)}>"
|
||||
|
||||
def _color_code(self, color_, is_foreground):
|
||||
if isinstance(color_, int):
|
||||
|
|
@ -144,12 +137,9 @@ class CharStyle:
|
|||
def as_html(self):
|
||||
bold_code = "font-weight:bold; " if self.is_bold else ""
|
||||
italic_code = "font-style:italic; " if self.is_italic else ""
|
||||
underline_code = ("text-decoration:underline; "
|
||||
if self.is_underlined else "")
|
||||
fg_color = (self.fg_color if type(self.fg_color) == tuple
|
||||
else XTERM_COLORS[self.fg_color])
|
||||
bg_color = (self.bg_color if type(self.bg_color) == tuple
|
||||
else XTERM_COLORS[self.bg_color])
|
||||
underline_code = "text-decoration:underline; " if self.is_underlined else ""
|
||||
fg_color = self.fg_color if type(self.fg_color) == tuple else XTERM_COLORS[self.fg_color]
|
||||
bg_color = self.bg_color if type(self.bg_color) == tuple else XTERM_COLORS[self.bg_color]
|
||||
return (f"<style>.S{id(self)} {{font-size:80%%; color:rgb{fg_color!r};"
|
||||
f" background-color:rgb{bg_color!r}; "
|
||||
f"{bold_code}{italic_code}{underline_code}}}</style>")
|
||||
|
|
@ -227,23 +217,19 @@ class TermStr(collections.UserString):
|
|||
bg_color = int(codes[index+1])
|
||||
codes[index+1:index+2] = []
|
||||
elif code == "2" and previous_code == "38": # rgb fg color
|
||||
fg_color = tuple(int(component)
|
||||
for component in codes[index+1:index+4])
|
||||
fg_color = tuple(int(component) for component in codes[index+1:index+4])
|
||||
codes[index+1:index+4] = []
|
||||
elif code == "2" and previous_code == "48": # rgb bg color
|
||||
bg_color = tuple(int(component)
|
||||
for component in codes[index+1:index+4])
|
||||
bg_color = tuple(int(component) for component in codes[index+1:index+4])
|
||||
codes[index+1:index+4] = []
|
||||
previous_code = code
|
||||
result_parts.append(cls(part[end_index+1:],
|
||||
CharStyle(fg_color, bg_color, is_bold,
|
||||
is_italic, is_underlined)))
|
||||
result_parts.append(cls(part[end_index+1:], CharStyle(fg_color, bg_color, is_bold,
|
||||
is_italic, is_underlined)))
|
||||
return cls("").join(result_parts)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (self is other or
|
||||
(isinstance(other, self.__class__) and
|
||||
self.data == other.data and self.style == other.style))
|
||||
return (self is other or (isinstance(other, self.__class__) and
|
||||
self.data == other.data and self.style == other.style))
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
|
@ -266,10 +252,8 @@ class TermStr(collections.UserString):
|
|||
return result
|
||||
|
||||
def __str__(self):
|
||||
return "".join(_join_lists(
|
||||
[style.code_for_term, self.data[start_index:end_index]]
|
||||
for style, start_index, end_index in self._partition_style) +
|
||||
[ESC + NORMAL])
|
||||
return "".join(_join_lists([style.code_for_term, self.data[start_index:end_index]]
|
||||
for style, start_index, end_index in self._partition_style) + [ESC + NORMAL])
|
||||
|
||||
def __repr__(self):
|
||||
return f"<TermStr: {self.data!r}>"
|
||||
|
|
@ -302,8 +286,7 @@ class TermStr(collections.UserString):
|
|||
return self.__class__(result, self.style[index])
|
||||
|
||||
def join(self, parts):
|
||||
parts = [TermStr(part) if isinstance(part, str) else part
|
||||
for part in parts]
|
||||
parts = [TermStr(part) if isinstance(part, str) else part for part in parts]
|
||||
joined_style = _join_lists(self.style + part.style for part in parts)
|
||||
return self.__class__(self.data.join(part.data for part in parts),
|
||||
tuple(joined_style[len(self.style):]))
|
||||
|
|
@ -356,50 +339,43 @@ class TermStr(collections.UserString):
|
|||
if left_width < 1:
|
||||
return self
|
||||
return (self.__class__(fillchar * left_width) + self +
|
||||
self.__class__(fillchar *
|
||||
(width - left_width - len(self.data))))
|
||||
self.__class__(fillchar * (width - left_width - len(self.data))))
|
||||
|
||||
# Below are extra methods useful for termstrs.
|
||||
|
||||
def transform_style(self, transform_func):
|
||||
new_style = tuple(_join_lists(
|
||||
[transform_func(style)] * (end_index - start_index)
|
||||
for style, start_index, end_index in self._partition_style))
|
||||
new_style = tuple(_join_lists([transform_func(style)] * (end_index - start_index)
|
||||
for style, start_index, end_index in self._partition_style))
|
||||
return self.__class__(self.data, new_style)
|
||||
|
||||
def bold(self):
|
||||
def make_bold(style):
|
||||
return CharStyle(style.fg_color, style.bg_color, is_bold=True,
|
||||
is_italic=style.is_italic,
|
||||
is_underlined=style.is_underlined)
|
||||
is_italic=style.is_italic, is_underlined=style.is_underlined)
|
||||
return self.transform_style(make_bold)
|
||||
|
||||
def underline(self):
|
||||
def make_underlined(style):
|
||||
return CharStyle(style.fg_color, style.bg_color,
|
||||
is_bold=style.is_bold, is_italic=style.is_italic,
|
||||
is_underlined=True)
|
||||
return CharStyle(style.fg_color, style.bg_color, is_bold=style.is_bold,
|
||||
is_italic=style.is_italic, is_underlined=True)
|
||||
return self.transform_style(make_underlined)
|
||||
|
||||
def italic(self):
|
||||
def make_italic(style):
|
||||
return CharStyle(style.fg_color, style.bg_color,
|
||||
is_bold=style.is_bold, is_italic=True,
|
||||
is_underlined=style.is_underlined)
|
||||
return CharStyle(style.fg_color, style.bg_color, is_bold=style.is_bold,
|
||||
is_italic=True, is_underlined=style.is_underlined)
|
||||
return self.transform_style(make_italic)
|
||||
|
||||
def fg_color(self, fg_color):
|
||||
def set_fgcolor(style):
|
||||
return CharStyle(fg_color, style.bg_color, is_bold=style.is_bold,
|
||||
is_italic=style.is_italic,
|
||||
is_underlined=style.is_underlined)
|
||||
is_italic=style.is_italic, is_underlined=style.is_underlined)
|
||||
return self.transform_style(set_fgcolor)
|
||||
|
||||
def bg_color(self, bg_color):
|
||||
def set_bgcolor(style):
|
||||
return CharStyle(style.fg_color, bg_color, is_bold=style.is_bold,
|
||||
is_italic=style.is_italic,
|
||||
is_underlined=style.is_underlined)
|
||||
is_italic=style.is_italic, is_underlined=style.is_underlined)
|
||||
return self.transform_style(set_bgcolor)
|
||||
|
||||
def as_html(self):
|
||||
|
|
|
|||
|
|
@ -22,12 +22,9 @@ class XtermColorsTests(unittest.TestCase):
|
|||
self.assertEqual(termstr.XTERM_COLORS[255], (238, 238, 238))
|
||||
|
||||
def test_closest_color_index(self):
|
||||
self.assertEqual(termstr.closest_color_index(
|
||||
(0, 0, 0), termstr.XTERM_COLORS), 0)
|
||||
self.assertEqual(termstr.closest_color_index(
|
||||
(255, 255, 255), termstr.XTERM_COLORS), 15)
|
||||
self.assertEqual(termstr.closest_color_index(
|
||||
(135, 135, 1), termstr.XTERM_COLORS), 100)
|
||||
self.assertEqual(termstr.closest_color_index((0, 0, 0), termstr.XTERM_COLORS), 0)
|
||||
self.assertEqual(termstr.closest_color_index((255, 255, 255), termstr.XTERM_COLORS), 15)
|
||||
self.assertEqual(termstr.closest_color_index((135, 135, 1), termstr.XTERM_COLORS), 100)
|
||||
|
||||
|
||||
class CharStyleTests(unittest.TestCase):
|
||||
|
|
@ -48,12 +45,10 @@ class CharStyleTests(unittest.TestCase):
|
|||
self.assertTrue(style is loaded_style)
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(self.style),
|
||||
"<CharStyle: fg:(255, 255, 255) bg:(0, 0, 0) attr:>")
|
||||
self.assertEqual(repr(self.style), "<CharStyle: fg:(255, 255, 255) bg:(0, 0, 0) attr:>")
|
||||
|
||||
def test_code_for_term(self):
|
||||
self.assertEqual(self.style.code_for_term,
|
||||
"\x1b[m\x1b[38;2;255;255;255m\x1b[48;2;0;0;0m")
|
||||
self.assertEqual(self.style.code_for_term, "\x1b[m\x1b[38;2;255;255;255m\x1b[48;2;0;0;0m")
|
||||
|
||||
|
||||
class TermStrTests(unittest.TestCase):
|
||||
|
|
@ -65,8 +60,7 @@ class TermStrTests(unittest.TestCase):
|
|||
foo_bold = termstr.TermStr("foo", bold_style)
|
||||
self.assertEqual(repr(foo_bold), "<TermStr: 'foo'>")
|
||||
self.assertEqual(foo + "bar", termstr.TermStr("foobar"))
|
||||
self.assertEqual(foo + termstr.TermStr("bar"),
|
||||
termstr.TermStr("foobar"))
|
||||
self.assertEqual(foo + termstr.TermStr("bar"), termstr.TermStr("foobar"))
|
||||
self.assertEqual("bar" + foo, termstr.TermStr("barfoo"))
|
||||
self.assertFalse(foo == foo_bold)
|
||||
self.assertFalse(foo_bold == foo)
|
||||
|
|
@ -88,11 +82,9 @@ class TermStrTests(unittest.TestCase):
|
|||
self.assertEqual(foo.find("oo"), 1)
|
||||
self.assertEqual(termstr.TermStr("fo") * 2, termstr.TermStr("fofo"))
|
||||
self.assertEqual(2 * termstr.TermStr("fo"), termstr.TermStr("fofo"))
|
||||
self.assertEqual(foobar.split("b"), [termstr.TermStr("foo"),
|
||||
termstr.TermStr("ar")])
|
||||
self.assertEqual(foobar.split("b"), [termstr.TermStr("foo"), termstr.TermStr("ar")])
|
||||
self.assertEqual(foo.join(["C", "D"]), termstr.TermStr("CfooD"))
|
||||
self.assertEqual(foo.join(["C", termstr.TermStr("D")]),
|
||||
termstr.TermStr("CfooD"))
|
||||
self.assertEqual(foo.join(["C", termstr.TermStr("D")]), termstr.TermStr("CfooD"))
|
||||
self.assertEqual(foo.join([]), termstr.TermStr(""))
|
||||
self.assertEqual(foo.join(["C"]), termstr.TermStr("C"))
|
||||
bar = termstr.TermStr("bar", bold_style)
|
||||
|
|
@ -107,16 +99,14 @@ class TermStrTests(unittest.TestCase):
|
|||
self.assertEqual(termstr.TermStr("FOO").lower(), foo)
|
||||
self.assertEqual(termstr.TermStr("FOO", bold_style).lower(), foo_bold)
|
||||
self.assertEqual(termstr.TermStr("FOO").swapcase(), foo)
|
||||
self.assertEqual(termstr.TermStr("FOO", bold_style).swapcase(),
|
||||
foo_bold)
|
||||
self.assertEqual(termstr.TermStr("FOO", bold_style).swapcase(), foo_bold)
|
||||
phrase = termstr.TermStr("foo bar")
|
||||
self.assertEqual(phrase.title(), termstr.TermStr("Foo Bar"))
|
||||
self.assertEqual(phrase.capitalize(), termstr.TermStr("Foo bar"))
|
||||
self.assertEqual(foo.upper(), termstr.TermStr("FOO"))
|
||||
self.assertEqual(foo_bold.center(0), foo_bold)
|
||||
self.assertEqual(foo_bold.center(7),
|
||||
termstr.TermStr(" ") + foo_bold +
|
||||
termstr.TermStr(" "))
|
||||
termstr.TermStr(" ") + foo_bold + termstr.TermStr(" "))
|
||||
self.assertEqual(foo_bold.ljust(0), foo_bold)
|
||||
self.assertEqual(foo_bold.ljust(5), foo_bold + termstr.TermStr(" "))
|
||||
self.assertEqual(foo_bold.rjust(0), foo_bold)
|
||||
|
|
@ -129,16 +119,15 @@ class TermStrTests(unittest.TestCase):
|
|||
|
||||
def test_from_term(self):
|
||||
def test_round_trip(term_str):
|
||||
self.assertEqual(termstr.TermStr.from_term(str(term_str)),
|
||||
term_str)
|
||||
self.assertEqual(termstr.TermStr.from_term(str(term_str)), term_str)
|
||||
|
||||
test_round_trip(termstr.TermStr("foo"))
|
||||
test_round_trip(termstr.TermStr("foo").bold())
|
||||
test_round_trip(termstr.TermStr("foo").underline())
|
||||
test_round_trip(termstr.TermStr("foo").italic())
|
||||
test_round_trip(termstr.TermStr("foo").fg_color(termstr.Color.red))
|
||||
test_round_trip(termstr.TermStr("foo").fg_color(termstr.Color.red).
|
||||
bg_color(termstr.Color.green))
|
||||
test_round_trip(
|
||||
termstr.TermStr("foo").fg_color(termstr.Color.red).bg_color(termstr.Color.green))
|
||||
test_round_trip(termstr.TermStr("foo").fg_color(1))
|
||||
test_round_trip(termstr.TermStr("foo").bg_color(10))
|
||||
self.assertEqual(
|
||||
|
|
@ -147,14 +136,10 @@ class TermStrTests(unittest.TestCase):
|
|||
termstr.TermStr("foo").fg_color(3))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[45mfoo"),
|
||||
termstr.TermStr("foo").bg_color(5))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[45mfoo" +
|
||||
ESC + "[mbar"),
|
||||
termstr.TermStr("foo").bg_color(5) +
|
||||
termstr.TermStr("bar"))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[45mfoo" +
|
||||
ESC + "[0mbar"),
|
||||
termstr.TermStr("foo").bg_color(5) +
|
||||
termstr.TermStr("bar"))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[45mfoo" + ESC + "[mbar"),
|
||||
termstr.TermStr("foo").bg_color(5) + termstr.TermStr("bar"))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[45mfoo" + ESC + "[0mbar"),
|
||||
termstr.TermStr("foo").bg_color(5) + termstr.TermStr("bar"))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[1;3mfoo"),
|
||||
termstr.TermStr("foo").bold().italic())
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[01mfoo"),
|
||||
|
|
@ -165,12 +150,9 @@ class TermStrTests(unittest.TestCase):
|
|||
termstr.TermStr("foo").fg_color(13))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "[105mfoo"),
|
||||
termstr.TermStr("foo").bg_color(13))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "(B" +
|
||||
ESC + "[mfoo"),
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "(B" + ESC + "[mfoo"),
|
||||
termstr.TermStr("foo"))
|
||||
self.assertEqual(
|
||||
termstr.TermStr.from_term(ESC + "39;49;00mfoo"),
|
||||
termstr.TermStr("foo"))
|
||||
self.assertEqual(termstr.TermStr.from_term(ESC + "39;49;00mfoo"), termstr.TermStr("foo"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue