Skip to content

Commit 5f1096d

Browse files
committed
enhanced progress bar
1 parent 53ce56b commit 5f1096d

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,67 @@ def _get_auth_headers(cli_ctx, subscription_id):
4040
}
4141

4242

43-
def _make_what_if_request(payload, headers_dict):
43+
def _make_what_if_request(payload, headers_dict, cli_ctx=None):
4444
request_completed = threading.Event()
4545

4646
def _rotating_progress():
4747
"""Simulate a rotating progress indicator."""
48-
chars = ["|", "\\", "/", "-"]
48+
spinner_chars = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
49+
fallback_chars = ["|", "\\", "/", "-"]
50+
51+
try:
52+
"⠋".encode(sys.stderr.encoding or 'utf-8')
53+
chars = spinner_chars
54+
except (UnicodeEncodeError, UnicodeDecodeError, LookupError):
55+
chars = fallback_chars
56+
57+
use_color = cli_ctx and getattr(cli_ctx, 'enable_color', False)
58+
if use_color:
59+
try:
60+
CYAN = '\033[36m'
61+
GREEN = '\033[32m'
62+
YELLOW = '\033[33m'
63+
BLUE = '\033[34m'
64+
RESET = '\033[0m'
65+
BOLD = '\033[1m'
66+
except (UnicodeError, AttributeError):
67+
use_color = False
68+
69+
if not use_color:
70+
CYAN = GREEN = YELLOW = BLUE = RESET = BOLD = ''
71+
4972
idx = 0
73+
start_time = time.time()
74+
75+
# Simulate different stages, can be improved with real stages if available
5076
while not request_completed.is_set():
51-
sys.stderr.write(f"\r{chars[idx % len(chars)]} Running")
77+
elapsed = time.time() - start_time
78+
if elapsed < 10:
79+
status = f"{CYAN}Connecting to what-if service{RESET}"
80+
spinner_color = CYAN
81+
elif elapsed < 30:
82+
status = f"{BLUE}Analyzing Azure CLI script{RESET}"
83+
spinner_color = BLUE
84+
elif elapsed < 60:
85+
status = f"{YELLOW}Processing what-if analysis{RESET}"
86+
spinner_color = YELLOW
87+
else:
88+
status = f"{GREEN}Finalizing results{RESET}"
89+
spinner_color = GREEN
90+
elapsed_str = f"{BOLD}({elapsed:.0f}s){RESET}"
91+
spinner = f"{spinner_color}{chars[idx % len(chars)]}{RESET}"
92+
progress_line = f"{spinner} {status}... {elapsed_str}"
93+
visible_length = len(progress_line) - (progress_line.count('\033[') * 5)
94+
max_width = 100
95+
if visible_length > max_width:
96+
truncated_status = status[:max_width-30] + "..."
97+
progress_line = f"{spinner} {truncated_status} {elapsed_str}"
98+
sys.stderr.write(f"\r{' ' * 120}\r{progress_line}")
5299
sys.stderr.flush()
53100
idx += 1
54-
time.sleep(0.2)
55-
sys.stderr.write("\r" + " " * 20 + "\r")
101+
time.sleep(0.12)
102+
clear_line = f"\r{' ' * 120}\r"
103+
sys.stderr.write(clear_line)
56104
sys.stderr.flush()
57105

58106
try:
@@ -180,7 +228,7 @@ def show_what_if(cli_ctx, azcli_script: str, subscription_id: str = None, no_pre
180228
}
181229

182230
headers_dict = _get_auth_headers(cli_ctx, subscription_id)
183-
response = _make_what_if_request(payload, headers_dict)
231+
response = _make_what_if_request(payload, headers_dict, cli_ctx)
184232

185233
try:
186234
raw_results = response.json()

0 commit comments

Comments
 (0)