Skip to content

Commit 44af22b

Browse files
committed
Fix command handling and exit behavior
- Remove redundant screen clearing and agent restart on Ctrl+D/Ctrl+C - Change online/agent command behavior to be consistent - Improve exit handling to properly terminate the program
1 parent b2c3539 commit 44af22b

File tree

1 file changed

+79
-49
lines changed

1 file changed

+79
-49
lines changed

agent.py

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ def visible_length(s):
6060
# for line in lines:
6161
# print(" " * padding + line)
6262
# sys.stdout.flush()
63-
#
64-
#
63+
#
64+
#
6565
# try:
6666
# for padding in range(0, terminal_width - max_width + 1):
6767
# display_at_position(padding)
6868
# time.sleep(0.05)
69-
#
69+
#
7070
# center_position = (terminal_width - max_width) // 2
7171
# for padding in range(terminal_width - max_width, center_position - 1, -1):
7272
# display_at_position(padding)
7373
# time.sleep(0.05)
74-
#
74+
#
7575
# display_at_position(center_position)
76-
#
76+
#
7777
# except KeyboardInterrupt:
7878
# clear_screen()
7979
# for line in lines:
8080
# print(line)
81-
#
81+
#
8282

8383
def get_user_rating(username):
8484
try:
@@ -120,7 +120,8 @@ def draw_ascii_chart(data, width=60, height=20):
120120
if range_rating == 0:
121121
range_rating = 1
122122

123-
normalized = [int((r - min_rating) / range_rating * (height - 1)) for r in ratings]
123+
normalized = [int((r - min_rating) / range_rating * (height - 1))
124+
for r in ratings]
124125

125126
chart = []
126127
for y in range(height - 1, -1, -1):
@@ -152,10 +153,12 @@ def draw_ascii_chart(data, width=60, height=20):
152153
def show_rating_history(username):
153154
rating_data = get_user_rating(username)
154155
if not rating_data:
155-
console.print(f"[bold red]Could not fetch rating data for {username}[/]")
156+
console.print(
157+
f"[bold red]Could not fetch rating data for {username}[/]")
156158
return
157159

158-
console.print(f"[bright_green]\nCodeforces Rating History ({username}):[/]")
160+
console.print(
161+
f"[bright_green]\nCodeforces Rating History ({username}):[/]")
159162
console.print("[bright_yellow]=[/]" * 70)
160163

161164
current_rating = rating_data[-1][3]
@@ -176,7 +179,8 @@ def show_rating_history(username):
176179
console.print("[bright_red] \nLast 5 Contests: [/]")
177180
for contest in rating_data[-5:]:
178181
date = datetime.fromtimestamp(contest[2]).strftime("%Y-%m-%d")
179-
console.print(f"[bright_green]{date}: {contest[1]} (Rating: {contest[3]})[/]")
182+
console.print(
183+
f"[bright_green]{date}: {contest[1]} (Rating: {contest[3]})[/]")
180184

181185

182186
def main():
@@ -201,7 +205,8 @@ def main():
201205

202206
elif command == "play":
203207
contest_name = input("Enter contest name: ")
204-
webbrowser.open(f"https://atcoder.jp/contests/{contest_name}")
208+
webbrowser.open(
209+
f"https://atcoder.jp/contests/{contest_name}")
205210

206211
elif command == "task":
207212
contest_name = input("Enter contest name: ")
@@ -213,11 +218,13 @@ def main():
213218

214219
elif command == "submit":
215220
contest_name = input("Enter contest name: ")
216-
webbrowser.open(f"https://atcoder.jp/contests/{contest_name}/submit")
221+
webbrowser.open(
222+
f"https://atcoder.jp/contests/{contest_name}/submit")
217223

218224
elif command == "stand":
219225
contest_name = input("Enter contest name: ")
220-
webbrowser.open(f"https://atcoder.jp/contests/{contest_name}/standings")
226+
webbrowser.open(
227+
f"https://atcoder.jp/contests/{contest_name}/standings")
221228

222229
elif command == "code":
223230
user = input("Enter your Mac username: ")
@@ -226,7 +233,8 @@ def main():
226233
folder_name = input("Enter contest name for folder: ")
227234
os.mkdir(folder_name)
228235
os.chdir(f"/Users/{user}/Desktop/{folder_name}")
229-
os.system("touch A.cpp B.cpp C.cpp D.cpp E.cpp F.cpp G.cpp")
236+
os.system(
237+
"touch A.cpp B.cpp C.cpp D.cpp E.cpp F.cpp G.cpp")
230238

231239
cpp_code = """#include <bits/stdc++.h>
232240
@@ -282,7 +290,8 @@ def main():
282290
elif command == "time":
283291
console.print(
284292
time.strftime(
285-
"[bright_red]%Y-%m-%d %H:%M:%S[/]", time.localtime(time.time())
293+
"[bright_red]%Y-%m-%d %H:%M:%S[/]", time.localtime(
294+
time.time())
286295
)
287296
)
288297

@@ -295,26 +304,32 @@ def main():
295304
print(
296305
"\033[34m| $$ | $$| $$__ $$ /$$__ $$ |____ $$ /$$__ $$\033[0m"
297306
)
298-
print("\033[34m| $$ | $$| $$ | $$| $$_____/ /$$__ $$| $$\033[0m")
299-
print("\033[34m| $$$$$$$$| $$| $$ | $$| $$$$$$$| $$$$$$$| $$ \033[0m")
300-
print("\033[34m|________/|__/|__/ |__/ \\_______/ \\_______/|__/ \033[0m")
307+
print(
308+
"\033[34m| $$ | $$| $$ | $$| $$_____/ /$$__ $$| $$\033[0m")
309+
print(
310+
"\033[34m| $$$$$$$$| $$| $$ | $$| $$$$$$$| $$$$$$$| $$ \033[0m")
311+
print(
312+
"\033[34m|________/|__/|__/ |__/ \\_______/ \\_______/|__/ \033[0m")
301313

302-
print("\033[34mWeChat QR code payment to support this project:\033[0m")
314+
print(
315+
"\033[34mWeChat QR code payment to support this project:\033[0m")
303316
code = "wxp://f2f08Xmtax1P6TX2gayuRlMjXvgWRIJSXz5TmEnDiWWHgLLc3W7dIqFeUqjb4g8DAPp4"
304317
qr = qrcode.QRCode(version=1, box_size=1, border=1)
305318
qr.add_data(code)
306319
qr.print_ascii()
307320

308321
print("\033[34mDeveloped by Linus Shyu\033[0m")
309-
print("\033[34mSupport this project to keep it running!\033[0m")
322+
print(
323+
"\033[34mSupport this project to keep it running!\033[0m")
310324

311325
print(
312326
"GitHub Repository: \033[4mhttps://github.com/Linus-Shyu/AT-Tool\033[0m"
313327
)
314328
print(
315329
"Developer Bilibili: \033[4mhttps://space.bilibili.com/411591950\033[0m"
316330
)
317-
print("Developer YouTube: \033[4mhttps://www.youtube.com/@LinusShyu\033[0m")
331+
print(
332+
"Developer YouTube: \033[4mhttps://www.youtube.com/@LinusShyu\033[0m")
318333
print(" ")
319334
print(" ")
320335
print("----------------------------------------------------")
@@ -358,28 +373,45 @@ def main():
358373

359374
elif command == "help":
360375
console.print("[bright_yellow]\nAvailable commands:[/]")
361-
console.print("[bright_yellow]online - Open online agent[/]")
362-
console.print("[bright_yellow]agent - Open local agent[/]")
363-
console.print("[bright_yellow]home - Open AtCoder home page[/]")
364-
console.print("[bright_yellow]contest - Open contests page[/]")
365-
console.print("[bright_yellow]rank - Open global rankings[/]")
366-
console.print("[bright_yellow]userdata - View user profile[/]")
367-
console.print("[bright_yellow]play - Open specific contest[/]")
368-
console.print("[bright_yellow]task - Open specific problem (a-g)[/]")
369-
console.print("[bright_yellow]submit - Open submission page[/]")
370-
console.print("[bright_yellow]stand - Open contest standings[/]")
376+
console.print(
377+
"[bright_yellow]online - Open online agent[/]")
378+
console.print(
379+
"[bright_yellow]agent - Open local agent[/]")
380+
console.print(
381+
"[bright_yellow]home - Open AtCoder home page[/]")
382+
console.print(
383+
"[bright_yellow]contest - Open contests page[/]")
384+
console.print(
385+
"[bright_yellow]rank - Open global rankings[/]")
386+
console.print(
387+
"[bright_yellow]userdata - View user profile[/]")
388+
console.print(
389+
"[bright_yellow]play - Open specific contest[/]")
390+
console.print(
391+
"[bright_yellow]task - Open specific problem (a-g)[/]")
392+
console.print(
393+
"[bright_yellow]submit - Open submission page[/]")
394+
console.print(
395+
"[bright_yellow]stand - Open contest standings[/]")
371396
console.print(
372397
"[bright_yellow]code - Create C++ template files for contest[/]"
373398
)
374-
console.print("[bright_yellow]clear - Clear terminal screen[/]")
375-
console.print("[bright_yellow]time - Show current time[/]")
376-
console.print("[bright_yellow]rating - Show Codeforces rating graph[/]")
377-
console.print("[bright_yellow]about - Show about information[/]")
378-
console.print("[bright_yellow]exit - Exit the program[/]")
379-
console.print("[bright_yellow]help - Show this help message\n[/]")
399+
console.print(
400+
"[bright_yellow]clear - Clear terminal screen[/]")
401+
console.print(
402+
"[bright_yellow]time - Show current time[/]")
403+
console.print(
404+
"[bright_yellow]rating - Show Codeforces rating graph[/]")
405+
console.print(
406+
"[bright_yellow]about - Show about information[/]")
407+
console.print(
408+
"[bright_yellow]exit - Exit the program[/]")
409+
console.print(
410+
"[bright_yellow]help - Show this help message\n[/]")
380411

381412
elif command == "rating":
382-
username = console.input("[bright_white]Enter Codeforces username: [/]")
413+
username = console.input(
414+
"[bright_white]Enter Codeforces username: [/]")
383415
show_rating_history(username)
384416

385417
elif command.strip() == "":
@@ -404,23 +436,21 @@ def main():
404436
rich.print(Markdown(line), end="")
405437
if problem == "quit":
406438
break
407-
elif command == "agent":
439+
elif command == "online":
440+
time.sleep(1)
408441
os.system("./axec")
409442

410443
else:
411-
console.print("[cyan]Command not found. Type 'help' for available commands.[/]")
444+
console.print(
445+
"[cyan]Command not found. Type 'help' for available commands.[/]")
412446

413447
except EOFError: # Ctrl+D
414-
console.print("\n[bright_red]Clearing screen...[/]")
415-
os.system('cls' if os.name == 'nt' else 'clear')
416-
os.system("./axec")
417-
continue
418-
448+
console.print("\n[bright_red]Exiting program...[/]")
449+
sys.exit(0)
450+
419451
except KeyboardInterrupt: # Ctrl+C
420-
os.system("./axec")
421-
console.print("\n[bright_red]Clearing screen...[/]")
422-
os.system('cls' if os.name == 'nt' else 'clear')
423-
continue
452+
console.print("\n[bright_red]Exiting program...[/]")
453+
sys.exit(0)
424454

425455
except KeyboardInterrupt: # Outer Ctrl+C handler
426456
console.print("\n[bright_red]Exiting program...[/]")

0 commit comments

Comments
 (0)