/* * decide what to do with contents of m_userText */ private void RunState_ParseUserCommand() { // detect type of command CommandsHelper.Command commandType = m_commandsHelper.Parse(m_userText); // try to process switch (commandType) { case CommandsHelper.Command.Quit: m_lastEvent = ClientEvent.QuitCommandEntered; break; case CommandsHelper.Command.Help: m_commandsHelper.ShowHelp(); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; case CommandsHelper.Command.About: Console.WriteLine("(version 0.1 - Sep 2018) Stand along console client - to connect to Unity Server "); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; case CommandsHelper.Command.History: CommandHistory.PrintAll(); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; case CommandsHelper.Command.IncompleteConnect: Console.WriteLine("usage: connect <ip> <port>"); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; case CommandsHelper.Command.ConnectWithArguments: m_ipAddress = m_commandsHelper.LastConnectCommandParseResult.IpAddress; m_port = m_commandsHelper.LastConnectCommandParseResult.Port; m_lastEvent = ClientEvent.ReadyToTryConnection; break; case CommandsHelper.Command.NotImplemented: Console.WriteLine("sorry - that command isn't implemented yet"); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; case CommandsHelper.Command.NotRecognised: // connected, and not a simple client command, so send text on to the Server if (m_connected) { m_lastEvent = ClientEvent.MessageReadyToSendToServer; } else { // not connected - so tell user to connect Console.Write("you need to connect to a game server..."); Console.WriteLine("// connect <ip> <port>"); m_lastEvent = ClientEvent.DisplayedSimpleMessage; } break; default: Console.Write("(sorry - command not recognised)"); m_lastEvent = ClientEvent.DisplayedSimpleMessage; break; } // now EXECUTE command based on event message m_state = State.PROCESS_USER_COMMAND; }
/* * ------- console reading stuff -------- */ public string ReadConsole() { /* * if had an event that displayed text on the console, we need to re-display prompt and any typing */ if (m_lastEvent == ClientEvent.MessageReceivedFromServer) { m_lastEvent = ClientEvent.Consumed; ClearConsole(m_incompleteTyping); } // loop until Enter key is pressed ConsoleKeyInfo KeyInfoPressed = Console.ReadKey(); switch (KeyInfoPressed.Key) { case ConsoleKey.UpArrow: m_incompleteTyping = CommandHistory.GetAtCursor(); CommandHistory.Back(); ClearConsole(m_incompleteTyping); break; case ConsoleKey.DownArrow: CommandHistory.Forward(); m_incompleteTyping = CommandHistory.GetAtCursor(); ClearConsole(m_incompleteTyping); break; case ConsoleKey.Backspace: Boolean someTextToDelete = (m_incompleteTyping.Length > 0) && (Console.CursorLeft > m_prompt.Length - 1); if (someTextToDelete) { m_incompleteTyping = m_incompleteTyping.Remove(m_incompleteTyping.Length - 1, 1); ClearConsole(m_incompleteTyping); } else { Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); } break; case ConsoleKey.Delete: break; default: // just add the char to the answer building string m_incompleteTyping = m_incompleteTyping + KeyInfoPressed.KeyChar.ToString(); ClearConsole(m_incompleteTyping); break; case ConsoleKey.Enter: // exit this routine and return the Answer to process further // set command history cursor back to top of stack CommandHistory.CursorToTop(); // output current text Console.Write("\n"); // if not empty string, then add this to Command history if (m_incompleteTyping.Length > 0) { CommandHistory.Add(m_incompleteTyping); m_lastEvent = ClientEvent.UserCommandEntered; } else { m_lastEvent = ClientEvent.IgnorableUserCommandEntered; } return(m_incompleteTyping); } return(""); }
public static string ReadConsole(string prompt) { string currentText = ""; Console.Write(prompt); while (true) { // loop until Enter key is pressed ConsoleKeyInfo KeyInfoPressed = Console.ReadKey(); switch (KeyInfoPressed.Key) { case ConsoleKey.UpArrow: currentText = CommandHistory.GetAtCursor(); CommandHistory.Back(); ClearConsole(prompt, currentText); break; case ConsoleKey.DownArrow: CommandHistory.Forward(); currentText = CommandHistory.GetAtCursor(); ClearConsole(prompt, currentText); break; case ConsoleKey.Backspace: Boolean someTextToDelete = (currentText.Length > 0) && (Console.CursorLeft > prompt.Length - 1); if (someTextToDelete) { currentText = currentText.Remove(currentText.Length - 1, 1); ClearConsole(prompt, currentText); } else { Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); } break; case ConsoleKey.Delete: break; default: // just add the char to the answer building string currentText = currentText + KeyInfoPressed.KeyChar.ToString(); ClearConsole(prompt, currentText); break; case ConsoleKey.Enter: // exit this routine and return the Answer to process further // set comamnd history cursor back to top of stack CommandHistory.CursorToTop(); // output current text Console.Write("\n"); // Console.Write(prompt); // if not empty string, then add this to Command history if (currentText.Length > 0) { CommandHistory.Add(currentText); } return(currentText); } } }