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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue