Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions PyTermStylePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,6 @@ def _apply_code(self, code):
self._applied_codes.append(code)
return self

for color_name, color_code in _COLORS.items():
exec(f"def {color_name}(self): return self._apply_code(self._COLORS['{color_name}'])")

for bg_color_name, bg_color_code in _BG_COLORS.items():
exec(f"def {bg_color_name}(self): return self._apply_code(self._BG_COLORS['{bg_color_name}'])")

for format_name, format_code in _FORMATS.items():
exec(f"def {format_name}(self): return self._apply_code(self._FORMATS['{format_name}'])")

def render(self):
"""
Renders the text with all applied ANSI escape codes and resets the style.
Expand All @@ -136,6 +127,20 @@ def __str__(self):
"""
return self.render()

def _make_method(code: str):
def _method(self):
return self._apply_code(code)
return _method

for _name, _code in TermStyle._COLORS.items():
setattr(TermStyle, _name, _make_method(_code))

for _name, _code in TermStyle._BG_COLORS.items():
setattr(TermStyle, _name, _make_method(_code))

for _name, _code in TermStyle._FORMATS.items():
setattr(TermStyle, _name, _make_method(_code))

TermStyle._initialize_ansi_support_check()

THEMES = {
Expand Down