Skip to content

Commit 0bfeda2

Browse files
Victor ViannaV8 LUCI CQ
authored andcommitted
Revert "Improve ui for non-interactive terminals"
This reverts commit 1009ebd. Reason for revert: see crbug.com/428049900 Original change's description: > Improve ui for non-interactive terminals > > Timer and Countdown: > - Only show single message line > > Spinner: > - Only print single title and message update lines > - Avoid an additional sleep on the main thread > > Change-Id: Iba56e895638655c23918fa055ffa318d83aa7ce1 > Reviewed-on: https://chromium-review.googlesource.com/c/crossbench/+/6632506 > Commit-Queue: Camillo Bruni <[email protected]> > Reviewed-by: Patrick Thier <[email protected]> Bug: 428049900 Change-Id: I56ab6e3235fbb1e2e505f15fc1c604ecf72e475c Reviewed-on: https://chromium-review.googlesource.com/c/crossbench/+/6695109 Reviewed-by: Camillo Bruni <[email protected]> Auto-Submit: Victor Vianna <[email protected]> Commit-Queue: Camillo Bruni <[email protected]>
1 parent b288a57 commit 0bfeda2

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

crossbench/cli/ui.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
colorama.init()
2323

24-
IS_ATTY: Final[bool] = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
2524
COLOR_LOGGING: bool = True
2625

2726

@@ -92,14 +91,12 @@ def clear_indented() -> None:
9291
@contextlib.contextmanager
9392
def timer(msg: str = "Elapsed Time",
9493
update_interval=DEFAULT_INTERVAL_S) -> Iterator[None]:
95-
if not IS_ATTY:
96-
yield
97-
return
98-
9994
start_time = dt.datetime.now()
95+
10096
def print_timer():
10197
delta = dt.datetime.now() - start_time
10298
write_indented(f"{msg}: {format_duration(delta)}")
99+
103100
with RepeatTimer(interval=update_interval, function=print_timer):
104101
yield
105102
clear_indented()
@@ -109,16 +106,13 @@ def print_timer():
109106
def countdown(duration: dt.timedelta,
110107
msg: str = "Waiting",
111108
update_interval=DEFAULT_INTERVAL_S) -> Iterator[None]:
112-
if not IS_ATTY:
113-
print(f"{msg}: {format_duration(duration)}")
114-
yield
115-
return
116-
117109
start_time = dt.datetime.now()
110+
118111
def print_timer():
119112
delta = dt.datetime.now() - start_time
120113
time_left = duration - delta
121114
write_indented(f"{msg}: {format_duration(time_left)}")
115+
122116
with RepeatTimer(interval=update_interval, function=print_timer):
123117
yield
124118
clear_indented()
@@ -137,5 +131,5 @@ def __exit__(self, *args, **kwargs) -> None:
137131
self.cancel()
138132

139133

140-
def spinner(sleep: float = 0.5, title: str = "") -> Spinner:
141-
return Spinner(IS_ATTY, sleep, title)
134+
def spinner(*args, **kwargs) -> Spinner:
135+
return Spinner(*args, **kwargs)

crossbench/helper/spinner.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,34 @@
1515
class Spinner:
1616
CURSORS = "◐◓◑◒"
1717

18-
def __init__(self, is_atty: bool, sleep: float, title: str) -> None:
18+
def __init__(self, sleep: float = 0.5, title: str = "") -> None:
1919
self._is_running: bool = False
20-
# Only enable the spinner if the output is an interactive terminal.
21-
self._is_atty: bool = is_atty
2220
self._sleep_time_seconds: float = sleep
2321
self._title: str = title
2422
self._message: str = ""
2523
self._cursor: str = " "
2624

2725
def __enter__(self) -> None:
28-
if self._is_atty:
26+
# Only enable the spinner if the output is an interactive terminal.
27+
is_atty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
28+
if is_atty:
2929
self._is_running = True
3030
threading.Thread(target=self._spin).start()
31-
elif self._title:
32-
# Write single title line.
33-
self._write_message()
3431

3532
def __exit__(self, exc_type, exc_value, traceback) -> None:
36-
self._is_running = False
33+
if self._is_running:
34+
self._is_running = False
35+
self._sleep()
3736

3837
def _cursors(self) -> Iterable[str]:
3938
while True:
4039
yield from Spinner.CURSORS
4140

4241
def _spin(self) -> None:
4342
for cursor in self._cursors():
43+
self._cursor = cursor
4444
if not self._is_running:
4545
return
46-
self._cursor = cursor
4746
self._write_message()
4847
self._sleep()
4948

@@ -64,12 +63,6 @@ def title(self, title: str) -> None:
6463
self._write_message()
6564

6665
def _write_message(self) -> None:
67-
if self._is_atty:
68-
self._write_interactive_message()
69-
else:
70-
print(f"{self._title}{self._message}")
71-
72-
def _write_interactive_message(self) -> None:
7366
stdout = sys.stdout
7467
stdout.write(f"{terminal.STORE_CURSOR_POS} {self._cursor} "
7568
f"{self._title}{self._message}{terminal.CLEAR_END}"

0 commit comments

Comments
 (0)