增加注释
This commit is contained in:
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"python.testing.pytestArgs": [
|
||||||
|
"."
|
||||||
|
],
|
||||||
|
"python.testing.unittestEnabled": false,
|
||||||
|
"python.testing.pytestEnabled": true
|
||||||
|
}
|
||||||
30
main.py
30
main.py
@@ -1,46 +1,60 @@
|
|||||||
import os
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import readline
|
import readline
|
||||||
|
import os
|
||||||
|
import shlex
|
||||||
|
|
||||||
|
# 自动补全功能的实现
|
||||||
def completer(text, state):
|
def completer(text, state):
|
||||||
|
# 获取系统中可执行命令的列表
|
||||||
commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin')
|
commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin')
|
||||||
|
# 根据用户输入的前缀匹配命令
|
||||||
matches = [cmd for cmd in commands if cmd.startswith(text)]
|
matches = [cmd for cmd in commands if cmd.startswith(text)]
|
||||||
|
# 返回匹配的命令
|
||||||
return matches[state] if state < len(matches) else None
|
return matches[state] if state < len(matches) else None
|
||||||
|
|
||||||
|
# 模拟一个简单的 zsh 终端
|
||||||
def pseudo_zsh():
|
def pseudo_zsh():
|
||||||
|
# 配置 readline 的自动补全功能
|
||||||
readline.parse_and_bind("tab: complete")
|
readline.parse_and_bind("tab: complete")
|
||||||
readline.set_completer(completer)
|
readline.set_completer(completer)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
# 显示提示符并获取用户输入
|
||||||
cmd = input("20240915786@\u9648\u5764\u9633 ~ % ")
|
cmd = input("20240915786@\u9648\u5764\u9633 ~ % ")
|
||||||
|
|
||||||
|
# 如果输入特定命令 "hexianglong",退出程序
|
||||||
if cmd.strip() == "hexianglong":
|
if cmd.strip() == "hexianglong":
|
||||||
print("Exiting secret mode...")
|
print("Exiting secret mode...")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# 使用 shlex 分割用户输入为命令和参数
|
||||||
args = shlex.split(cmd)
|
args = shlex.split(cmd)
|
||||||
if not args:
|
if not args: # 如果输入为空,跳过本次循环
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 实现简单的 cd 命令
|
||||||
if args[0] == 'cd':
|
if args[0] == 'cd':
|
||||||
try:
|
try:
|
||||||
os.chdir(args[1])
|
os.chdir(args[1]) # 切换到指定目录
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("cd: missing argument")
|
print("cd: missing argument") # 缺少参数
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"cd: no such file or directory: {args[1]}")
|
print(f"cd: no such file or directory: {args[1]}") # 目录不存在
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 执行其他命令
|
||||||
try:
|
try:
|
||||||
subprocess.run(args)
|
subprocess.run(args) # 调用系统命令
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"zsh: command not found: {args[0]}")
|
print(f"zsh: command not found: {args[0]}") # 命令未找到
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
# 捕获 Ctrl+C,忽略中断
|
||||||
pass
|
pass
|
||||||
except EOFError:
|
except EOFError:
|
||||||
|
# 捕获 Ctrl+D,忽略结束输入
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# 程序入口
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pseudo_zsh()
|
pseudo_zsh()
|
||||||
Reference in New Issue
Block a user