/// <summary> /// Read a line from the standard input /// </summary> public static string ReadLine() { if (BasicIO) { return(Console.ReadLine()); } ConsoleKeyInfo k = new ConsoleKeyInfo(); lock (io_lock) { Console.Write('>'); reading = true; buffer = ""; buffer2 = ""; } while (k.Key != ConsoleKey.Enter) { k = Console.ReadKey(true); lock (io_lock) { if (k.Key == ConsoleKey.V && k.Modifiers == ConsoleModifiers.Control) { string clip = ReadClipboard(); foreach (char c in clip) { AddChar(c); } } else { switch (k.Key) { case ConsoleKey.Escape: ClearLineAndBuffer(); break; case ConsoleKey.Backspace: RemoveOneChar(); break; case ConsoleKey.Enter: Console.Write('\n'); break; case ConsoleKey.LeftArrow: GoLeft(); break; case ConsoleKey.RightArrow: GoRight(); break; case ConsoleKey.Home: while (buffer.Length > 0) { GoLeft(); } break; case ConsoleKey.End: while (buffer2.Length > 0) { GoRight(); } break; case ConsoleKey.Delete: if (buffer2.Length > 0) { GoRight(); RemoveOneChar(); } break; case ConsoleKey.DownArrow: if (previous.Count > 0) { ClearLineAndBuffer(); buffer = previous.First.Value; previous.AddLast(buffer); previous.RemoveFirst(); Console.Write(buffer); } break; case ConsoleKey.UpArrow: if (previous.Count > 0) { ClearLineAndBuffer(); buffer = previous.Last.Value; previous.AddFirst(buffer); previous.RemoveLast(); Console.Write(buffer); } break; case ConsoleKey.Tab: if (autocomplete_words.Count == 0 && autocomplete_engine != null && buffer.Length > 0) { foreach (string result in autocomplete_engine.AutoComplete(buffer)) { autocomplete_words.AddLast(result); } } string word_autocomplete = null; if (autocomplete_words.Count > 0) { word_autocomplete = autocomplete_words.First.Value; autocomplete_words.RemoveFirst(); autocomplete_words.AddLast(word_autocomplete); } if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != buffer) { while (buffer.Length > 0 && buffer[buffer.Length - 1] != ' ') { RemoveOneChar(); } foreach (char c in word_autocomplete) { AddChar(c); } } break; default: if (k.KeyChar != 0) { AddChar(k.KeyChar); } break; } } if (k.Key != ConsoleKey.Tab) { autocomplete_words.Clear(); } } } lock (io_lock) { reading = false; previous.AddLast(buffer + buffer2); return(buffer + buffer2); } }
/// <summary> /// Read a line from the standard input /// </summary> public static string ReadLine(bool disablespace = false) { if (basicIO) { return(System.Console.ReadLine()); } ConsoleKeyInfo k = new ConsoleKeyInfo(); /*System.Console.*/ //Write("> "); reading = true; buffer = ""; buffer2 = ""; while (k.Key != ConsoleKey.Enter) { k = System.Console.ReadKey(true); while (writing_lock) { } reading_lock = true; if (k.Key == ConsoleKey.V && k.Modifiers == ConsoleModifiers.Control) { string clip = ReadClipboard(); foreach (char c in clip) { AddChar(c); } } else { switch (k.Key) { case ConsoleKey.Escape: ClearLineAndBuffer(); break; case ConsoleKey.Backspace: RemoveOneChar(); break; case ConsoleKey.Enter: //System.Console.Write('\n'); System.Console.SetCursorPosition(0, System.Console.CursorTop); break; case ConsoleKey.LeftArrow: GoLeft(); break; case ConsoleKey.RightArrow: GoRight(); break; case ConsoleKey.Home: while (buffer.Length > 0) { GoLeft(); } break; case ConsoleKey.End: while (buffer2.Length > 0) { GoRight(); } break; case ConsoleKey.Delete: if (buffer2.Length > 0) { GoRight(); RemoveOneChar(); } break; case ConsoleKey.Oem6: break; case ConsoleKey.DownArrow: if (previous.Count > 0) { ClearLineAndBuffer(); buffer = previous.First.Value; previous.AddLast(buffer); previous.RemoveFirst(); System.Console.Write(buffer); } break; case ConsoleKey.UpArrow: if (previous.Count > 0) { ClearLineAndBuffer(); buffer = previous.Last.Value; previous.AddFirst(buffer); previous.RemoveLast(); System.Console.Write(buffer); } break; case ConsoleKey.Tab: if (autocomplete_engine != null && buffer.Length > 0) { string[] tmp = buffer.Split(' '); if (tmp.Length > 0) { string word_tocomplete = tmp[tmp.Length - 1]; string word_autocomplete = autocomplete_engine.AutoComplete(buffer); if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != word_tocomplete) { while (buffer.Length > 0 && buffer[buffer.Length - 1] != ' ') { RemoveOneChar(); } foreach (char c in word_autocomplete) { AddChar(c); } } } } break; case ConsoleKey.Spacebar: if (!disablespace) { AddChar(k.KeyChar); } break; default: if (k.KeyChar != 0) { AddChar(k.KeyChar); } break; } } reading_lock = false; } while (writing_lock) { } reading = false; previous.AddLast(buffer + buffer2); return(buffer + buffer2); }