Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix command line progress output #1632

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions gramps/cli/test/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ def _progress_begin_step_end(self):
self.user.step_progress()
self.user.end_progress()

def test_progress_percent(self):
self.user.begin_progress("Foo", "Bar", 20)
self.user._fileout.write.assert_called_with("Bar")
for i in range(20):
self.user.step_progress()
pct = (i + 1) * 5
self.user._fileout.write.assert_called_with("\r{:3d}% ".format(pct))
self.user.end_progress()
self.user._fileout.write.assert_called_with("\n")

def test_progress_spinner(self):
self.user.begin_progress("Foo", "Bar", 0)
self.user._fileout.write.assert_called_with("Bar")
for i in range(5):
self.user.step_progress()
spn = user._SPINNER[(i + 1) % 4]
self.user._fileout.write.assert_called_with("\r {} ".format(spn))
self.user.end_progress()
self.user._fileout.write.assert_called_with("\n")


if __name__ == "__main__":
unittest.main()
28 changes: 16 additions & 12 deletions gramps/cli/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,35 @@ def begin_progress(self, title, message, steps):
:type steps: int
:returns: none
"""
self._fileout.write(message)
self.steps = steps
self.current_step = 0
if self.steps == 0:
self._fileout.write(_SPINNER[self.current_step])
else:
self._fileout.write("00%")
self.display_progress()
self._fileout.write(message)

def step_progress(self):
"""
Advance the progress meter.
"""
self.current_step += 1
if self.steps == 0:
self.current_step %= 4
self._fileout.write("\r %s " % _SPINNER[self.current_step])
else:
percent = int((float(self.current_step) / self.steps) * 100)
self._fileout.write("\r%02d%%" % percent)
self.display_progress()

def end_progress(self):
"""
Stop showing the progress indicator to the user.
"""
self._fileout.write("\r100%\n")
self.display_progress(end=True)

def display_progress(self, end=False):
if end:
self.steps = self.current_step = 1
if self.steps == 0:
self.current_step %= 4
self._fileout.write("\r %s " % _SPINNER[self.current_step])
else:
percent = int((float(self.current_step) / self.steps) * 100)
self._fileout.write("\r%3d%% " % percent)
if end:
self._fileout.write("\n")

def prompt(
self,
Expand Down
Loading