Skip to content

Commit 27d80fd

Browse files
committed
fix terminal width issue
1 parent 8ee25aa commit 27d80fd

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

samcli/cli/command.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import click
1010
from click import Group
1111

12-
from samcli.cli.formatters import RootCommandHelpTextFormatter
12+
from samcli.cli.formatters import RootCommandHelpTextFormatter, get_terminal_width
1313
from samcli.cli.root.command_list import SAM_CLI_COMMANDS
1414
from samcli.cli.row_modifiers import RowDefinition, ShowcaseRowModifier
1515

@@ -65,6 +65,13 @@ class CustomFormatterContext(click.Context):
6565
def __init__(self, *args, **kwargs):
6666
if "max_content_width" not in kwargs:
6767
kwargs["max_content_width"] = 140
68+
# Explicitly set terminal width if not provided to ensure proper text wrapping
69+
# Apply right padding to prevent text from wrapping at the terminal edge
70+
if "terminal_width" not in kwargs:
71+
terminal_width = get_terminal_width()
72+
if terminal_width is not None:
73+
# Apply padding but ensure minimum width of 60
74+
kwargs["terminal_width"] = max(terminal_width - 5, 60)
6875
super().__init__(*args, **kwargs)
6976

7077
context_class = CustomFormatterContext

samcli/cli/formatters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Click Help Formatter Classes that are customized for the root command.
33
"""
44

5+
import shutil
56
from contextlib import contextmanager
67
from typing import Iterator, Optional, Sequence
78

@@ -11,6 +12,21 @@
1112
from samcli.cli.row_modifiers import BaseLineRowModifier, RowDefinition
1213

1314

15+
def get_terminal_width() -> Optional[int]:
16+
"""
17+
Get the current terminal width.
18+
19+
Returns
20+
-------
21+
Optional[int]
22+
Terminal width in columns, or None if it cannot be determined.
23+
"""
24+
try:
25+
return shutil.get_terminal_size().columns
26+
except (AttributeError, ValueError, OSError):
27+
return None
28+
29+
1430
class RootCommandHelpTextFormatter(HelpFormatter):
1531
# Picked an additive constant that gives an aesthetically pleasing look.
1632
ADDITIVE_JUSTIFICATION = 6

samcli/commands/common/formatters.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Shared formatter for all SAM CLI commands.
33
"""
44

5-
from samcli.cli.formatters import RootCommandHelpTextFormatter
5+
from samcli.cli.formatters import RootCommandHelpTextFormatter, get_terminal_width
66
from samcli.cli.row_modifiers import BaseLineRowModifier
77

88

@@ -13,7 +13,7 @@ class CommandHelpTextFormatter(RootCommandHelpTextFormatter):
1313

1414
DEFAULT_ADDITIVE_JUSTIFICATION = 6
1515

16-
def __init__(self, options, additive_justification=None, *args, **kwargs):
16+
def __init__(self, options, additive_justification=None, width=None, *args, **kwargs):
1717
"""
1818
Initialize the formatter.
1919
@@ -24,8 +24,17 @@ def __init__(self, options, additive_justification=None, *args, **kwargs):
2424
additive_justification : int, optional
2525
Override the default additive justification value.
2626
If not provided, uses DEFAULT_ADDITIVE_JUSTIFICATION (6).
27+
width : int, optional
28+
Terminal width. If None, will attempt to detect terminal width.
2729
"""
28-
super().__init__(*args, **kwargs)
30+
# If width is None, try to detect terminal width and apply padding
31+
if width is None:
32+
detected_width = get_terminal_width()
33+
if detected_width is not None:
34+
# Apply padding but ensure minimum width of 60
35+
width = max(detected_width - 5, 60)
36+
37+
super().__init__(width=width, *args, **kwargs)
2938
justification = (
3039
additive_justification if additive_justification is not None else self.DEFAULT_ADDITIVE_JUSTIFICATION
3140
)

samcli/commands/local/cli_common/options.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,12 @@ def warm_containers_common_options(f):
244244
"--warm-containers",
245245
help="""
246246
\b
247-
Optional. Specifies how AWS SAM CLI manages
248-
containers for each function.
247+
Optional. Specifies how AWS SAM CLI manages containers for each function.'
248+
\b
249249
Two modes are available:
250-
EAGER: Containers for all functions are
251-
loaded at startup and persist between
252-
invocations.
253-
LAZY: Containers are only loaded when each
254-
function is first invoked. Those containers
255-
persist for additional invocations.
250+
EAGER: Containers for all functions are loaded at startup and persist between invocations.
251+
LAZY: Containers are only loaded when each function is first invoked.
252+
Those containers persist for additional invocations.
256253
""",
257254
type=click.Choice(ContainersInitializationMode.__members__, case_sensitive=False),
258255
),

0 commit comments

Comments
 (0)