同步zsh.py->main.py

This commit is contained in:
2025-04-02 10:53:37 +08:00
Unverified
parent 331b23a20c
commit 8061acbc75

74
main.py
View File

@@ -1,36 +1,46 @@
import os
import re
import signal
import readline # 用于支持历史记录
import shlex
import subprocess
import readline
# 标志位,用于处理信号
interrupted = False
def completer(text, state):
commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin')
matches = [cmd for cmd in commands if cmd.startswith(text)]
return matches[state] if state < len(matches) else None
def signal_handler(signal, frame):
global interrupted
interrupted = True # 设置标志位
def pseudo_zsh():
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
while True:
try:
cmd = input("20240915786@\u9648\u5764\u9633 ~ % ")
if cmd.strip() == "hexianglong":
print("Exiting secret mode...")
break
args = shlex.split(cmd)
if not args:
continue
if args[0] == 'cd':
try:
os.chdir(args[1])
except IndexError:
print("cd: missing argument")
except FileNotFoundError:
print(f"cd: no such file or directory: {args[1]}")
continue
try:
subprocess.run(args)
except FileNotFoundError:
print(f"zsh: command not found: {args[0]}")
except KeyboardInterrupt:
pass
except EOFError:
pass
signal.signal(signal.SIGINT, signal_handler)
os.system('clear')
print(f"Last login: Tue Apr 1 20:23:24 on ttys001")
def shell():
dir = os.getcwd()
if dir == '/':
CmdDir = '/'
elif re.match(dir, "/home/*"): # 用户目录下的任意文件夹
CmdDir = '~'
else:
CmdDir = dir.split('/')[-1]
print(f'20250910553@何相龙 {CmdDir} % ', end='')
cmd = input().strip() # 去除输入中的多余空白字符,包括换行符
if cmd: # 如果输入不为空,保存到历史记录
readline.add_history(cmd)
os.system(cmd)
while True:
if interrupted:
print("\nSignal received. Type 'exit' to quit.")
interrupted = False # 重置标志位
shell()
if __name__ == "__main__":
pseudo_zsh()