Skip to content

Commit 69ebe64

Browse files
committed
Merge branch 'master' into 3.0.0
2 parents d6aa085 + 68ddd22 commit 69ebe64

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* Added `RawDescriptionCmd2HelpFormatter`, `RawTextCmd2HelpFormatter`, `ArgumentDefaultsCmd2HelpFormatter`,
1010
and `MetavarTypeCmd2HelpFormatter` and they all use `rich-argparse`.
1111

12+
## 2.5.4 (November 6, 2024)
13+
* Bug Fixes
14+
* Fixed `ZeroDivisionError` in `async_alert()` when `shutil.get_terminal_size().columns` is 0.
15+
1216
## 2.5.3 (November 5, 2024)
1317
* Enhancements
1418
* Changed `CommandSet._cmd` to a read-only property which never returns `None` because it

cmd2/cmd2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5077,9 +5077,12 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
50775077
if update_terminal:
50785078
import shutil
50795079

5080+
# Prior to Python 3.11 this can return 0, so use a fallback if needed.
5081+
terminal_columns = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
5082+
50805083
# Print a string which replaces the onscreen prompt and input lines with the alert.
50815084
terminal_str = ansi.async_alert_str(
5082-
terminal_columns=shutil.get_terminal_size().columns,
5085+
terminal_columns=terminal_columns,
50835086
prompt=rl_get_display_prompt(),
50845087
line=readline.get_line_buffer(),
50855088
cursor_offset=rl_get_point(),

cmd2/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@
5757

5858
# custom attributes added to argparse Namespaces
5959
NS_ATTR_SUBCMD_HANDLER = '__subcmd_handler__'
60+
61+
# For cases prior to Python 3.11 when shutil.get_terminal_size().columns can return 0.
62+
DEFAULT_TERMINAL_WIDTH = 80

cmd2/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ def align_text(
874874
)
875875

876876
if width is None:
877-
width = shutil.get_terminal_size().columns
877+
# Prior to Python 3.11 this can return 0, so use a fallback if needed.
878+
width = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
878879

879880
if width < 1:
880881
raise ValueError("width must be at least 1")

tests/test_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import cmd2.utils as cu
1818
from cmd2 import (
1919
ansi,
20+
constants,
2021
)
2122
from cmd2.constants import (
2223
HORIZONTAL_ELLIPSIS,
@@ -624,7 +625,9 @@ def test_align_text_term_width():
624625
text = 'foo'
625626
fill_char = ' '
626627

627-
term_width = shutil.get_terminal_size().columns
628+
# Prior to Python 3.11 this can return 0, so use a fallback, so
629+
# use the same fallback that cu.align_text() does if needed.
630+
term_width = shutil.get_terminal_size().columns or constants.DEFAULT_TERMINAL_WIDTH
628631
expected_fill = (term_width - ansi.style_aware_wcswidth(text)) * fill_char
629632

630633
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char)

0 commit comments

Comments
 (0)