Skip to content

Commit 1fb284a

Browse files
authored
Merge pull request #12 from ModelCloud/fix-ci-test-failure-in-test_draw_respects_terminal_width
Ensure progress bar output matches terminal width
2 parents f15c584 + 3df8db4 commit 1fb284a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

logbar/progress.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,19 @@ def draw(self):
150150
# available columns. Subtracting an extra character caused the rendered line
151151
# to fall short of the terminal width which was noticeable when resizing the
152152
# terminal window.
153-
bar_length = columns - pre_bar_size - len(log) - 2
153+
bar_length = max(0, columns - pre_bar_size - len(log) - 2)
154154

155155
filled_length = int(bar_length * self.step() // len(self))
156156
bar = self._fill * filled_length + '-' * (bar_length - filled_length)
157-
self.log(bar=bar, log=log, pre_bar_padding=padding, end='') # '\n' if percent_num >= 1.0 else ''
157+
self.log(bar=bar, log=log, pre_bar_padding=padding, end='', columns=columns) # '\n' if percent_num >= 1.0 else ''
158158

159159
def calc_time(self, iteration):
160160
used_time = int(time.time() - self.time)
161161
formatted_time = str(datetime.timedelta(seconds=used_time))
162162
remaining = str(datetime.timedelta(seconds=int((used_time / max(iteration, 1)) * len(self))))
163163
return f"{formatted_time} / {remaining}"
164164

165-
def log(self, bar:str, log:str, pre_bar_padding:str = "", end: str = ""):
165+
def log(self, bar:str, log:str, pre_bar_padding:str = "", end: str = "", columns: Optional[int] = None):
166166
out = ""
167167
if self._title:
168168
out += self._title + " "
@@ -178,6 +178,12 @@ def log(self, bar:str, log:str, pre_bar_padding:str = "", end: str = ""):
178178

179179
out += f"{bar}| {log}"
180180

181+
if columns is not None:
182+
if len(out) > columns:
183+
out = out[:columns]
184+
elif len(out) < columns:
185+
out = out + " " * (columns - len(out))
186+
181187
print(f'\r{out}', end=end, flush=True)
182188

183189
update_last_pb_instance(src=self) # let logger now we logged

0 commit comments

Comments
 (0)