Skip to content

Commit 4aa1441

Browse files
committed
ensure bar prints across full terminal width
- put in a few hacks but I'll need to add unit tests to actually understand what's happening
1 parent d9e95c7 commit 4aa1441

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

laser_prynter/pbar.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def randgrad() -> tuple[RGB, RGB]:
6868
)
6969

7070
def _pbar(self) -> Iterator[tuple[tuple[int, RGB]]]:
71-
for x in range(self.curr, self.w):
71+
for x in range(self.w + 1): # TODO: i'm so dumb, why do I need a +1 here?
7272
if self.t > self.w:
7373
tpos = int((x / self.w) * self.t)
7474
color = self.g[tpos]
@@ -113,23 +113,29 @@ def __enter__(self) -> PBar:
113113
_print_to_terminal(
114114
'\n' # ensure space for scrollbar
115115
'\x1b7' # save cursor position
116-
f'\x1b[0;{self.h - 1}r' # set scrollable region (margin
116+
f'\x1b[0;{self.h - 1}r' # set top & bottom regions (margins)
117117
'\x1b8' # restore cursor position
118118
'\x1b[1A' # move cursor up
119119
)
120120
return self
121121

122122
def __exit__(self, _exc_type: type, _exc_val: BaseException, _exc_tb: type) -> None:
123+
# TODO: this is to ensure that the bar draws the full width. it should have done this already?
124+
while True:
125+
try:
126+
next(self)
127+
except StopIteration:
128+
break
123129
_print_to_terminal(
124-
f'\x1b[0;{self.h}r' # reset scrollable region
130+
f'\x1b[0;{self.h}r' # reset margins
125131
f'\x1b[{self.h};0H' # move to bottom line
126132
'\n'
127133
)
128134
self.curr = self.t
129135

130136

131137
if __name__ == '__main__':
132-
with PBar(100) as pbar:
138+
with PBar(100, *PBar.randgrad()) as pbar:
133139
for i in range(100):
134140
time.sleep(0.01)
135141
print(f'-> {i}')

0 commit comments

Comments
 (0)