Coding style.
- termstr no longer depends on terminal. - Moved some code from terminal to termstr.
This commit is contained in:
parent
d540636fd5
commit
5341d91592
2 changed files with 31 additions and 32 deletions
|
|
@ -8,12 +8,6 @@ import termios
|
||||||
ESC = "\x1b"
|
ESC = "\x1b"
|
||||||
MOUSE = ESC + "[M"
|
MOUSE = ESC + "[M"
|
||||||
|
|
||||||
normal = "[m"
|
|
||||||
bold = "[1m"
|
|
||||||
italic = "[3m"
|
|
||||||
standout = "[7m"
|
|
||||||
underline = "[4m"
|
|
||||||
|
|
||||||
UP_KEY = ESC + "[A"
|
UP_KEY = ESC + "[A"
|
||||||
DOWN_KEY = ESC + "[B"
|
DOWN_KEY = ESC + "[B"
|
||||||
RIGHT_KEY = ESC + "[C"
|
RIGHT_KEY = ESC + "[C"
|
||||||
|
|
@ -24,14 +18,6 @@ HOME_KEY = ESC + "[H"
|
||||||
END_KEY = ESC + "[F"
|
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):
|
def move(x, y):
|
||||||
return ESC + f"[{y + 1:d};{x + 1:d}H"
|
return ESC + f"[{y + 1:d};{x + 1:d}H"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,27 @@ import pygments.formatters.terminal256
|
||||||
import cwcwidth
|
import cwcwidth
|
||||||
|
|
||||||
import termstr.ColorMap
|
import termstr.ColorMap
|
||||||
import termstr.terminal as terminal
|
|
||||||
|
|
||||||
|
ESC = "\x1b"
|
||||||
|
|
||||||
|
NORMAL = "[m"
|
||||||
|
BOLD = "[1m"
|
||||||
|
ITALIC = "[3m"
|
||||||
|
UNDERLINE = "[4m"
|
||||||
|
|
||||||
|
|
||||||
xterm_colormap = termstr.ColorMap.XTermColorMap()
|
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()
|
@functools.lru_cache()
|
||||||
def xterm_color_to_rgb(color_index):
|
def xterm_color_to_rgb(color_index):
|
||||||
return termstr.ColorMap._rgb(xterm_colormap.colors[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}"
|
return (f"<CharStyle: fg:{self.fg_color} bg:{self.bg_color}"
|
||||||
f" attr:{','.join(attributes)}>")
|
f" attr:{','.join(attributes)}>")
|
||||||
|
|
||||||
def _color_code(self, color, is_foreground):
|
def _color_code(self, color_, is_foreground):
|
||||||
if isinstance(color, int):
|
if isinstance(color_, int):
|
||||||
return terminal.color(color, is_foreground)
|
return color(color_, is_foreground)
|
||||||
else: # true color
|
else: # true color
|
||||||
if os.environ.get("TERM", None) == "xterm":
|
if os.environ.get("TERM", None) == "xterm":
|
||||||
closest_color = self._TERMINAL256_FORMATTER._closest_color(
|
closest_color = self._TERMINAL256_FORMATTER._closest_color(
|
||||||
*color)
|
*color_)
|
||||||
return terminal.color(closest_color, is_foreground)
|
return color(closest_color, is_foreground)
|
||||||
else:
|
else:
|
||||||
return terminal.rgb_color(color, is_foreground)
|
return rgb_color(color_, is_foreground)
|
||||||
|
|
||||||
@functools.cached_property
|
@functools.cached_property
|
||||||
def code_for_term(self):
|
def code_for_term(self):
|
||||||
fg_termcode = terminal.ESC + self._color_code(self.fg_color, True)
|
fg_termcode = ESC + self._color_code(self.fg_color, True)
|
||||||
bg_termcode = terminal.ESC + self._color_code(self.bg_color, False)
|
bg_termcode = ESC + self._color_code(self.bg_color, False)
|
||||||
bold_code = (terminal.ESC + terminal.bold) if self.is_bold else ""
|
bold_code = (ESC + BOLD) if self.is_bold else ""
|
||||||
italic_code = ((terminal.ESC + terminal.italic)
|
italic_code = ((ESC + ITALIC) if self.is_italic else "")
|
||||||
if self.is_italic else "")
|
underline_code = ((ESC + UNDERLINE) if self.is_underlined else "")
|
||||||
underline_code = ((terminal.ESC + terminal.underline)
|
return "".join([ESC, NORMAL, fg_termcode, bg_termcode, bold_code,
|
||||||
if self.is_underlined else "")
|
italic_code, underline_code])
|
||||||
return "".join([terminal.ESC, terminal.normal, fg_termcode,
|
|
||||||
bg_termcode, bold_code, italic_code, underline_code])
|
|
||||||
|
|
||||||
def as_html(self):
|
def as_html(self):
|
||||||
bold_code = "font-weight:bold; " if self.is_bold else ""
|
bold_code = "font-weight:bold; " if self.is_bold else ""
|
||||||
|
|
@ -155,7 +168,7 @@ class TermStr(collections.UserString):
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_term(cls, data):
|
def from_term(cls, data):
|
||||||
data = data.expandtabs(tabsize=4)
|
data = data.expandtabs(tabsize=4)
|
||||||
parts = data.split(terminal.ESC)
|
parts = data.split(ESC)
|
||||||
fg_color, bg_color = None, None
|
fg_color, bg_color = None, None
|
||||||
is_bold, is_italic, is_underlined = False, False, False
|
is_bold, is_italic, is_underlined = False, False, False
|
||||||
result_parts = [parts[0]]
|
result_parts = [parts[0]]
|
||||||
|
|
@ -241,7 +254,7 @@ class TermStr(collections.UserString):
|
||||||
return "".join(_join_lists(
|
return "".join(_join_lists(
|
||||||
[style.code_for_term, self.data[start_index:end_index]]
|
[style.code_for_term, self.data[start_index:end_index]]
|
||||||
for style, start_index, end_index in self._partition_style) +
|
for style, start_index, end_index in self._partition_style) +
|
||||||
[terminal.ESC + terminal.normal])
|
[ESC + NORMAL])
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<TermStr: {self.data!r}>"
|
return f"<TermStr: {self.data!r}>"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue