/// <summary> /// Create a new CMenu. /// <para> /// The menu will initially contain the following commands: /// <list type="bullet"> /// <item>help</item> /// <item>quit</item> /// </list> /// </para> /// </summary> public CommandMenu(ICommandLayer io, IAutoCompleteSource autoCompleteSource, PromptDelegate prompt, string[] selector = null) : base(selector) { _IO = io; _AutoCompleteSource = autoCompleteSource; if (selector == null) { Add(new CommandMenuQuit(this)); Add(new CommandMenuHelp(this)); } PromptCharacter = "> "; Prompt = prompt == null ? OnPrompt : prompt; }
/// <summary> /// Create a new CMenu. /// <para> /// The menu will initially contain the following commands: /// <list type="bullet"> /// <item>help</item> /// <item>quit</item> /// </list> /// </para> /// </summary> public CommandMenu(CommandLayer io, IAutoCompleteSource autoCompleteSource, PromptDelegate prompt, string[] selector = null) : base(selector) { _IO = io; _AutoCompleteSource = autoCompleteSource; if (selector == null) { Add(new CommandMenuQuit(this)); Add(new CommandMenuHelp(this)); } PromptCharacter = "> "; Prompt = prompt == null ? OnPrompt : prompt; }
/// <summary> /// Returns the next available line of input. /// </summary> /// <param name="prompt">Prompt</param> /// <param name="autoComplete">AutoComplete source</param> /// <param name="isPassword">True for hide the input</param> string ReadLine(PromptDelegate prompt, IAutoCompleteSource autoComplete, bool isPassword) { int index = 0; _IO.SetCursorMode(_InsertMode ? ConsoleCursor.ECursorMode.Visible : ConsoleCursor.ECursorMode.Small); for (;;) { string input; if (_ManualInput.Count >= 1) { input = _ManualInput[0]; _ManualInput.RemoveAt(0); } else { input = null; } if (input == null) { prompt?.Invoke(this); ConsoleKeyInfo myKey; do { myKey = ReadKey(true); switch (myKey.Key) { case ConsoleKey.Tab: { if (autoComplete == null || isPassword) { break; } // Check in list string command; string word; string[] args; GetCommand(input, out word, out command, out args); IEnumerable <string> source; if (string.IsNullOrEmpty(command)) { source = autoComplete.GetCommand(); } else { source = autoComplete.GetArgument(command, args); } if (source == null) { break; } List <string> ls = new List <string>(); foreach (string s in source) { if (s != null && word != s && s.StartsWith(word, autoComplete.ComparisonMethod)) { if (!ls.Contains(s)) { ls.Add(s); } } } ConsoleCursor point = _IO.GetCursorPosition(); // Ver que hacer según el numero de encuentros string toWrite = ""; switch (ls.Count) { // Add space case 0: { toWrite = " "; break; } case 1: { // Add input toWrite = ls[0]; toWrite = toWrite.Substring(word.Length) + " "; break; } default: { // Autocompletar el contenido conjunto string fInput = word; string l = ls[0]; while (l.Length > fInput.Length) { string lw = l.Substring(0, fInput.Length + 1); bool enTodos = true; foreach (string l2 in ls) { if (l2 == l) { continue; } if (!l2.StartsWith(lw, autoComplete.ComparisonMethod)) { enTodos = false; break; } } if (!enTodos) { break; } fInput = lw; } if (fInput != word) { // Relleno fInput = fInput.Remove(0, word.Length); word += fInput; toWrite = fInput; } break; } } if (input == null) { break; } // Go to end of line int mas = input.Length - index; input = input + toWrite; index = input.Length; point.MoveRight(mas); point.Flush(_IO); if (ls.Count > 1) { // Auto complete WriteLine(""); if (ls.Count > 50) { // Check show results WriteLine(Lang.Get("Show_All_Results", ls.Count.ToString())); string s1 = InternalReadLine().ToUpperInvariant(); // Top signal? if (s1 == "T" || s1 == "TOP") { ls.RemoveRange(50, ls.Count - 50); } else { // No signal? if (s1 != "Y" && s1 != "YES") { index = 0; input = ""; if (prompt != null) { prompt(this); } break; } } } ls.Sort(); foreach (string s in ls) { SetBackgroundColor(ConsoleColor.Gray); SetForeColor(ConsoleColor.Black); Write(s.Substring(0, word.Length)); SetBackgroundColor(ConsoleColor.Black); SetForeColor(ConsoleColor.Gray); WriteLine(s.Substring(word.Length)); } if (prompt != null) { prompt(this); } Write(input); } else { Write(toWrite); } break; } #region NOT USED case ConsoleKey.Clear: case ConsoleKey.Help: case ConsoleKey.F1: case ConsoleKey.F2: case ConsoleKey.F3: case ConsoleKey.F4: case ConsoleKey.F5: case ConsoleKey.F6: case ConsoleKey.F7: case ConsoleKey.F8: case ConsoleKey.F9: case ConsoleKey.F10: case ConsoleKey.F11: case ConsoleKey.F12: case ConsoleKey.F13: case ConsoleKey.F14: case ConsoleKey.F15: case ConsoleKey.F16: case ConsoleKey.F17: case ConsoleKey.F18: case ConsoleKey.F19: case ConsoleKey.F20: case ConsoleKey.F21: case ConsoleKey.F22: case ConsoleKey.F23: case ConsoleKey.F24: case ConsoleKey.Applications: case ConsoleKey.Attention: case ConsoleKey.BrowserBack: case ConsoleKey.BrowserFavorites: case ConsoleKey.BrowserForward: case ConsoleKey.BrowserHome: case ConsoleKey.BrowserRefresh: case ConsoleKey.BrowserSearch: case ConsoleKey.BrowserStop: case ConsoleKey.LaunchApp1: case ConsoleKey.LaunchApp2: case ConsoleKey.LaunchMail: case ConsoleKey.LaunchMediaSelect: case ConsoleKey.MediaNext: case ConsoleKey.MediaPlay: case ConsoleKey.MediaPrevious: case ConsoleKey.MediaStop: case ConsoleKey.Packet: case ConsoleKey.Pause: case ConsoleKey.Play: case ConsoleKey.Print: case ConsoleKey.PrintScreen: case ConsoleKey.Process: case ConsoleKey.RightWindows: case ConsoleKey.Select: case ConsoleKey.Sleep: case ConsoleKey.VolumeDown: case ConsoleKey.VolumeMute: case ConsoleKey.VolumeUp: case ConsoleKey.Zoom: break; #endregion case ConsoleKey.Enter: { WriteLine(""); break; } case ConsoleKey.Insert: { // Tongle insert mode _InsertMode = !_InsertMode; _IO.SetCursorMode(_InsertMode ? ConsoleCursor.ECursorMode.Visible : ConsoleCursor.ECursorMode.Small); break; } case ConsoleKey.Home: { if (index <= 0) { break; } ConsoleCursor point = _IO.GetCursorPosition(); point.MoveLeft(index); point.Flush(_IO); index = 0; break; } case ConsoleKey.End: { if (input == null || index >= input.Length) { break; } ConsoleCursor point = _IO.GetCursorPosition(); point.MoveRight(input.Length - index); point.Flush(_IO); index = input.Length; break; } case ConsoleKey.Delete: { if (input == null || index >= input.Length) { break; } if (index + 1 == input.Length) { input = input.Substring(0, input.Length - 1); Write(" "); ConsoleCursor point = _IO.GetCursorPosition(); point.MoveLeft(1); point.Flush(_IO); } else { input = input.Remove(index, 1); ConsoleCursor point = _IO.GetCursorPosition(); Write(input.Substring(index) + " "); point.Flush(_IO); } break; } case ConsoleKey.Backspace: { if (input == null || index <= 0) { break; } ConsoleCursor point = _IO.GetCursorPosition(); if (index == input.Length) { input = input.Substring(0, input.Length - 1); point.MoveLeft(1); point.Flush(_IO); Write(" "); point.Flush(_IO); } else { input = input.Remove(index - 1, 1); point.MoveLeft(1); Write("".PadLeft(input.Length - index + 2)); point.Flush(_IO); Write(input.Substring(index - 1)); point.Flush(_IO); } index--; break; } case ConsoleKey.LeftArrow: { if (input == null || index <= 0) { break; } index--; ConsoleCursor point = _IO.GetCursorPosition(); point.MoveLeft(1); point.Flush(_IO); break; } case ConsoleKey.RightArrow: { if (input == null || index >= input.Length) { break; } index++; ConsoleCursor point = _IO.GetCursorPosition(); point.MoveRight(1); point.Flush(_IO); break; } case ConsoleKey.PageUp: case ConsoleKey.UpArrow: case ConsoleKey.DownArrow: case ConsoleKey.PageDown: { if (_History.Count <= 0) { break; } string next = GetHistory(myKey.Key == ConsoleKey.PageUp || myKey.Key == ConsoleKey.UpArrow); ConsoleCursor point = _IO.GetCursorPosition(); if (index > 0) { point.MoveLeft(index); point.Flush(_IO); } Write(next); if (next.Length < index) { // Undo write move point.MoveRight(next.Length); int append = index - next.Length; Write("".PadLeft(append, ' ')); // Restore save point point.Flush(_IO); } input = next; index = next.Length; break; } case ConsoleKey.NoName: { // Not console Thread.Sleep(1000); break; } default: { if (myKey.KeyChar == '\0') { break; } if (input == null) { input = ""; } if (input.Length == index) { input = input + myKey.KeyChar; Write(isPassword ? "*" : myKey.KeyChar.ToString()); } else { ConsoleCursor point = _IO.GetCursorPosition(); if (_InsertMode) { int antes = input.Length; input = input.Insert(index, myKey.KeyChar.ToString()); Write(isPassword ? "".PadLeft(antes, '*') : input.Substring(index)); point.Flush(_IO); point.MoveRight(1); point.Flush(_IO); } else { char[] array = input.ToCharArray(); array[index] = myKey.KeyChar; input = new string(array); Write(isPassword ? "*" : myKey.KeyChar.ToString()); } } index++; break; } } continue; }while (myKey.Key != ConsoleKey.Enter); SetForeColor(ConsoleColor.Gray); } SetForeColor(ConsoleColor.Gray); _IO.SetCursorMode(ConsoleCursor.ECursorMode.Hidden); if (!string.IsNullOrWhiteSpace(input)) { WriteLog(input); return(input); } } }
public string ReadLine(PromptDelegate prompt, IAutoCompleteSource autoComplete) { return(ReadLine(prompt, autoComplete, false)); }
/// <summary> /// Returns the next available line of input. /// </summary> /// <param name="prompt"> /// String to prompt, or null. /// </param> string ReadLine(PromptDelegate prompt, IAutoCompleteSource autoComplete, bool isPassword) { Console.CursorVisible = true; for (; ;) { string input; if (_Frames.Count >= 1) { input = _Frames[0]; _Frames.RemoveAt(0); } else { input = null; } if (input == null) { if (prompt != null) { prompt(this); } if (autoComplete == null) { input = InternalReadLine(); } else { ConsoleKeyInfo myKey; do { myKey = ReadKey(true); switch (myKey.Key) { case ConsoleKey.Tab: { // Check in list string command; string word; string[] args; GetCommand(input, out word, out command, out args); IEnumerable <string> source; if (string.IsNullOrEmpty(command)) { source = autoComplete.GetCommand(); } else { source = autoComplete.GetArgument(command, args); } if (source == null) { break; } List <string> ls = new List <string>(); foreach (string s in source) { if (word != s && s.StartsWith(word, autoComplete.ComparisonMethod)) { if (!ls.Contains(s)) { ls.Add(s); } } } // Ver que hacer según el numero de encuentros switch (ls.Count) { case 0: { // Add space Write(" "); input = input + ' '; break; } case 1: { // Add input string toWrite = ls[0]; toWrite = toWrite.Substring(word.Length) + " "; Write(toWrite); input = input + toWrite; break; } default: { // Autocompletar el contenido conjunto string fInput = word; string l = ls[0]; while (l.Length > fInput.Length) { string lw = l.Substring(0, fInput.Length + 1); bool enTodos = true; foreach (string l2 in ls) { if (l2 == l) { continue; } if (!l2.StartsWith(lw, autoComplete.ComparisonMethod)) { enTodos = false; break; } } if (!enTodos) { break; } fInput = lw; } if (fInput != word) { // Relleno fInput = fInput.Remove(0, word.Length); word += fInput; input += fInput; Write(fInput); } WriteLine(""); if (ls.Count > 50) { // Check show results WriteLine("Show " + ls.Count.ToString() + " results? [Yes/No/Top]"); string s1 = InternalReadLine().ToUpperInvariant(); // Top signal? if (s1 == "T" || s1 == "TOP") { ls.RemoveRange(50, ls.Count - 50); } else { // No signal? if (s1 != "Y" && s1 != "YES") { if (prompt != null) { prompt(this); } Write(input); break; } } } ls.Sort(); foreach (string s in ls) { SetBackgroundColor(ConsoleColor.Gray); SetForeColor(ConsoleColor.Black); Write(s.Substring(0, word.Length)); SetBackgroundColor(ConsoleColor.Black); SetForeColor(ConsoleColor.Gray); WriteLine(s.Substring(word.Length)); } if (prompt != null) { prompt(this); } Write(input); break; } } break; } #region NOT USED case ConsoleKey.F1: case ConsoleKey.F2: case ConsoleKey.F3: case ConsoleKey.F4: case ConsoleKey.F5: case ConsoleKey.F6: case ConsoleKey.F7: case ConsoleKey.F8: case ConsoleKey.F9: case ConsoleKey.F10: case ConsoleKey.F11: case ConsoleKey.F12: case ConsoleKey.F13: case ConsoleKey.F14: case ConsoleKey.F15: case ConsoleKey.F16: case ConsoleKey.F17: case ConsoleKey.F18: case ConsoleKey.F19: case ConsoleKey.F20: case ConsoleKey.F21: case ConsoleKey.F22: case ConsoleKey.F23: case ConsoleKey.F24: case ConsoleKey.Applications: case ConsoleKey.Attention: case ConsoleKey.BrowserBack: case ConsoleKey.BrowserFavorites: case ConsoleKey.BrowserForward: case ConsoleKey.BrowserHome: case ConsoleKey.BrowserRefresh: case ConsoleKey.BrowserSearch: case ConsoleKey.BrowserStop: case ConsoleKey.Clear: case ConsoleKey.Help: case ConsoleKey.Home: case ConsoleKey.Insert: case ConsoleKey.LaunchApp1: case ConsoleKey.LaunchApp2: case ConsoleKey.LaunchMail: case ConsoleKey.LaunchMediaSelect: case ConsoleKey.MediaNext: case ConsoleKey.MediaPlay: case ConsoleKey.MediaPrevious: case ConsoleKey.MediaStop: case ConsoleKey.NoName: case ConsoleKey.Packet: case ConsoleKey.Pause: case ConsoleKey.Play: case ConsoleKey.Print: case ConsoleKey.PrintScreen: case ConsoleKey.Process: case ConsoleKey.RightWindows: case ConsoleKey.Select: case ConsoleKey.Sleep: case ConsoleKey.VolumeDown: case ConsoleKey.VolumeMute: case ConsoleKey.VolumeUp: case ConsoleKey.Zoom: break; #endregion case ConsoleKey.Enter: { WriteLine(""); break; } case ConsoleKey.Backspace: { if (input.Length > 0) { input = input.Substring(0, input.Length - 1); Write("\b \b"); } break; } #region ToDO case ConsoleKey.LeftArrow: { break; } case ConsoleKey.RightArrow: { break; } case ConsoleKey.PageUp: case ConsoleKey.UpArrow: { break; } case ConsoleKey.DownArrow: case ConsoleKey.PageDown: { break; } #endregion default: { if (myKey.KeyChar == '\0') { break; } Write(myKey.KeyChar.ToString()); input = input + myKey.KeyChar; break; } } continue; }while (myKey.Key != ConsoleKey.Enter); } SetForeColor(ConsoleColor.Gray); } SetForeColor(ConsoleColor.Gray); Console.CursorVisible = false; if (!string.IsNullOrWhiteSpace(input)) { return(input); } } }
/// <summary> /// Returns the next available line of input. /// </summary> /// <param name="prompt"> /// String to prompt, or null. /// </param> string ReadLine(PromptDelegate prompt, IAutoCompleteSource autoComplete, bool isPassword) { Console.CursorVisible = true; for (; ; ) { string input; if (_Frames.Count >= 1) { input = _Frames[0]; _Frames.RemoveAt(0); } else input = null; if (input == null) { if (prompt != null) prompt(this); if (autoComplete == null) input = InternalReadLine(); else { ConsoleKeyInfo myKey; do { myKey = ReadKey(true); switch (myKey.Key) { case ConsoleKey.Tab: { // Check in list string command; string word; string[] args; GetCommand(input, out word, out command, out args); IEnumerable<string> source; if (string.IsNullOrEmpty(command)) source = autoComplete.GetCommand(); else source = autoComplete.GetArgument(command, args); if (source == null) break; List<string> ls = new List<string>(); foreach (string s in source) if (word != s && s.StartsWith(word, autoComplete.ComparisonMethod)) { if (!ls.Contains(s)) ls.Add(s); } // Ver que hacer según el numero de encuentros switch (ls.Count) { case 0: { // Add space Write(" "); input = input + ' '; break; } case 1: { // Add input string toWrite = ls[0]; toWrite = toWrite.Substring(word.Length) + " "; Write(toWrite); input = input + toWrite; break; } default: { // Autocompletar el contenido conjunto string fInput = word; string l = ls[0]; while (l.Length > fInput.Length) { string lw = l.Substring(0, fInput.Length + 1); bool enTodos = true; foreach (string l2 in ls) { if (l2 == l) continue; if (!l2.StartsWith(lw, autoComplete.ComparisonMethod)) { enTodos = false; break; } } if (!enTodos) break; fInput = lw; } if (fInput != word) { // Relleno fInput = fInput.Remove(0, word.Length); word += fInput; input += fInput; Write(fInput); } WriteLine(""); if (ls.Count > 50) { // Check show results WriteLine("Show " + ls.Count.ToString() + " results? [Yes/No/Top]"); string s1 = InternalReadLine().ToUpperInvariant(); // Top signal? if (s1 == "T" || s1 == "TOP") ls.RemoveRange(50, ls.Count - 50); else { // No signal? if (s1 != "Y" && s1 != "YES") { if (prompt != null) prompt(this); Write(input); break; } } } ls.Sort(); foreach (string s in ls) { SetBackgroundColor(ConsoleColor.Gray); SetForeColor(ConsoleColor.Black); Write(s.Substring(0, word.Length)); SetBackgroundColor(ConsoleColor.Black); SetForeColor(ConsoleColor.Gray); WriteLine(s.Substring(word.Length)); } if (prompt != null) prompt(this); Write(input); break; } } break; } #region NOT USED case ConsoleKey.F1: case ConsoleKey.F2: case ConsoleKey.F3: case ConsoleKey.F4: case ConsoleKey.F5: case ConsoleKey.F6: case ConsoleKey.F7: case ConsoleKey.F8: case ConsoleKey.F9: case ConsoleKey.F10: case ConsoleKey.F11: case ConsoleKey.F12: case ConsoleKey.F13: case ConsoleKey.F14: case ConsoleKey.F15: case ConsoleKey.F16: case ConsoleKey.F17: case ConsoleKey.F18: case ConsoleKey.F19: case ConsoleKey.F20: case ConsoleKey.F21: case ConsoleKey.F22: case ConsoleKey.F23: case ConsoleKey.F24: case ConsoleKey.Applications: case ConsoleKey.Attention: case ConsoleKey.BrowserBack: case ConsoleKey.BrowserFavorites: case ConsoleKey.BrowserForward: case ConsoleKey.BrowserHome: case ConsoleKey.BrowserRefresh: case ConsoleKey.BrowserSearch: case ConsoleKey.BrowserStop: case ConsoleKey.Clear: case ConsoleKey.Help: case ConsoleKey.Home: case ConsoleKey.Insert: case ConsoleKey.LaunchApp1: case ConsoleKey.LaunchApp2: case ConsoleKey.LaunchMail: case ConsoleKey.LaunchMediaSelect: case ConsoleKey.MediaNext: case ConsoleKey.MediaPlay: case ConsoleKey.MediaPrevious: case ConsoleKey.MediaStop: case ConsoleKey.NoName: case ConsoleKey.Packet: case ConsoleKey.Pause: case ConsoleKey.Play: case ConsoleKey.Print: case ConsoleKey.PrintScreen: case ConsoleKey.Process: case ConsoleKey.RightWindows: case ConsoleKey.Select: case ConsoleKey.Sleep: case ConsoleKey.VolumeDown: case ConsoleKey.VolumeMute: case ConsoleKey.VolumeUp: case ConsoleKey.Zoom: break; #endregion case ConsoleKey.Enter: { WriteLine(""); break; } case ConsoleKey.Backspace: { if (input.Length > 0) { input = input.Substring(0, input.Length - 1); Write("\b \b"); } break; } #region ToDO case ConsoleKey.LeftArrow: { break; } case ConsoleKey.RightArrow: { break; } case ConsoleKey.PageUp: case ConsoleKey.UpArrow: { break; } case ConsoleKey.DownArrow: case ConsoleKey.PageDown: { break; } #endregion default: { if (myKey.KeyChar == '\0') break; Write(myKey.KeyChar.ToString()); input = input + myKey.KeyChar; break; } } continue; } while (myKey.Key != ConsoleKey.Enter); } SetForeColor(ConsoleColor.Gray); } SetForeColor(ConsoleColor.Gray); Console.CursorVisible = false; if (!string.IsNullOrWhiteSpace(input)) return input; } }
public string ReadLine(PromptDelegate prompt, IAutoCompleteSource autoComplete) { return ReadLine(prompt, autoComplete, false); }