Skip to content

Commit 0cf60fd

Browse files
committed
minor fix
1 parent 45f3dc9 commit 0cf60fd

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

src/azure-cli-core/azure/cli/core/what_if.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _get_auth_headers(cli_ctx, subscription_id):
4444

4545
def _make_what_if_request(payload, headers_dict, cli_ctx=None):
4646
request_completed = threading.Event()
47+
progress_lock = threading.Lock()
4748

4849
def _rotating_progress():
4950
"""Simulate a rotating progress indicator."""
@@ -73,8 +74,22 @@ def _rotating_progress():
7374

7475
idx = 0
7576
start_time = time.time()
77+
78+
is_tty = hasattr(sys.stderr, 'isatty') and sys.stderr.isatty()
79+
80+
if not is_tty:
81+
sys.stderr.write("Processing")
82+
sys.stderr.flush()
83+
while not request_completed.is_set():
84+
if request_completed.wait(timeout=1.0):
85+
break
86+
sys.stderr.write(".")
87+
sys.stderr.flush()
88+
sys.stderr.write(" Done\n")
89+
sys.stderr.flush()
90+
return
7691

77-
# Simulate different stages, can be improved with real stages if available
92+
last_line_length = 0
7893
while not request_completed.is_set():
7994
elapsed = time.time() - start_time
8095
if elapsed < 10:
@@ -97,13 +112,22 @@ def _rotating_progress():
97112
if visible_length > max_width:
98113
truncated_status = status[:max_width - 30] + "..."
99114
progress_line = f"{spinner} {truncated_status} {elapsed_str}"
100-
sys.stderr.write(f"\r{' ' * 120}\r{progress_line}")
101-
sys.stderr.flush()
115+
116+
with progress_lock:
117+
clear_spaces = ' ' * max(last_line_length, 120)
118+
sys.stderr.write(f"\r{clear_spaces}\r{progress_line}")
119+
sys.stderr.flush()
120+
last_line_length = len(progress_line)
121+
102122
idx += 1
103-
time.sleep(0.12)
104-
clear_line = f"\r{' ' * 120}\r"
105-
sys.stderr.write(clear_line)
106-
sys.stderr.flush()
123+
if request_completed.wait(timeout=0.12):
124+
break
125+
126+
with progress_lock:
127+
if is_tty:
128+
clear_spaces = ' ' * max(last_line_length, 120)
129+
sys.stderr.write(f"\r{clear_spaces}\r")
130+
sys.stderr.flush()
107131

108132
try:
109133
function_app_url = "https://azcli-script-insight.azurewebsites.net"
@@ -119,15 +143,33 @@ def _rotating_progress():
119143
prepared = session.prepare_request(req)
120144
response = session.send(prepared)
121145
logger.debug("response: %s", response)
146+
122147
request_completed.set()
123-
progress_thread.join(timeout=0.5)
148+
progress_thread.join(timeout=1.0)
149+
150+
try:
151+
with progress_lock:
152+
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
153+
sys.stderr.write("\r" + " " * 120 + "\r")
154+
sys.stderr.flush()
155+
except:
156+
pass
124157

125158
return response
126159

127160
except Exception as ex:
128161
request_completed.set()
129162
if 'progress_thread' in locals():
130-
progress_thread.join(timeout=0.5)
163+
progress_thread.join(timeout=1.0)
164+
165+
try:
166+
with progress_lock:
167+
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
168+
sys.stderr.write("\r" + " " * 120 + "\r")
169+
sys.stderr.flush()
170+
except:
171+
pass
172+
131173
raise CLIError(f"Failed to connect to the what-if service: {ex}")
132174

133175

0 commit comments

Comments
 (0)