Skip to content

Commit 7df5e10

Browse files
author
David Ruwodo
committed
Refactor: use shutil.get_terminal_size(fallback=(120, 40)) for safer terminal width detection
1 parent 0dba1e9 commit 7df5e10

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

  • src/dstack/_internal/cli/utils

src/dstack/_internal/cli/utils/run.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
import shutil
22
from typing import Any, Dict, List, Optional, Union
33

44
from rich.markup import escape
@@ -95,11 +95,7 @@ def th(s: str) -> str:
9595
props.add_row(th("Inactivity duration"), inactivity_duration)
9696
props.add_row(th("Reservation"), run_spec.configuration.reservation or "-")
9797

98-
try:
99-
width = os.get_terminal_size()[0]
100-
except OSError:
101-
width = 120 # Default width for non-TTY
102-
offers = Table(box=None, expand=width <= 110)
98+
offers = Table(box=None, expand=shutil.get_terminal_size(fallback=(120, 40)).columns <= 110)
10399
offers.add_column("#")
104100
offers.add_column("BACKEND", style="grey58", ratio=2)
105101
offers.add_column("RESOURCES", ratio=4)
@@ -153,11 +149,7 @@ def th(s: str) -> str:
153149
def get_runs_table(
154150
runs: List[Run], verbose: bool = False, format_date: DateFormatter = pretty_date
155151
) -> Table:
156-
try:
157-
width = os.get_terminal_size()[0]
158-
except OSError:
159-
width = 120 # Default width for non-TTY
160-
table = Table(box=None, expand=width <= 110)
152+
table = Table(box=None, expand=shutil.get_terminal_size(fallback=(120, 40)).columns <= 110)
161153
table.add_column("NAME", style="bold", no_wrap=True, ratio=2)
162154
table.add_column("BACKEND", style="grey58", ratio=2)
163155
table.add_column("RESOURCES", ratio=3 if not verbose else 2)

0 commit comments

Comments
 (0)