Skip to content

Commit

Permalink
Updating the progress bar for cross platform compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
anibalinn committed Nov 26, 2024
1 parent 4937164 commit cdaa979
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
27 changes: 22 additions & 5 deletions behavex/progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import platform
import sys
import time

from behavex.global_vars import global_vars


def get_progress_chars():
"""Get appropriate progress bar characters based on platform."""
if platform.system() == 'Windows':
return {
'bar': '#',
'edge': '|',
'empty': '-'
}
return {
'bar': '█',
'edge': '|',
'empty': '-'
}


class ProgressBar:
def __init__(self, prefix, total, bar_length=15, print_updates_in_new_lines=False):
def __init__(self, prefix, total, print_updates_in_new_lines=False):
self.prefix = prefix
self.total = total
self.bar_length = bar_length
self.print_updates_in_new_lines = print_updates_in_new_lines
self.progress_chars = get_progress_chars()
self.bar_length = 15
self.current_iteration = 0
self.start_time = global_vars.execution_start_time
self.print_in_new_lines = print_updates_in_new_lines

def start(self, start_increment=0):
self.current_iteration = start_increment
Expand All @@ -34,11 +51,11 @@ def _print_progress_bar(self, new_line=False):
else:
percent = 100 * float(self.current_iteration / float(self.total))
filled_length = int(self.bar_length * self.current_iteration // self.total)
bar = '█' * filled_length + '-' * (self.bar_length - filled_length)
bar = self.progress_chars['bar'] * filled_length + self.progress_chars['empty'] * (self.bar_length - filled_length)
elapsed_time = global_vars.execution_elapsed_time
elapsed_formatted = time.strftime("%M:%S", time.gmtime(elapsed_time))
progress_bar_content = f"\r{prefix}{percent:.0f}%|{bar}| {self.current_iteration}/{self.total} [{elapsed_formatted}]\r"
if self.print_in_new_lines or new_line or percent == 100:
if self.print_updates_in_new_lines or new_line or percent == 100:
print(progress_bar_content)
else:
sys.stdout.write(progress_bar_content)
Expand Down
10 changes: 5 additions & 5 deletions tests/features/progress_bar.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Background:
Scenario Outline: Progress bar should be shown when running tests in parallel
When I run the behavex command with "<parallel_processes>" parallel processes and parallel scheme set as "<parallel_scheme>"
Then I should see the following behavex console outputs and exit code "1"
| output_line |
| PARALLEL_PROCESSES \| <parallel_processes> |
| PARALLEL_SCHEME \| <parallel_scheme> |
| Exit code: 1 |
| Executed <parallel_scheme>s: 100%\|███████████████\| |
| output_line |
| PARALLEL_PROCESSES \| <parallel_processes> |
| PARALLEL_SCHEME \| <parallel_scheme> |
| Exit code: 1 |
| Executed <parallel_scheme>s: 100%\| |
And I should not see error messages in the output
Examples:
| parallel_scheme | parallel_processes |
Expand Down

0 comments on commit cdaa979

Please sign in to comment.