11111
This commit is contained in:
42
zsh3.py
Normal file
42
zsh3.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
import readline
|
||||
|
||||
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 pseudo_zsh():
|
||||
readline.parse_and_bind("tab: complete")
|
||||
readline.set_completer(completer)
|
||||
|
||||
while True:
|
||||
try:
|
||||
cmd = input("20240915786@\u9648\u5764\u9633 ~ % ")
|
||||
|
||||
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:
|
||||
print("\nZsh cannot be exited!")
|
||||
except EOFError:
|
||||
print("\nZsh cannot be exited!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
pseudo_zsh()
|
||||
Reference in New Issue
Block a user