NewyearDOS 0.029更新了!
添加了一個(gè)新的功能;語言切換,可以切換中/英,源碼:
import os
print("歡迎,版本號:0.029")
class NewyearDOS:
? ? def __init__(self):
? ? ? ? self.current_dir = os.getcwd()
? ? ? ? self.language = "english"? # 默認(rèn)語言為英語
? ? def run(self):
? ? ? ? while True:
? ? ? ? ? ? command = input(f"{self.current_dir}> ")
? ? ? ? ? ? self.execute_command(command)
? ? def execute_command(self, command):
? ? ? ? command_parts = command.split()
? ? ? ? if command_parts:
? ? ? ? ? ? cmd = command_parts[0].lower()
? ? ? ? ? ? args = command_parts[1:]
? ? ? ? ? ? if cmd == "cd":
? ? ? ? ? ? ? ? self.change_directory(args)
? ? ? ? ? ? elif cmd == "dir":
? ? ? ? ? ? ? ? self.list_directory()
? ? ? ? ? ? elif cmd == "mkdir":
? ? ? ? ? ? ? ? self.create_directory(args)
? ? ? ? ? ? elif cmd == "rmdir":
? ? ? ? ? ? ? ? self.remove_directory(args)
? ? ? ? ? ? elif cmd == "exit":
? ? ? ? ? ? ? ? exit()
? ? ? ? ? ? elif cmd == "help":
? ? ? ? ? ? ? ? self.show_help()
? ? ? ? ? ? elif cmd == "lang":
? ? ? ? ? ? ? ? self.set_language(args)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print(self.get_text("invalid_command"))
? ? def change_directory(self, args):
? ? ? ? if args:
? ? ? ? ? ? new_dir = os.path.join(self.current_dir, args[0])
? ? ? ? ? ? if os.path.isdir(new_dir):
? ? ? ? ? ? ? ? self.current_dir = new_dir
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print(self.get_text("directory_not_found"))
? ? ? ? else:
? ? ? ? ? ? print(self.get_text("missing_directory_name"))
? ? def list_directory(self):
? ? ? ? files = os.listdir(self.current_dir)
? ? ? ? for file in files:
? ? ? ? ? ? file_path = os.path.join(self.current_dir, file)
? ? ? ? ? ? if os.path.isdir(file_path):
? ? ? ? ? ? ? ? print(f"<DIR>\t{file}")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print(f"\t{file}")
? ? def create_directory(self, args):
? ? ? ? if args:
? ? ? ? ? ? new_dir = os.path.join(self.current_dir, args[0])
? ? ? ? ? ? try:
? ? ? ? ? ? ? ? os.mkdir(new_dir)
? ? ? ? ? ? ? ? print(self.get_text("directory_created").format(args[0]))
? ? ? ? ? ? except:
? ? ? ? ? ? ? ? print(self.get_text("failed_to_create_directory"))
? ? ? ? else:
? ? ? ? ? ? print(self.get_text("missing_directory_name"))
? ? def remove_directory(self, args):
? ? ? ? if args:
? ? ? ? ? ? dir_path = os.path.join(self.current_dir, args[0])
? ? ? ? ? ? if os.path.isdir(dir_path):
? ? ? ? ? ? ? ? try:
? ? ? ? ? ? ? ? ? ? os.rmdir(dir_path)
? ? ? ? ? ? ? ? ? ? print(self.get_text("directory_removed").format(args[0]))
? ? ? ? ? ? ? ? except:
? ? ? ? ? ? ? ? ? ? print(self.get_text("failed_to_remove_directory"))
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print(self.get_text("directory_not_found"))
? ? ? ? else:
? ? ? ? ? ? print(self.get_text("missing_directory_name"))
? ? def show_help(self):
? ? ? ? print(self.get_text("available_commands"))
? ? ? ? print("cd <directory> - " + self.get_text("change_directory"))
? ? ? ? print("dir - " + self.get_text("list_directory"))
? ? ? ? print("mkdir <directory> - " + self.get_text("create_directory"))
? ? ? ? print("rmdir <directory> - " + self.get_text("remove_directory"))
? ? ? ? print("exit - " + self.get_text("exit_program"))
? ? ? ? print("help - " + self.get_text("show_help"))
? ? ? ? print("lang <language> - " + self.get_text("set_language"))
? ? def set_language(self, args):
? ? ? ? if args:
? ? ? ? ? ? language = args[0].lower()
? ? ? ? ? ? if language == "english":
? ? ? ? ? ? ? ? self.language = "english"
? ? ? ? ? ? ? ? print("Language set to English")
? ? ? ? ? ? elif language == "chinese":
? ? ? ? ? ? ? ? self.language = "chinese"
? ? ? ? ? ? ? ? print("語言設(shè)置為中文")
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print("Unsupported language")
? ? ? ? else:
? ? ? ? ? ? print("Missing language option")
? ? def get_text(self, key):
? ? ? ? if self.language == "chinese":
? ? ? ? ? ? if key == "invalid_command":
? ? ? ? ? ? ? ? return "無效命令"
? ? ? ? ? ? elif key == "directory_not_found":
? ? ? ? ? ? ? ? return "目錄不存在"
? ? ? ? ? ? elif key == "missing_directory_name":
? ? ? ? ? ? ? ? return "缺少目錄名稱"
? ? ? ? ? ? elif key == "directory_created":
? ? ? ? ? ? ? ? return "目錄 '{}' 創(chuàng)建成功"
? ? ? ? ? ? elif key == "failed_to_create_directory":
? ? ? ? ? ? ? ? return "無法創(chuàng)建目錄"
? ? ? ? ? ? elif key == "directory_removed":
? ? ? ? ? ? ? ? return "目錄 '{}' 移除成功"
? ? ? ? ? ? elif key == "failed_to_remove_directory":
? ? ? ? ? ? ? ? return "無法移除目錄"
? ? ? ? ? ? elif key == "available_commands":
? ? ? ? ? ? ? ? return "可用命令:"
? ? ? ? ? ? elif key == "change_directory":
? ? ? ? ? ? ? ? return "切換當(dāng)前目錄"
? ? ? ? ? ? elif key == "list_directory":
? ? ? ? ? ? ? ? return "列出文件和目錄"
? ? ? ? ? ? elif key == "create_directory":
? ? ? ? ? ? ? ? return "創(chuàng)建新目錄"
? ? ? ? ? ? elif key == "remove_directory":
? ? ? ? ? ? ? ? return "移除目錄"
? ? ? ? ? ? elif key == "exit_program":
? ? ? ? ? ? ? ? return "退出程序"
? ? ? ? ? ? elif key == "show_help":
? ? ? ? ? ? ? ? return "顯示可用命令和描述"
? ? ? ? ? ? elif key == "set_language":
? ? ? ? ? ? ? ? return "設(shè)置語言"
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? return key? # 如果找不到相應(yīng)的鍵,則返回鍵本身
? ? ? ? else:
? ? ? ? ? ? if key == "invalid_command":
? ? ? ? ? ? ? ? return "Invalid command"
? ? ? ? ? ? elif key == "directory_not_found":
? ? ? ? ? ? ? ? return "Directory not found"
? ? ? ? ? ? elif key == "missing_directory_name":
? ? ? ? ? ? ? ? return "Missing directory name"
? ? ? ? ? ? elif key == "directory_created":
? ? ? ? ? ? ? ? return "Directory '{}' created successfully"
? ? ? ? ? ? elif key == "failed_to_create_directory":
? ? ? ? ? ? ? ? return "Failed to create directory"
? ? ? ? ? ? elif key == "directory_removed":
? ? ? ? ? ? ? ? return "Directory '{}' removed successfully"
? ? ? ? ? ? elif key == "failed_to_remove_directory":
? ? ? ? ? ? ? ? return "Failed to remove directory"
? ? ? ? ? ? elif key == "available_commands":
? ? ? ? ? ? ? ? return "Available commands:"
? ? ? ? ? ? elif key == "change_directory":
? ? ? ? ? ? ? ? return "Change current directory"
? ? ? ? ? ? elif key == "list_directory":
? ? ? ? ? ? ? ? return "List files and directories"
? ? ? ? ? ? elif key == "create_directory":
? ? ? ? ? ? ? ? return "Create new directory"
? ? ? ? ? ? elif key == "remove_directory":
? ? ? ? ? ? ? ? return "Remove directory"
? ? ? ? ? ? elif key == "exit_program":
? ? ? ? ? ? ? ? return "Exit program"
? ? ? ? ? ? elif key == "show_help":
? ? ? ? ? ? ? ? return "Display available commands and descriptions"
? ? ? ? ? ? elif key == "set_language":
? ? ? ? ? ? ? ? return "Set language"
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? return key? # If the corresponding key is not found, return the key itself
if __name__ == "__main__":
? ? dos = NewyearDOS()
? ? dos.run()
看完點(diǎn)個(gè)贊!