update quit_cursor options

This commit is contained in:
yeongpin 2025-01-15 10:45:55 +08:00
parent 98adbb76b7
commit fecdcb933e
3 changed files with 97 additions and 2 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
version=1.0.6
VERSION=1.0.6

View File

@ -24,7 +24,8 @@ def print_menu():
print(f"{Fore.YELLOW}{'' * 40}{Style.RESET_ALL}") print(f"{Fore.YELLOW}{'' * 40}{Style.RESET_ALL}")
print(f"{Fore.GREEN}0{Style.RESET_ALL}. {EMOJI['ERROR']} Exit Program | 退出程序") print(f"{Fore.GREEN}0{Style.RESET_ALL}. {EMOJI['ERROR']} Exit Program | 退出程序")
print(f"{Fore.GREEN}1{Style.RESET_ALL}. {EMOJI['RESET']} Reset Machine Manual | 重置机器标识") print(f"{Fore.GREEN}1{Style.RESET_ALL}. {EMOJI['RESET']} Reset Machine Manual | 重置机器标识")
print(f"{Fore.GREEN}2{Style.RESET_ALL}. {EMOJI['RESET']} Register Cursor | 注册 Cursor") print(f"{Fore.GREEN}2{Style.RESET_ALL}. {EMOJI['SUCCESS']} Register Cursor | 注册 Cursor")
print(f"{Fore.GREEN}3{Style.RESET_ALL}. {EMOJI['ERROR']} Quit Cursor | 退出 Cursor")
# 在这里添加更多选项 # 在这里添加更多选项
print(f"{Fore.YELLOW}{'' * 40}{Style.RESET_ALL}") print(f"{Fore.YELLOW}{'' * 40}{Style.RESET_ALL}")
@ -34,7 +35,7 @@ def main():
while True: while True:
try: try:
choice = input(f"\n{EMOJI['ARROW']} {Fore.CYAN}Enter your choice (0-2) | 输入选择 (0-2): {Style.RESET_ALL}") choice = input(f"\n{EMOJI['ARROW']} {Fore.CYAN}Enter your choice (0-3) | 输入选择 (0-3): {Style.RESET_ALL}")
if choice == "0": if choice == "0":
print(f"\n{Fore.YELLOW}{EMOJI['INFO']} Exiting program... | 正在退出程序...{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}{EMOJI['INFO']} Exiting program... | 正在退出程序...{Style.RESET_ALL}")
@ -48,6 +49,10 @@ def main():
import cursor_register import cursor_register
cursor_register.main() cursor_register.main()
break break
elif choice == "3":
import quit_cursor
quit_cursor.quit_cursor()
break
else: else:
print(f"{Fore.RED}{EMOJI['ERROR']} Invalid choice. Please try again | 无效选择,请重试{Style.RESET_ALL}") print(f"{Fore.RED}{EMOJI['ERROR']} Invalid choice. Please try again | 无效选择,请重试{Style.RESET_ALL}")
print_menu() print_menu()

88
quit_cursor.py Normal file
View File

@ -0,0 +1,88 @@
import psutil
import time
from colorama import Fore, Style, init
# 初始化colorama
init()
# 定义emoji和颜色常量
EMOJI = {
"FILE": "📄",
"BACKUP": "💾",
"SUCCESS": "",
"ERROR": "",
"INFO": "",
"RESET": "🔄",
"PROCESS": "⚙️",
"WAIT": ""
}
class CursorQuitter:
def __init__(self, timeout=5):
self.timeout = timeout
def quit_cursor(self):
"""温和地关闭 Cursor 进程"""
try:
print(f"{Fore.CYAN}{EMOJI['PROCESS']} Start Quitting Cursor | 开始退出 Cursor...{Style.RESET_ALL}")
cursor_processes = []
# 收集所有 Cursor 进程
for proc in psutil.process_iter(['pid', 'name']):
try:
if proc.info['name'].lower() in ['cursor.exe', 'cursor']:
cursor_processes.append(proc)
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if not cursor_processes:
print(f"{Fore.GREEN}{EMOJI['INFO']} No Running Cursor Process | 未发现运行中的 Cursor 进程{Style.RESET_ALL}")
return True
# 温和地请求进程终止
for proc in cursor_processes:
try:
if proc.is_running():
print(f"{Fore.YELLOW}{EMOJI['PROCESS']} Terminating Process | 正在终止进程 {proc.pid}...{Style.RESET_ALL}")
proc.terminate() # 发送终止信号
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
# 等待进程自然终止
print(f"{Fore.CYAN}{EMOJI['WAIT']} Waiting for Process to Exit | 等待进程退出...{Style.RESET_ALL}")
start_time = time.time()
while time.time() - start_time < self.timeout:
still_running = []
for proc in cursor_processes:
try:
if proc.is_running():
still_running.append(proc)
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if not still_running:
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} All Cursor Processes Closed | 所有 Cursor 进程已正常关闭{Style.RESET_ALL}")
return True
# 等待一小段时间再检查
time.sleep(0.5)
# 如果超时后仍有进程在运行
if still_running:
process_list = ", ".join([str(p.pid) for p in still_running])
print(f"{Fore.RED}{EMOJI['ERROR']} Process Timeout | 以下进程未能在规定时间内关闭: {process_list}{Style.RESET_ALL}")
return False
return True
except Exception as e:
print(f"{Fore.RED}{EMOJI['ERROR']} Error Occurred | 关闭 Cursor 进程时发生错误: {str(e)}{Style.RESET_ALL}")
return False
def quit_cursor(timeout=5):
"""便捷函数,用于直接调用退出功能"""
quitter = CursorQuitter(timeout)
return quitter.quit_cursor()
if __name__ == "__main__":
quit_cursor()