2021-10-08 20:23:15 -04:00
|
|
|
import functools
|
2021-09-22 10:12:04 -04:00
|
|
|
from threading import Lock
|
2021-10-20 12:37:32 -04:00
|
|
|
|
2022-04-11 18:32:57 -04:00
|
|
|
from .utils import supports_terminal_sequences, write_string
|
2021-10-20 12:37:32 -04:00
|
|
|
|
|
|
|
CONTROL_SEQUENCES = {
|
|
|
|
'DOWN': '\n',
|
|
|
|
'UP': '\033[A',
|
|
|
|
'ERASE_LINE': '\033[K',
|
|
|
|
'RESET': '\033[0m',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_COLORS = {
|
|
|
|
'BLACK': '0',
|
|
|
|
'RED': '1',
|
|
|
|
'GREEN': '2',
|
|
|
|
'YELLOW': '3',
|
|
|
|
'BLUE': '4',
|
|
|
|
'PURPLE': '5',
|
|
|
|
'CYAN': '6',
|
|
|
|
'WHITE': '7',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_TEXT_STYLES = {
|
|
|
|
'NORMAL': '0',
|
|
|
|
'BOLD': '1',
|
|
|
|
'UNDERLINED': '4',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_text(text, f):
|
2021-10-23 10:29:52 -04:00
|
|
|
'''
|
|
|
|
@param f String representation of formatting to apply in the form:
|
|
|
|
[style] [light] font_color [on [light] bg_color]
|
2022-08-14 08:04:13 -04:00
|
|
|
E.g. "red", "bold green on light blue"
|
2021-10-23 10:29:52 -04:00
|
|
|
'''
|
2021-10-20 12:37:32 -04:00
|
|
|
f = f.upper()
|
|
|
|
tokens = f.strip().split()
|
|
|
|
|
|
|
|
bg_color = ''
|
|
|
|
if 'ON' in tokens:
|
|
|
|
if tokens[-1] == 'ON':
|
|
|
|
raise SyntaxError(f'Empty background format specified in {f!r}')
|
|
|
|
if tokens[-1] not in _COLORS:
|
|
|
|
raise SyntaxError(f'{tokens[-1]} in {f!r} must be a color')
|
|
|
|
bg_color = f'4{_COLORS[tokens.pop()]}'
|
|
|
|
if tokens[-1] == 'LIGHT':
|
|
|
|
bg_color = f'0;10{bg_color[1:]}'
|
|
|
|
tokens.pop()
|
|
|
|
if tokens[-1] != 'ON':
|
|
|
|
raise SyntaxError(f'Invalid format {f.split(" ON ", 1)[1]!r} in {f!r}')
|
|
|
|
bg_color = f'\033[{bg_color}m'
|
|
|
|
tokens.pop()
|
|
|
|
|
|
|
|
if not tokens:
|
|
|
|
fg_color = ''
|
|
|
|
elif tokens[-1] not in _COLORS:
|
|
|
|
raise SyntaxError(f'{tokens[-1]} in {f!r} must be a color')
|
|
|
|
else:
|
|
|
|
fg_color = f'3{_COLORS[tokens.pop()]}'
|
|
|
|
if tokens and tokens[-1] == 'LIGHT':
|
|
|
|
fg_color = f'9{fg_color[1:]}'
|
|
|
|
tokens.pop()
|
|
|
|
fg_style = tokens.pop() if tokens and tokens[-1] in _TEXT_STYLES else 'NORMAL'
|
|
|
|
fg_color = f'\033[{_TEXT_STYLES[fg_style]};{fg_color}m'
|
|
|
|
if tokens:
|
|
|
|
raise SyntaxError(f'Invalid format {" ".join(tokens)!r} in {f!r}')
|
|
|
|
|
|
|
|
if fg_color or bg_color:
|
2022-04-28 10:33:26 -04:00
|
|
|
text = text.replace(CONTROL_SEQUENCES['RESET'], f'{fg_color}{bg_color}')
|
2021-10-20 12:37:32 -04:00
|
|
|
return f'{fg_color}{bg_color}{text}{CONTROL_SEQUENCES["RESET"]}'
|
|
|
|
else:
|
|
|
|
return text
|
2021-10-08 15:11:59 -04:00
|
|
|
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
class MultilinePrinterBase:
|
|
|
|
def __init__(self, stream=None, lines=1):
|
|
|
|
self.stream = stream
|
|
|
|
self.maximum = lines - 1
|
2021-11-28 16:25:37 -05:00
|
|
|
self._HAVE_FULLCAP = supports_terminal_sequences(stream)
|
2021-09-22 10:12:04 -04:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.end()
|
|
|
|
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
pass
|
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
def _add_line_number(self, text, line):
|
|
|
|
if self.maximum:
|
|
|
|
return f'{line + 1}: {text}'
|
|
|
|
return text
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-09 21:38:22 -04:00
|
|
|
def write(self, *text):
|
|
|
|
write_string(''.join(text), self.stream)
|
|
|
|
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
class QuietMultilinePrinter(MultilinePrinterBase):
|
|
|
|
pass
|
2021-09-22 10:12:04 -04:00
|
|
|
|
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
class MultilineLogger(MultilinePrinterBase):
|
2021-10-09 21:38:22 -04:00
|
|
|
def write(self, *text):
|
|
|
|
self.stream.debug(''.join(text))
|
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
# stream is the logger object, not an actual stream
|
2021-10-09 21:38:22 -04:00
|
|
|
self.write(self._add_line_number(text, pos))
|
2021-09-22 10:12:04 -04:00
|
|
|
|
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
class BreaklineStatusPrinter(MultilinePrinterBase):
|
|
|
|
def print_at_line(self, text, pos):
|
2021-10-09 21:38:22 -04:00
|
|
|
self.write(self._add_line_number(text, pos), '\n')
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
|
|
|
|
class MultilinePrinter(MultilinePrinterBase):
|
|
|
|
def __init__(self, stream=None, lines=1, preserve_output=True):
|
|
|
|
super().__init__(stream, lines)
|
|
|
|
self.preserve_output = preserve_output
|
|
|
|
self._lastline = self._lastlength = 0
|
|
|
|
self._movelock = Lock()
|
|
|
|
|
|
|
|
def lock(func):
|
2021-10-08 20:23:15 -04:00
|
|
|
@functools.wraps(func)
|
2021-10-08 15:11:59 -04:00
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
with self._movelock:
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
return wrapper
|
2021-09-22 10:12:04 -04:00
|
|
|
|
|
|
|
def _move_cursor(self, dest):
|
2021-10-08 15:11:59 -04:00
|
|
|
current = min(self._lastline, self.maximum)
|
2021-10-09 21:38:22 -04:00
|
|
|
yield '\r'
|
2021-10-08 15:11:59 -04:00
|
|
|
distance = dest - current
|
|
|
|
if distance < 0:
|
2021-10-20 12:37:32 -04:00
|
|
|
yield CONTROL_SEQUENCES['UP'] * -distance
|
2021-10-08 15:11:59 -04:00
|
|
|
elif distance > 0:
|
2021-10-20 12:37:32 -04:00
|
|
|
yield CONTROL_SEQUENCES['DOWN'] * distance
|
2021-10-08 15:11:59 -04:00
|
|
|
self._lastline = dest
|
|
|
|
|
|
|
|
@lock
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
if self._HAVE_FULLCAP:
|
2021-10-20 12:37:32 -04:00
|
|
|
self.write(*self._move_cursor(pos), CONTROL_SEQUENCES['ERASE_LINE'], text)
|
2021-12-20 01:06:46 -05:00
|
|
|
return
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
text = self._add_line_number(text, pos)
|
|
|
|
textlen = len(text)
|
|
|
|
if self._lastline == pos:
|
|
|
|
# move cursor at the start of progress when writing to same line
|
2021-10-09 21:38:22 -04:00
|
|
|
prefix = '\r'
|
2021-10-08 15:11:59 -04:00
|
|
|
if self._lastlength > textlen:
|
|
|
|
text += ' ' * (self._lastlength - textlen)
|
|
|
|
self._lastlength = textlen
|
|
|
|
else:
|
|
|
|
# otherwise, break the line
|
2021-10-09 21:38:22 -04:00
|
|
|
prefix = '\n'
|
2021-10-08 15:11:59 -04:00
|
|
|
self._lastlength = textlen
|
2021-10-09 21:38:22 -04:00
|
|
|
self.write(prefix, text)
|
2021-10-08 15:11:59 -04:00
|
|
|
self._lastline = pos
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
@lock
|
2021-09-22 10:12:04 -04:00
|
|
|
def end(self):
|
2021-10-08 15:11:59 -04:00
|
|
|
# move cursor to the end of the last line, and write line break
|
|
|
|
# so that other to_screen calls can precede
|
2021-10-09 21:38:22 -04:00
|
|
|
text = self._move_cursor(self.maximum) if self._HAVE_FULLCAP else []
|
2021-10-08 15:11:59 -04:00
|
|
|
if self.preserve_output:
|
2021-10-09 21:38:22 -04:00
|
|
|
self.write(*text, '\n')
|
2021-10-08 15:11:59 -04:00
|
|
|
return
|
2021-09-22 10:12:04 -04:00
|
|
|
|
2021-10-08 15:11:59 -04:00
|
|
|
if self._HAVE_FULLCAP:
|
2021-10-09 21:38:22 -04:00
|
|
|
self.write(
|
2021-10-20 12:37:32 -04:00
|
|
|
*text, CONTROL_SEQUENCES['ERASE_LINE'],
|
|
|
|
f'{CONTROL_SEQUENCES["UP"]}{CONTROL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
|
2021-10-08 15:11:59 -04:00
|
|
|
else:
|
2022-04-09 15:31:48 -04:00
|
|
|
self.write('\r', ' ' * self._lastlength, '\r')
|