From 846022cfc574417f7ebf38071bacd9292e474332 Mon Sep 17 00:00:00 2001 From: CodeWriter21 Date: Mon, 25 Jul 2022 17:32:26 +0430 Subject: [PATCH] Fixed a bug that would cause an error creating a progress bar with no value set for width in systems without support for os.get_terminal_size(). --- CHANGES-LOG.md | 5 +++++ README.md | 18 +++--------------- log21/ProgressBar.py | 10 +++++++++- log21/__init__.py | 2 +- setup.py | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/CHANGES-LOG.md b/CHANGES-LOG.md index ac11a53..ea07dd0 100644 --- a/CHANGES-LOG.md +++ b/CHANGES-LOG.md @@ -6,6 +6,11 @@ Help this project by [Donation](DONATE.md) Changes log ----------- +### 2.3.3 + +Fixed a bug that would cause an error creating a progress bar with no value set for width in systems without support for +os.get_terminal_size(). + ### 2.3.2 Added `additional_variables` argument to `log21.ProgressBar` class. You can use it in order to add additional variables diff --git a/README.md b/README.md index 4dbaafe..f852d02 100644 --- a/README.md +++ b/README.md @@ -53,22 +53,10 @@ python setup.py install Changes ------- -### 2.3.2 +### 2.3.3 -Added `additional_variables` argument to `log21.ProgressBar` class. You can use it in order to add additional variables -to the progress bar: - -```python3 -import log21, time - -progress_bar = log21.ProgressBar(format_='Iteration: {i} {prefix}{bar}{suffix} {percentage}%', style='{', - additional_variables={"i": 0}) - -for i in range(100): - progress_bar(i + 1, 100, i=i) - time.sleep(0.1) -# Iteration: 99 |██████████████████████████████████████████████████████████████████████████████| 100% -``` +Fixed a bug that would cause an error creating a progress bar with no value set for width in systems without support for +os.get_terminal_size(). [Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md) diff --git a/log21/ProgressBar.py b/log21/ProgressBar.py index 64a5d6d..94078d9 100644 --- a/log21/ProgressBar.py +++ b/log21/ProgressBar.py @@ -58,7 +58,15 @@ def __init__(self, *args, width: int = None, show_percentage: bool = True, prefi :param logger: The logger to use :param additional_variables: Additional variables to use in the format and their default values """ - self.width = width if width else _os.get_terminal_size().columns - 1 + # Sets a default value for the width + if width is None: + try: + width = _os.get_terminal_size().columns - 1 + except OSError: + width = 50 + if width < 1: + width = 50 + self.width = width if self.width < 3: raise ValueError('`width` must be greater than 1') if not isinstance(fill, str): diff --git a/log21/__init__.py b/log21/__init__.py index 1ca6e64..00b0358 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -21,7 +21,7 @@ from log21.Formatters import ColorizingFormatter, DecolorizingFormatter from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color -__version__ = "2.3.2" +__version__ = "2.3.3" __author__ = "CodeWriter21 (Mehrad Pooryoussof)" __github__ = "Https://GitHub.com/MPCodeWriter21/log21" __all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter', diff --git a/setup.py b/setup.py index 3c7a5b1..8c04294 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = file.read() DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.' -VERSION = '2.3.2' +VERSION = '2.3.3' setup( name='log21',