|
| 1 | +from typing import Any, Dict |
1 | 2 | from rich.console import Console |
2 | 3 | from rich.panel import Panel |
3 | | -from rich.progress import Progress, BarColumn, TextColumn |
| 4 | +from rich.progress import Progress, SpinnerColumn, TextColumn |
4 | 5 | from rich import box |
5 | 6 | from rich.columns import Columns |
6 | 7 | import typer |
@@ -58,122 +59,220 @@ def display_submission_canceled(): |
58 | 59 | def create_submission_progress(): |
59 | 60 | """Create and return a submission progress context""" |
60 | 61 | return Progress( |
61 | | - TextColumn("[bold yellow]Submitting solution...", justify="right"), |
62 | | - BarColumn(bar_width=40, style="yellow"), |
| 62 | + SpinnerColumn(), |
| 63 | + TextColumn("[progress.description]{task.description}"), |
63 | 64 | transient=True, |
64 | 65 | ) |
65 | 66 |
|
66 | | -def display_submission_results(result): |
| 67 | +def display_submission_results(result: Dict[str, Any], is_test: bool = False): |
67 | 68 | """Display submission results with a cleaner layout""" |
68 | | - if result["success"]: |
69 | | - status = result['status'] |
70 | | - |
71 | | - if status == "Accepted": |
72 | | - status_style = "bold green" |
73 | | - border_style = "green" |
74 | | - emoji = "✅" |
75 | | - elif status in ["Runtime Error", "Time Limit Exceeded"]: |
76 | | - status_style = "bold yellow" |
77 | | - border_style = "yellow" |
78 | | - emoji = "⚠️" |
79 | | - else: |
80 | | - status_style = "bold red" |
81 | | - border_style = "red" |
82 | | - emoji = "❌" |
83 | | - |
84 | | - runtime = result.get('runtime', 'N/A') |
85 | | - memory = result.get('memory', 'N/A') |
86 | | - |
87 | | - memory_warning = result.get('memory_warning') |
88 | | - if memory_warning: |
89 | | - memory = f"{memory} [bold yellow](!)[/]" |
90 | | - |
91 | | - passed = result.get('passed_testcases', 0) |
92 | | - total = result.get('total_testcases', 0) |
93 | | - test_case_str = f"{passed}/{total} ({passed/total*100:.1f}%)" if total > 0 else "N/A" |
94 | | - |
95 | | - content = [ |
96 | | - f"[cyan]⏱️ Runtime:[/] {runtime}", |
97 | | - f"[cyan]💾 Memory:[/] {memory}" |
98 | | - ] |
99 | | - |
100 | | - if result.get('elapsed_time'): |
101 | | - content.append(f"[cyan]⏲️ Elapsed Time:[/] {result.get('elapsed_time')} ms") |
102 | | - |
103 | | - content.append(f"[cyan]🧪 Test Cases:[/] {test_case_str}") |
104 | | - |
105 | | - title = f"{emoji} Submission Result: [{status_style}]{status}[/]" |
106 | | - console.print(Panel( |
107 | | - "\n".join(content), |
108 | | - title=title, |
109 | | - border_style=border_style, |
110 | | - box=box.ROUNDED |
111 | | - )) |
| 69 | + status_code = result.get('status_code') |
| 70 | + status = result.get('status', result.get('status_msg', 'Unknown')) |
| 71 | + run_success = result.get('run_success', result.get('success', False)) |
| 72 | + |
| 73 | + if status == "Accepted" and run_success: |
| 74 | + status_style = "bold green" |
| 75 | + border_style = "green" |
| 76 | + emoji = "✅" |
| 77 | + elif status in ["Runtime Error", "Time Limit Exceeded"] or status_code in [14, 15]: |
| 78 | + status_style = "bold yellow" |
| 79 | + border_style = "yellow" |
| 80 | + emoji = "⚠️" |
| 81 | + elif status == "Compile Error" or status_code == 20: |
| 82 | + status_style = "bold red" |
| 83 | + border_style = "red" |
| 84 | + emoji = "❌" |
| 85 | + else: |
| 86 | + status_style = "bold red" |
| 87 | + border_style = "red" |
| 88 | + emoji = "❌" |
| 89 | + |
| 90 | + runtime = result.get('status_runtime', result.get('runtime', 'N/A')) |
| 91 | + memory = result.get('status_memory', result.get('memory', 'N/A')) |
| 92 | + |
| 93 | + if isinstance(memory, int): |
| 94 | + memory = f"{memory / 1000000:.1f} MB" |
| 95 | + |
| 96 | + memory_warning = None |
| 97 | + if isinstance(result.get('memory'), int): |
| 98 | + memory_value = result.get('memory', 0) |
| 99 | + if memory_value > 200000000: |
| 100 | + memory_warning = f"Your solution is using a large amount of memory ({memory_value/1000000:.1f} MB). Consider optimizing to reduce memory usage." |
| 101 | + |
| 102 | + if memory_warning: |
| 103 | + memory = f"{memory} [bold yellow](!)[/]" |
| 104 | + |
| 105 | + passed = result.get('total_correct', 0) |
| 106 | + total = result.get('total_testcases', 0) |
| 107 | + |
| 108 | + test_case_str = "N/A" |
| 109 | + if total and total > 0: |
| 110 | + percentage = (passed / total) * 100 |
| 111 | + test_case_str = f"{passed}/{total} ({percentage:.1f}%)" |
| 112 | + |
| 113 | + content_parts = [] |
| 114 | + content_parts.append(f"[cyan]⏱️ Runtime:[/] {runtime}") |
| 115 | + content_parts.append(f"[cyan]💾 Memory:[/] {memory}") |
| 116 | + |
| 117 | + if result.get('elapsed_time'): |
| 118 | + content_parts.append(f"[cyan]⏲️ Elapsed Time:[/] {result.get('elapsed_time')} ms") |
112 | 119 |
|
113 | | - if memory_warning: |
| 120 | + content_parts.append(f"[cyan]🧪 Test Cases:[/] {test_case_str}") |
| 121 | + content_string = "\n".join(content_parts) |
| 122 | + |
| 123 | + title = f"{emoji} {'Test' if is_test else 'Submission'} Result: [{status_style}]{status}[/]" |
| 124 | + console.print(Panel( |
| 125 | + content_string, |
| 126 | + title=title, |
| 127 | + border_style=border_style, |
| 128 | + box=box.ROUNDED, |
| 129 | + padding=(0, 10) |
| 130 | + )) |
| 131 | + |
| 132 | + # Display specific error messages |
| 133 | + if status == "Compile Error" or status_code == 20: |
| 134 | + error_msg = result.get('compile_error', result.get('error', 'No details available')) |
| 135 | + full_error = result.get('full_compile_error', result.get('full_error', '')) |
| 136 | + |
| 137 | + if error_msg: |
| 138 | + console.print(Panel( |
| 139 | + error_msg, |
| 140 | + title="Compilation Error", |
| 141 | + border_style="red", |
| 142 | + box=box.ROUNDED, |
| 143 | + padding=(0, 1) |
| 144 | + )) |
| 145 | + |
| 146 | + if full_error: |
114 | 147 | console.print(Panel( |
115 | | - memory_warning, |
116 | | - title="⚠️ Memory Usage Warning", |
| 148 | + full_error, |
| 149 | + title="Full Compilation Error", |
| 150 | + border_style="red", |
| 151 | + box=box.ROUNDED, |
| 152 | + padding=(0, 1) |
| 153 | + )) |
| 154 | + |
| 155 | + elif status == "Runtime Error" or status_code == 15: |
| 156 | + error_msg = result.get('runtime_error', result.get('error', 'No details available')) |
| 157 | + full_error = result.get('full_runtime_error', result.get('full_error', '')) |
| 158 | + |
| 159 | + if error_msg: |
| 160 | + console.print(Panel( |
| 161 | + error_msg, |
| 162 | + title="Runtime Error", |
117 | 163 | border_style="yellow", |
118 | | - box=box.ROUNDED |
| 164 | + box=box.ROUNDED, |
| 165 | + padding=(0, 1) |
119 | 166 | )) |
120 | 167 |
|
121 | | - if result.get('stdout'): |
| 168 | + if full_error: |
122 | 169 | console.print(Panel( |
123 | | - result.get('stdout'), |
124 | | - title="📝 Standard Output", |
125 | | - border_style="blue", |
126 | | - box=box.ROUNDED |
| 170 | + full_error, |
| 171 | + title="Full Error Trace", |
| 172 | + border_style="yellow", |
| 173 | + box=box.ROUNDED, |
| 174 | + padding=(0, 1) |
127 | 175 | )) |
128 | 176 |
|
129 | | - if result.get('output') and result.get('expected'): |
130 | | - is_wrong_answer = status == "Wrong Answer" |
| 177 | + # Display memory warning if present |
| 178 | + if memory_warning: |
| 179 | + console.print(Panel( |
| 180 | + memory_warning, |
| 181 | + title="⚠️ Memory Usage Warning", |
| 182 | + border_style="yellow", |
| 183 | + box=box.ROUNDED, |
| 184 | + padding=(0, 1) |
| 185 | + )) |
| 186 | + |
| 187 | + stdout_lines = [] |
| 188 | + if result.get('std_output_list'): |
| 189 | + current_test_case = [] |
| 190 | + for line in result.get('std_output_list', []): |
| 191 | + if line and line.strip(): |
| 192 | + current_test_case.append(line.rstrip()) |
| 193 | + elif current_test_case: |
| 194 | + stdout_lines.extend(current_test_case) |
| 195 | + current_test_case = [] |
| 196 | + |
| 197 | + if current_test_case: |
| 198 | + stdout_lines.extend(current_test_case) |
| 199 | + |
| 200 | + if stdout_lines: |
| 201 | + console.print(Panel( |
| 202 | + "\n".join(stdout_lines), |
| 203 | + title="📝 Standard Output", |
| 204 | + border_style="blue", |
| 205 | + box=box.ROUNDED, |
| 206 | + padding=(0, 1) |
| 207 | + )) |
| 208 | + elif result.get('stdout') and result.get('stdout', '').strip(): |
| 209 | + console.print(Panel( |
| 210 | + result.get('stdout', '').strip(), |
| 211 | + title="📝 Standard Output", |
| 212 | + border_style="blue", |
| 213 | + box=box.ROUNDED, |
| 214 | + padding=(0, 1) |
| 215 | + )) |
| 216 | + |
| 217 | + if result.get('code_answer') and result.get('expected_code_answer'): |
| 218 | + output_lines = [line for line in result.get('code_answer', []) if line and line.strip()] |
| 219 | + expected_lines = [line for line in result.get('expected_code_answer', []) if line and line.strip()] |
| 220 | + |
| 221 | + if output_lines or expected_lines: |
| 222 | + output = "\n".join(output_lines) |
| 223 | + expected = "\n".join(expected_lines) |
| 224 | + |
| 225 | + is_wrong_answer = status == "Wrong Answer" or (not run_success and not (status in ["Compile Error", "Runtime Error", "Time Limit Exceeded"])) |
131 | 226 |
|
132 | 227 | output_panel = Panel( |
133 | | - result.get('output', ''), |
| 228 | + output, |
134 | 229 | title="Your Output", |
135 | | - border_style="red" if is_wrong_answer else "blue" |
| 230 | + border_style="red" if is_wrong_answer else "blue", |
| 231 | + padding=(0, 1) |
136 | 232 | ) |
137 | 233 | expected_panel = Panel( |
138 | | - result.get('expected', ''), |
| 234 | + expected, |
139 | 235 | title="Expected Output", |
140 | | - border_style="green" |
| 236 | + border_style="green", |
| 237 | + padding=(0, 1) |
141 | 238 | ) |
142 | 239 | console.print(Columns([output_panel, expected_panel])) |
| 240 | + elif result.get('output') or result.get('expected'): |
| 241 | + output = (result.get('output', '') or '').strip() |
| 242 | + expected = (result.get('expected', '') or '').strip() |
143 | 243 |
|
144 | | - if status != "Accepted" and result.get('error'): |
145 | | - error_msg = result.get('error', 'No details available') |
146 | | - console.print(Panel(error_msg, title="Error Details", border_style="red")) |
147 | | - |
148 | | - if result.get('full_error'): |
149 | | - console.print(Panel( |
150 | | - result.get('full_error', ''), |
151 | | - title="Full Error Trace", |
152 | | - border_style="red", |
153 | | - box=box.ROUNDED |
154 | | - )) |
155 | | - else: |
156 | | - error_panel = Panel( |
157 | | - f"{result.get('error', 'Unknown error')}", |
158 | | - title="❌ Submission Failed", |
159 | | - border_style="red" |
| 244 | + is_wrong_answer = status == "Wrong Answer" or (not run_success and not (status in ["Compile Error", "Runtime Error", "Time Limit Exceeded"])) |
| 245 | + |
| 246 | + output_panel = Panel( |
| 247 | + output, |
| 248 | + title="Your Output", |
| 249 | + border_style="red" if is_wrong_answer else "blue", |
| 250 | + padding=(0, 1) |
160 | 251 | ) |
161 | | - console.print(error_panel) |
| 252 | + expected_panel = Panel( |
| 253 | + expected, |
| 254 | + title="Expected Output", |
| 255 | + border_style="green", |
| 256 | + padding=(0, 1) |
| 257 | + ) |
| 258 | + console.print(Columns([output_panel, expected_panel])) |
162 | 259 |
|
163 | | - if result.get('stdout'): |
164 | | - console.print(Panel( |
165 | | - result.get('stdout'), |
166 | | - title="📝 Standard Output", |
167 | | - border_style="blue", |
168 | | - box=box.ROUNDED |
169 | | - )) |
| 260 | + if not run_success and status not in ["Compile Error", "Runtime Error"] and result.get('error'): |
| 261 | + error_msg = result.get('error', 'No details available') |
| 262 | + console.print(Panel( |
| 263 | + error_msg, |
| 264 | + title="Error Details", |
| 265 | + border_style="red", |
| 266 | + padding=(0, 1) |
| 267 | + )) |
170 | 268 |
|
171 | 269 | if result.get('full_error'): |
172 | 270 | console.print(Panel( |
173 | 271 | result.get('full_error', ''), |
174 | 272 | title="Full Error Trace", |
175 | 273 | border_style="red", |
176 | | - box=box.ROUNDED |
| 274 | + box=box.ROUNDED, |
| 275 | + padding=(0, 1) |
177 | 276 | )) |
178 | 277 |
|
179 | 278 | def display_exception_error(e): |
|
0 commit comments