/* * ------- 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); } } }