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")
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
def _color_code(color_, is_foreground):
if isinstance(color_, int):
@ -375,40 +399,22 @@ class TermStr(collections.UserString):
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)
return self.transform_style(make_bold)
return self.transform_style(CharStyle.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 self.transform_style(make_underlined)
return self.transform_style(CharStyle.underline)
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 self.transform_style(make_italic)
return self.transform_style(CharStyle.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)
return self.transform_style(set_fgcolor)
return self.transform_style(lambda style: style.fg_color_(fg_color))
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)
return self.transform_style(set_bgcolor)
return self.transform_style(lambda style: style.bg_color_(bg_color))
def invert(self):
def invert_(style):
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_)
return self.transform_style(CharStyle.invert_)
def as_html(self):
result = []