Coding style.

- termstr no longer depends on terminal.
  - Moved some code from terminal to termstr.
This commit is contained in:
Andrew Hamilton 2021-11-03 01:26:06 +10:00
parent d540636fd5
commit 5341d91592
2 changed files with 31 additions and 32 deletions

View file

@ -8,12 +8,6 @@ import termios
ESC = "\x1b"
MOUSE = ESC + "[M"
normal = "[m"
bold = "[1m"
italic = "[3m"
standout = "[7m"
underline = "[4m"
UP_KEY = ESC + "[A"
DOWN_KEY = ESC + "[B"
RIGHT_KEY = ESC + "[C"
@ -24,14 +18,6 @@ HOME_KEY = ESC + "[H"
END_KEY = ESC + "[F"
def color(color_number, is_foreground):
return f"[{'38' if is_foreground else '48'};5;{color_number:d}m"
def rgb_color(rgb, is_foreground):
return f"[{'38' if is_foreground else '48'};2;" + "%i;%i;%im" % rgb
def move(x, y):
return ESC + f"[{y + 1:d};{x + 1:d}H"

View file

@ -11,12 +11,27 @@ import pygments.formatters.terminal256
import cwcwidth
import termstr.ColorMap
import termstr.terminal as terminal
ESC = "\x1b"
NORMAL = "[m"
BOLD = "[1m"
ITALIC = "[3m"
UNDERLINE = "[4m"
xterm_colormap = termstr.ColorMap.XTermColorMap()
def color(color_number, is_foreground):
return f"[{'38' if is_foreground else '48'};5;{color_number:d}m"
def rgb_color(rgb, is_foreground):
return f"[{'38' if is_foreground else '48'};2;" + "%i;%i;%im" % rgb
@functools.lru_cache()
def xterm_color_to_rgb(color_index):
return termstr.ColorMap._rgb(xterm_colormap.colors[color_index])
@ -90,28 +105,26 @@ class CharStyle:
return (f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color}"
f" attr:{','.join(attributes)}>")
def _color_code(self, color, is_foreground):
if isinstance(color, int):
return terminal.color(color, is_foreground)
def _color_code(self, color_, is_foreground):
if isinstance(color_, int):
return color(color_, is_foreground)
else: # true color
if os.environ.get("TERM", None) == "xterm":
closest_color = self._TERMINAL256_FORMATTER._closest_color(
*color)
return terminal.color(closest_color, is_foreground)
*color_)
return color(closest_color, is_foreground)
else:
return terminal.rgb_color(color, is_foreground)
return rgb_color(color_, is_foreground)
@functools.cached_property
def code_for_term(self):
fg_termcode = terminal.ESC + self._color_code(self.fg_color, True)
bg_termcode = terminal.ESC + self._color_code(self.bg_color, False)
bold_code = (terminal.ESC + terminal.bold) if self.is_bold else ""
italic_code = ((terminal.ESC + terminal.italic)
if self.is_italic else "")
underline_code = ((terminal.ESC + terminal.underline)
if self.is_underlined else "")
return "".join([terminal.ESC, terminal.normal, fg_termcode,
bg_termcode, bold_code, italic_code, underline_code])
fg_termcode = ESC + self._color_code(self.fg_color, True)
bg_termcode = ESC + self._color_code(self.bg_color, False)
bold_code = (ESC + BOLD) if self.is_bold else ""
italic_code = ((ESC + ITALIC) if self.is_italic else "")
underline_code = ((ESC + UNDERLINE) if self.is_underlined else "")
return "".join([ESC, NORMAL, fg_termcode, bg_termcode, bold_code,
italic_code, underline_code])
def as_html(self):
bold_code = "font-weight:bold; " if self.is_bold else ""
@ -155,7 +168,7 @@ class TermStr(collections.UserString):
@classmethod
def from_term(cls, data):
data = data.expandtabs(tabsize=4)
parts = data.split(terminal.ESC)
parts = data.split(ESC)
fg_color, bg_color = None, None
is_bold, is_italic, is_underlined = False, False, False
result_parts = [parts[0]]
@ -241,7 +254,7 @@ class TermStr(collections.UserString):
return "".join(_join_lists(
[style.code_for_term, self.data[start_index:end_index]]
for style, start_index, end_index in self._partition_style) +
[terminal.ESC + terminal.normal])
[ESC + NORMAL])
def __repr__(self):
return f"<TermStr: {self.data!r}>"