]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/minicurses.py
[ie/loom] Add extractors (#8686)
[yt-dlp.git] / yt_dlp / minicurses.py
index 38fdb5bc6e0fbea2d56853f79bbfdbd76cd80dc9..7db02cb59c64b9656460b505554dc571aee6b1dc 100644 (file)
@@ -1,7 +1,7 @@
 import functools
 from threading import Lock
-from .utils import supports_terminal_sequences, write_string
 
+from .utils import supports_terminal_sequences, write_string
 
 CONTROL_SEQUENCES = {
     'DOWN': '\n',
 
 
 def format_text(text, f):
+    '''
+    @param f    String representation of formatting to apply in the form:
+                [style] [light] font_color [on [light] bg_color]
+                E.g. "red", "bold green on light blue"
+    '''
     f = f.upper()
     tokens = f.strip().split()
 
@@ -64,6 +69,7 @@ def format_text(text, f):
             raise SyntaxError(f'Invalid format {" ".join(tokens)!r} in {f!r}')
 
     if fg_color or bg_color:
+        text = text.replace(CONTROL_SEQUENCES['RESET'], f'{fg_color}{bg_color}')
         return f'{fg_color}{bg_color}{text}{CONTROL_SEQUENCES["RESET"]}'
     else:
         return text
@@ -73,6 +79,7 @@ class MultilinePrinterBase:
     def __init__(self, stream=None, lines=1):
         self.stream = stream
         self.maximum = lines - 1
+        self._HAVE_FULLCAP = supports_terminal_sequences(stream)
 
     def __enter__(self):
         return self
@@ -119,7 +126,6 @@ def __init__(self, stream=None, lines=1, preserve_output=True):
         self.preserve_output = preserve_output
         self._lastline = self._lastlength = 0
         self._movelock = Lock()
-        self._HAVE_FULLCAP = supports_terminal_sequences(self.stream)
 
     def lock(func):
         @functools.wraps(func)
@@ -142,6 +148,7 @@ def _move_cursor(self, dest):
     def print_at_line(self, text, pos):
         if self._HAVE_FULLCAP:
             self.write(*self._move_cursor(pos), CONTROL_SEQUENCES['ERASE_LINE'], text)
+            return
 
         text = self._add_line_number(text, pos)
         textlen = len(text)
@@ -172,4 +179,4 @@ def end(self):
                 *text, CONTROL_SEQUENCES['ERASE_LINE'],
                 f'{CONTROL_SEQUENCES["UP"]}{CONTROL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
         else:
-            self.write(*text, ' ' * self._lastlength)
+            self.write('\r', ' ' * self._lastlength, '\r')