|
|
@ -1,6 +1,6 @@
|
|
|
|
import functools
|
|
|
|
import functools
|
|
|
|
from threading import Lock
|
|
|
|
from threading import Lock
|
|
|
|
from .utils import supports_terminal_sequences, TERMINAL_SEQUENCES
|
|
|
|
from .utils import supports_terminal_sequences, TERMINAL_SEQUENCES, write_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultilinePrinterBase:
|
|
|
|
class MultilinePrinterBase:
|
|
|
@ -25,20 +25,26 @@ class MultilinePrinterBase:
|
|
|
|
return f'{line + 1}: {text}'
|
|
|
|
return f'{line + 1}: {text}'
|
|
|
|
return text
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write(self, *text):
|
|
|
|
|
|
|
|
write_string(''.join(text), self.stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuietMultilinePrinter(MultilinePrinterBase):
|
|
|
|
class QuietMultilinePrinter(MultilinePrinterBase):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultilineLogger(MultilinePrinterBase):
|
|
|
|
class MultilineLogger(MultilinePrinterBase):
|
|
|
|
|
|
|
|
def write(self, *text):
|
|
|
|
|
|
|
|
self.stream.debug(''.join(text))
|
|
|
|
|
|
|
|
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
# stream is the logger object, not an actual stream
|
|
|
|
# stream is the logger object, not an actual stream
|
|
|
|
self.stream.debug(self._add_line_number(text, pos))
|
|
|
|
self.write(self._add_line_number(text, pos))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BreaklineStatusPrinter(MultilinePrinterBase):
|
|
|
|
class BreaklineStatusPrinter(MultilinePrinterBase):
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
self.stream.write(self._add_line_number(text, pos) + '\n')
|
|
|
|
self.write(self._add_line_number(text, pos), '\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultilinePrinter(MultilinePrinterBase):
|
|
|
|
class MultilinePrinter(MultilinePrinterBase):
|
|
|
@ -58,50 +64,46 @@ class MultilinePrinter(MultilinePrinterBase):
|
|
|
|
|
|
|
|
|
|
|
|
def _move_cursor(self, dest):
|
|
|
|
def _move_cursor(self, dest):
|
|
|
|
current = min(self._lastline, self.maximum)
|
|
|
|
current = min(self._lastline, self.maximum)
|
|
|
|
self.stream.write('\r')
|
|
|
|
yield '\r'
|
|
|
|
distance = dest - current
|
|
|
|
distance = dest - current
|
|
|
|
if distance < 0:
|
|
|
|
if distance < 0:
|
|
|
|
self.stream.write(TERMINAL_SEQUENCES['UP'] * -distance)
|
|
|
|
yield TERMINAL_SEQUENCES['UP'] * -distance
|
|
|
|
elif distance > 0:
|
|
|
|
elif distance > 0:
|
|
|
|
self.stream.write(TERMINAL_SEQUENCES['DOWN'] * distance)
|
|
|
|
yield TERMINAL_SEQUENCES['DOWN'] * distance
|
|
|
|
self._lastline = dest
|
|
|
|
self._lastline = dest
|
|
|
|
|
|
|
|
|
|
|
|
@lock
|
|
|
|
@lock
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
def print_at_line(self, text, pos):
|
|
|
|
if self._HAVE_FULLCAP:
|
|
|
|
if self._HAVE_FULLCAP:
|
|
|
|
self._move_cursor(pos)
|
|
|
|
self.write(*self._move_cursor(pos), TERMINAL_SEQUENCES['ERASE_LINE'], text)
|
|
|
|
self.stream.write(TERMINAL_SEQUENCES['ERASE_LINE'])
|
|
|
|
|
|
|
|
self.stream.write(text)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text = self._add_line_number(text, pos)
|
|
|
|
text = self._add_line_number(text, pos)
|
|
|
|
textlen = len(text)
|
|
|
|
textlen = len(text)
|
|
|
|
if self._lastline == pos:
|
|
|
|
if self._lastline == pos:
|
|
|
|
# move cursor at the start of progress when writing to same line
|
|
|
|
# move cursor at the start of progress when writing to same line
|
|
|
|
self.stream.write('\r')
|
|
|
|
prefix = '\r'
|
|
|
|
if self._lastlength > textlen:
|
|
|
|
if self._lastlength > textlen:
|
|
|
|
text += ' ' * (self._lastlength - textlen)
|
|
|
|
text += ' ' * (self._lastlength - textlen)
|
|
|
|
self._lastlength = textlen
|
|
|
|
self._lastlength = textlen
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# otherwise, break the line
|
|
|
|
# otherwise, break the line
|
|
|
|
self.stream.write('\n')
|
|
|
|
prefix = '\n'
|
|
|
|
self._lastlength = textlen
|
|
|
|
self._lastlength = textlen
|
|
|
|
self.stream.write(text)
|
|
|
|
self.write(prefix, text)
|
|
|
|
self._lastline = pos
|
|
|
|
self._lastline = pos
|
|
|
|
|
|
|
|
|
|
|
|
@lock
|
|
|
|
@lock
|
|
|
|
def end(self):
|
|
|
|
def end(self):
|
|
|
|
# move cursor to the end of the last line, and write line break
|
|
|
|
# move cursor to the end of the last line, and write line break
|
|
|
|
# so that other to_screen calls can precede
|
|
|
|
# so that other to_screen calls can precede
|
|
|
|
if self._HAVE_FULLCAP:
|
|
|
|
text = self._move_cursor(self.maximum) if self._HAVE_FULLCAP else []
|
|
|
|
self._move_cursor(self.maximum)
|
|
|
|
|
|
|
|
if self.preserve_output:
|
|
|
|
if self.preserve_output:
|
|
|
|
self.stream.write('\n')
|
|
|
|
self.write(*text, '\n')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if self._HAVE_FULLCAP:
|
|
|
|
if self._HAVE_FULLCAP:
|
|
|
|
self.stream.write(
|
|
|
|
self.write(
|
|
|
|
TERMINAL_SEQUENCES['ERASE_LINE']
|
|
|
|
*text, TERMINAL_SEQUENCES['ERASE_LINE'],
|
|
|
|
+ f'{TERMINAL_SEQUENCES["UP"]}{TERMINAL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
|
|
|
|
f'{TERMINAL_SEQUENCES["UP"]}{TERMINAL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.stream.write(' ' * self._lastlength)
|
|
|
|
self.write(*text, ' ' * self._lastlength)
|
|
|
|