using System; using System.Collections.Generic; namespace CMLeonOS.Commands { public static class HelpCommand { private class CommandInfo { public string Command { get; set; } public string Parameters { get; set; } public string Description { get; set; } public SubCommandInfo[] SubCommands { get; set; } } private class SubCommandInfo { public string Command { get; set; } public string Description { get; set; } } private static readonly List allCommands = new List { new CommandInfo { Command = "echo", Parameters = "", Description = "Display text (supports \\n for newline)" }, new CommandInfo { Command = "clear/cls", Parameters = "", Description = "Clear screen" }, new CommandInfo { Command = "restart", Parameters = "", Description = "Restart system" }, new CommandInfo { Command = "shutdown", Parameters = "", Description = "Shutdown system" }, new CommandInfo { Command = "time", Parameters = "", Description = "Display current time" }, new CommandInfo { Command = "date", Parameters = "", Description = "Display current date" }, new CommandInfo { Command = "prompt", Parameters = "", Description = "Change command prompt" }, new CommandInfo { Command = "calc", Parameters = "", Description = "Simple calculator" }, new CommandInfo { Command = "calcgui", Parameters = "", Description = "TUI calculator" }, new CommandInfo { Command = "history", Parameters = "", Description = "Show command history" }, new CommandInfo { Command = "background", Parameters = "", Description = "Change background color" }, new CommandInfo { Command = "cuitest", Parameters = "", Description = "Test CUI framework" }, new CommandInfo { Command = "labyrinth", Parameters = "", Description = "Play maze escape game" }, new CommandInfo { Command = "snake", Parameters = "", Description = "Play snake game" }, new CommandInfo { Command = "edit", Parameters = "", Description = "Simple code editor", SubCommands = new[] { new SubCommandInfo { Command = "", Description = "Tab key inserts 4 spaces" } } }, new CommandInfo { Command = "ls", Parameters = "", Description = "List files and directories" }, new CommandInfo { Command = "cd", Parameters = "", Description = "Change directory", SubCommands = new[] { new SubCommandInfo { Command = "cd ..", Description = "Go to parent directory" }, new SubCommandInfo { Command = "cd dir1/dir2/dir3", Description = "Go to numbered directory" } } }, new CommandInfo { Command = "pwd", Parameters = "", Description = "Show current directory" }, new CommandInfo { Command = "mkdir", Parameters = "", Description = "Create directory" }, new CommandInfo { Command = "rm", Parameters = "", Description = "Remove file", SubCommands = new[] { new SubCommandInfo { Command = "", Description = "Use -norisk to delete files in sys folder" } } }, new CommandInfo { Command = "rmdir", Parameters = "", Description = "Remove directory" }, new CommandInfo { Command = "cat", Parameters = "", Description = "Display file content" }, new CommandInfo { Command = "echo", Parameters = " > ", Description = "Write text to file" }, new CommandInfo { Command = "head", Parameters = "", Description = "Display first lines of file", SubCommands = new[] { new SubCommandInfo { Command = "head ", Description = "Usage" } } }, new CommandInfo { Command = "tail", Parameters = "", Description = "Display last lines of file", SubCommands = new[] { new SubCommandInfo { Command = "tail ", Description = "Usage" } } }, new CommandInfo { Command = "wc", Parameters = "", Description = "Count lines, words, characters" }, new CommandInfo { Command = "cp", Parameters = " ", Description = "Copy file" }, new CommandInfo { Command = "mv", Parameters = " ", Description = "Move/rename file" }, new CommandInfo { Command = "touch", Parameters = "", Description = "Create empty file" }, new CommandInfo { Command = "find", Parameters = "", Description = "Find file" }, new CommandInfo { Command = "getdisk", Parameters = "", Description = "Show disk information" }, new CommandInfo { Command = "user", Parameters = "", Description = "User management", SubCommands = new[] { new SubCommandInfo { Command = "user add admin ", Description = "Add admin user" }, new SubCommandInfo { Command = "user add user ", Description = "Add regular user" }, new SubCommandInfo { Command = "user delete ", Description = "Delete user" }, new SubCommandInfo { Command = "user list", Description = "List all users" } } }, new CommandInfo { Command = "cpass", Parameters = "", Description = "Change password" }, new CommandInfo { Command = "env", Parameters = "", Description = "Environment variables", SubCommands = new[] { new SubCommandInfo { Command = "env see ", Description = "Show variable value" }, new SubCommandInfo { Command = "env change ", Description = "Set variable value" }, new SubCommandInfo { Command = "env delete ", Description = "Delete variable" } } }, new CommandInfo { Command = "beep", Parameters = "", Description = "Play beep sound" }, new CommandInfo { Command = "uptime", Parameters = "", Description = "Show system uptime" }, new CommandInfo { Command = "ps", Parameters = "", Description = "List all running processes" }, new CommandInfo { Command = "kill", Parameters = "", Description = "Kill a process by ID" }, new CommandInfo { Command = "matrix", Parameters = "", Description = "Show Matrix effect (The Matrix movie)" }, new CommandInfo { Command = "app", Parameters = "", Description = "Application manager" }, new CommandInfo { Command = "branswe", Parameters = "", Description = "Execute Branswe code file" }, new CommandInfo { Command = "grep", Parameters = " ", Description = "Search text in file" }, new CommandInfo { Command = "ping", Parameters = "", Description = "Ping IP address (5 times)" }, new CommandInfo { Command = "tcpserver", Parameters = "", Description = "Start TCP server on specified port" }, new CommandInfo { Command = "tcpclient", Parameters = " ", Description = "Connect to TCP server" }, new CommandInfo { Command = "wget", Parameters = "", Description = "Download file from URL" }, new CommandInfo { Command = "setdns", Parameters = "", Description = "Set DNS server" }, new CommandInfo { Command = "setgateway", Parameters = "", Description = "Set gateway" }, new CommandInfo { Command = "ipconfig", Parameters = "", Description = "Show network configuration" }, new CommandInfo { Command = "nslookup", Parameters = "", Description = "DNS lookup" }, new CommandInfo { Command = "whoami", Parameters = "", Description = "Show current username" }, new CommandInfo { Command = "base64", Parameters = "encrypt ", Description = "Encode text to Base64" }, new CommandInfo { Command = "base64", Parameters = "decrypt ", Description = "Decode Base64 to text" }, new CommandInfo { Command = "alias", Parameters = " ", Description = "Create command alias", SubCommands = new[] { new SubCommandInfo { Command = "alias", Description = "List all aliases" } } }, new CommandInfo { Command = "unalias", Parameters = "", Description = "Remove command alias" }, new CommandInfo { Command = "lua", Parameters = "", Description = "Execute Lua script" }, new CommandInfo { Command = "lua2cla", Parameters = "", Description = "Convert Lua to CLA format" }, new CommandInfo { Command = "cla", Parameters = "", Description = "Run CLA application" }, new CommandInfo { Command = "version", Parameters = "", Description = "Show OS version" }, new CommandInfo { Command = "about", Parameters = "", Description = "Show about information" }, new CommandInfo { Command = "settings", Parameters = " [value]", Description = "View or modify system settings", SubCommands = new[] { new SubCommandInfo { Command = "settings", Description = "List all settings" } } }, new CommandInfo { Command = "help", Parameters = "", Description = "Show help page (1-5)", SubCommands = new[] { new SubCommandInfo { Command = "help all", Description = "Show all help pages" } } } }; private const int CommandsPerPage = 15; private static int GetCommandLinesCount(CommandInfo cmd) { int lines = 1; if (cmd.SubCommands != null && cmd.SubCommands.Length > 0) { lines += cmd.SubCommands.Length; } return lines; } public static void ProcessHelp(string args) { string[] helpArgs = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int pageNumber = 1; bool showAll = false; if (helpArgs.Length > 0) { if (helpArgs[0].ToLower() == "all") { showAll = true; } else if (int.TryParse(helpArgs[0], out int page)) { pageNumber = page; } } int totalLines = 0; foreach (var cmd in allCommands) { totalLines += GetCommandLinesCount(cmd); } int totalPages = (int)Math.Ceiling((double)totalLines / CommandsPerPage); if (showAll) { ShowAllPages(); } else { ShowPage(pageNumber, totalPages); } } private static void ShowAllPages() { Console.WriteLine("===================================="); Console.WriteLine(" Help - All Pages"); Console.WriteLine("===================================="); Console.WriteLine(); foreach (var cmd in allCommands) { DisplayCommand(cmd); } Console.WriteLine(); Console.WriteLine("-- End of help --"); } private static void ShowPage(int pageNumber, int totalPages) { if (pageNumber > totalPages) { pageNumber = totalPages; } if (pageNumber < 1) { pageNumber = 1; } Console.WriteLine("===================================="); Console.WriteLine($" Help - Page {pageNumber}/{totalPages}"); Console.WriteLine("===================================="); Console.WriteLine(); int linesOnPage = 0; int currentLine = 0; for (int i = 0; i < allCommands.Count; i++) { int cmdLines = GetCommandLinesCount(allCommands[i]); if (currentLine + cmdLines <= (pageNumber - 1) * CommandsPerPage) { currentLine += cmdLines; continue; } if (linesOnPage >= CommandsPerPage) { break; } DisplayCommand(allCommands[i]); linesOnPage += cmdLines; currentLine += cmdLines; } if (pageNumber < totalPages) { Console.WriteLine(); Console.WriteLine($"-- More -- Type 'help {pageNumber + 1}' for next page or 'help all' for all pages --"); } else { Console.WriteLine(); Console.WriteLine("-- End of help --"); } } private static void DisplayCommand(CommandInfo cmd) { int maxCommandWidth = 18; int maxParamWidth = 18; int subCommandIndent = 21; int maxSubCommandWidth = 17; string commandPart = PadRight(cmd.Command, maxCommandWidth); string paramPart = PadRight(cmd.Parameters, maxParamWidth); Console.WriteLine($" {commandPart} {paramPart} - {cmd.Description}"); if (cmd.SubCommands != null && cmd.SubCommands.Length > 0) { string indent = new string(' ', subCommandIndent); foreach (var subCmd in cmd.SubCommands) { string subCmdPart = PadRight(subCmd.Command, maxSubCommandWidth); Console.WriteLine($"{indent}{subCmdPart} - {subCmd.Description}"); } } } private static string PadRight(string str, int totalWidth) { if (string.IsNullOrEmpty(str)) { return new string(' ', totalWidth); } if (str.Length >= totalWidth) { return str; } return str + new string(' ', totalWidth - str.Length); } } }