private static void AliasCommand() { List <string> commandWords = new List <string> (Display.Command.Trim().Split(' ')); Display.kMUDMessage = Display.Command; if (commandWords.Count == 1) { // if the user just typed "#alias", show the list of aliases // ShowAliases (); } else if (commandWords.Count == 2) { // if the user typed "#alias somealiasname" and nothing else, show the definition of that alias. if (Aliases.ContainsKey(commandWords [1])) { Display.AddOutputLine("\t" + commandWords [1] + ": " + Aliases [commandWords [1]]); Display.kMUDMessage = "To see a list of defined aliases, type #alias. To delete an alias, type #unalias alias."; } } else { // user tried to define an alias string aliasText = ""; for (int i = 2; i < commandWords.Count; i++) { aliasText += commandWords[i] + " "; } AddAlias(commandWords[1], aliasText); } }
private static void ShowAliases() { // show the aliases that have been made if (Aliases.Count == 0) { Display.kMUDMessage = "You have no aliases defined right now."; } else { Display.AddOutputLine("Currently defined aliases:"); foreach (string alias in Aliases.Keys) { Display.AddOutputLine("\t" + alias + ": " + Aliases [alias]); } } }
private static void ShowCommands() { Display.AddOutputLine("kMUD commands (add a # symbol in front of these):\n" + "alias\t\texit\t\tcommands\t\tcommand\t\t\n" + "unalias\t\t"); }
private static void HandleKeyPress() { if (KeyPressed.Key == ConsoleKey.RightArrow || KeyPressed.Key == ConsoleKey.LeftArrow) { Display.Highlighted = false; } else if (KeyPressed.Key != ConsoleKey.Enter && Display.Highlighted == true) { Display.Highlighted = false; Display.Command = ""; } switch (KeyPressed.Key) { case ConsoleKey.Enter: // no command = no behavior if (Display.Command.Length == 0) { try{ if (netStream.CanWrite) { byte[] toSend = ASCIIEncoding.ASCII.GetBytes("\r\n"); Client.Client.Send(toSend); } else { Display.kMUDMessage = "Can't send data to the MUD...?"; } } catch (Exception e) { Display.kMUDMessage = "Got an error...: " + e.Message; } break; } // If command starts with "#", process it if (Display.Command [0] == '#') { string firstWord = Display.Command.Trim().Split(' ') [0]; if (CommandFunctions.ContainsKey(firstWord)) { CommandFunctions [firstWord] (); } // HandleCommand (Display.Command); } // otherwise, check if it's an alias, then send it to the MUD and let them deal with it. else { try { if (netStream.CanWrite) { // check if there's an alias first string[] wordsInCommand = Display.Command.Split(' '); if (Aliases.ContainsKey(wordsInCommand[0])) { // it's an aliased command, so process it first string actualCommand = ""; for (int i = 1; i < wordsInCommand.Length; i++) { actualCommand += wordsInCommand [i] + " "; } Display.Command = ConvertAlias(wordsInCommand [0], actualCommand); } // split command by semicolons // TODO make this work in conjunction with aliases if (CommandSplitter != '\0') // signifies no splitting desired { List <string> splitCommands = new List <string> (Display.Command.Split(CommandSplitter)); foreach (string command in splitCommands) { byte[] toSend = ASCIIEncoding.ASCII.GetBytes(command + "\r\n"); Client.Client.Send(toSend); } } else { byte[] toSend = ASCIIEncoding.ASCII.GetBytes(Display.Command + "\r\n"); Client.Client.Send(toSend); } } else { Display.kMUDMessage = "Can't send data to the MUD...?"; } } catch (Exception e) { Display.kMUDMessage = "Got an error...: " + e.Message; } } // record command in command history if (Display.Echoing) { CommandHistory.Add(Display.Command); } CurrentCommand = CommandHistory.Count; // command was executed, so record it with a '#' prepended if (Display.Echoing) { Display.AddOutputLine("#" + Display.Command); // show the command Highlighted, so it's clear you can hit enter and do the same command again, // but if you type anything else or hit delete it will overwrite it. Display.Highlighted = true; // update the display to show this. repeatCommand = true; } else { Display.Highlighted = false; repeatCommand = false; Display.Command = ""; } Display.Update(); break; case ConsoleKey.Backspace: // ALT+Backspace deletes a whole word, not just a character Display.BackspaceDelete(); Display.Update(); break; case ConsoleKey.Escape: // somehow undo or cancel something, maybe? Display.Highlighted = false; Display.Command = ""; Display.Update(); break; case ConsoleKey.UpArrow: // if there is no more command history, do nothing if (CurrentCommand <= 0) { break; } // If user started typing a command, save it. If not, save a blank space (if there wasn't one) // so the user can return to a blank slate by pressing down enough. if (CurrentCommand == CommandHistory.Count && CommandHistory [CommandHistory.Count - 1] != Display.Command) { CommandHistory.Add(Display.Command); } // scroll back one command in the history, if possible CurrentCommand = Math.Max(CurrentCommand - 1, 0); Display.Command = CommandHistory [CurrentCommand]; Display.Highlighted = true; Display.Update(); break; case ConsoleKey.DownArrow: // scroll forward one command in the history, if possible. Don't overwrite history. if (CurrentCommand >= CommandHistory.Count || CommandHistory.Count == 0) { break; } CurrentCommand = Math.Min(CurrentCommand + 1, CommandHistory.Count - 1); Display.Command = CommandHistory [CurrentCommand]; Display.Highlighted = true; Display.Update(); break; case ConsoleKey.RightArrow: // move the cursor to the right Display.MoveCursorRight(); Display.Update(); break; case ConsoleKey.LeftArrow: // move cursor to the left Display.MoveCursorLeft(); Display.Update(); break; default: // Display.Command += KeyPressed.KeyChar; Display.AddToCommand(KeyPressed.KeyChar); Display.Update(); break; } }