File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 '\x1b 7' # 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 '\x1b 8' # 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
131137if __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 } ' )
You can’t perform that action at this time.
0 commit comments