Coding style

- Make modified CharStyles more easily.
This commit is contained in:
Andrew Hamilton 2022-10-18 19:55:51 +10:00
parent cad84d9dfb
commit 573bc5969c

View file

@ -118,6 +118,30 @@ class CharStyle:
attributes.append("u") attributes.append("u")
return f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color} attr:{','.join(attributes)}>" return f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color} attr:{','.join(attributes)}>"
def bold(self):
return self.__class__(self.fg_color, self.bg_color, is_bold=True,
is_italic=self.is_italic, is_underlined=self.is_underlined)
def underline(self):
return self.__class__(self.fg_color, self.bg_color, is_bold=self.is_bold,
is_italic=self.is_italic, is_underlined=True)
def italic(self):
return self.__class__(self.fg_color, self.bg_color, is_bold=self.is_bold,
is_italic=True, is_underlined=self.is_underlined)
def fg_color_(self, fg_color):
return self.__class__(fg_color, self.bg_color, is_bold=self.is_bold,
is_italic=self.is_italic, is_underlined=self.is_underlined)
def bg_color_(self, bg_color):
return self.__class__(self.fg_color, bg_color, is_bold=self.is_bold,
is_italic=self.is_italic, is_underlined=self.is_underlined)
def invert(self):
return self.__class__(self.bg_color, self.fg_color, is_bold=self.is_bold,
is_italic=self.is_italic, is_underlined=self.is_underlined)
@staticmethod @staticmethod
def _color_code(color_, is_foreground): def _color_code(color_, is_foreground):
if isinstance(color_, int): if isinstance(color_, int):
@ -375,40 +399,22 @@ class TermStr(collections.UserString):
return self.__class__(self.data, new_style) return self.__class__(self.data, new_style)
def bold(self): def bold(self):
def make_bold(style): return self.transform_style(CharStyle.bold)
return CharStyle(style.fg_color, style.bg_color, is_bold=True,
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(make_bold)
def underline(self): def underline(self):
def make_underlined(style): return self.transform_style(CharStyle.underline)
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 italic(self):
def make_italic(style): return self.transform_style(CharStyle.italic)
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 fg_color(self, fg_color):
def set_fgcolor(style): return self.transform_style(lambda style: style.fg_color_(fg_color))
return CharStyle(fg_color, style.bg_color, is_bold=style.is_bold,
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(set_fgcolor)
def bg_color(self, bg_color): def bg_color(self, bg_color):
def set_bgcolor(style): return self.transform_style(lambda style: style.bg_color_(bg_color))
return CharStyle(style.fg_color, bg_color, is_bold=style.is_bold,
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(set_bgcolor)
def invert(self): def invert(self):
def invert_(style): return self.transform_style(CharStyle.invert_)
return CharStyle(style.bg_color, style.fg_color, is_bold=style.is_bold,
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(invert_)
def as_html(self): def as_html(self):
result = [] result = []