From 573bc5969cf143262d73650ae494ee817ec3b045 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Tue, 18 Oct 2022 19:55:51 +1000 Subject: [PATCH] Coding style - Make modified CharStyles more easily. --- termstr/termstr.py | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/termstr/termstr.py b/termstr/termstr.py index 2e402a7..e4bd21b 100644 --- a/termstr/termstr.py +++ b/termstr/termstr.py @@ -118,6 +118,30 @@ class CharStyle: attributes.append("u") return f"" + 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 = []