Use the repeat escape code for progress bar rendering

This commit is contained in:
Kovid Goyal
2021-09-16 17:55:22 +05:30
parent aa9a991a85
commit eaeece8177
3 changed files with 17 additions and 8 deletions

View File

@@ -582,7 +582,7 @@ class Send(Handler):
af = self.manager.active_file af = self.manager.active_file
now = monotonic() now = monotonic()
if af is None: if af is None:
self.write('' * self.screen_size.width) self.cmd.repeat('', self.screen_size.width)
else: else:
self.draw_progress_for_current_file(af, spinner_char=sc) self.draw_progress_for_current_file(af, spinner_char=sc)
self.print() self.print()

View File

@@ -114,6 +114,11 @@ def without_line_wrap(write: Callable[[str], None]) -> Generator[None, None, Non
write(set_line_wrapping(True)) write(set_line_wrapping(True))
@cmd
def repeat(char: str, count: int) -> str:
return f'{char}\x1b[{abs(count)}b'
@cmd @cmd
def set_cursor_visible(yes_or_no: bool) -> str: def set_cursor_visible(yes_or_no: bool) -> str:
return set_mode(Mode.DECTCEM) if yes_or_no else reset_mode(Mode.DECTCEM) return set_mode(Mode.DECTCEM) if yes_or_no else reset_mode(Mode.DECTCEM)

View File

@@ -3,7 +3,7 @@
# License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
from .operations import styled from .operations import styled, repeat
def render_progress_bar(frac: float, width: int = 80) -> str: def render_progress_bar(frac: float, width: int = 80) -> str:
@@ -12,28 +12,32 @@ def render_progress_bar(frac: float, width: int = 80) -> str:
if frac <= 0: if frac <= 0:
return styled('🬋' * width, dim=True) return styled('🬋' * width, dim=True)
w = frac * width w = frac * width
overhang = w - int(w) fl = int(w)
filled = '🬋' * int(w) overhang = w - fl
filled = repeat('🬋', fl)
if overhang < 0.2: if overhang < 0.2:
needs_break = True needs_break = True
elif overhang < 0.8: elif overhang < 0.8:
filled += '🬃' filled += '🬃'
fl += 1
needs_break = False needs_break = False
else: else:
if len(filled) < width - 1: if fl < width - 1:
filled += '🬋' filled += '🬋'
fl += 1
needs_break = True needs_break = True
else: else:
filled += '🬃' filled += '🬃'
fl += 1
needs_break = False needs_break = False
ans = styled(filled, fg='blue') ans = styled(filled, fg='blue')
unfilled = '' unfilled = ''
if width > len(filled): if width > fl:
if needs_break: if needs_break:
unfilled += '🬇' unfilled += '🬇'
filler = width - len(filled) - len(unfilled) filler = width - fl - len(unfilled)
if filler > 0: if filler > 0:
unfilled += '🬋' * filler unfilled += repeat('🬋', filler)
if unfilled: if unfilled:
ans += styled(unfilled, dim=True) ans += styled(unfilled, dim=True)
return ans return ans