diff --git a/src/ReadLine.Demo/Program.cs b/src/ReadLine.Demo/Program.cs index 8678b59..ca13c43 100755 --- a/src/ReadLine.Demo/Program.cs +++ b/src/ReadLine.Demo/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace ConsoleApplication { @@ -16,13 +17,17 @@ public static void Main(string[] args) ReadLine.AutoCompletionHandler = (t, s) => { if (t.StartsWith("git ")) - return new string[] { "init", "clone", "pull", "push" }; + return new string[] { "init", "clone", "pull", "push", "cherry-pick", "merge", "rebase", "commit", "status" }; else - return null; + return new string[] { "ls", "la", "li", "longcommandwhichwillbeononeline", "move", "moll", "moun" }.Where(x => x.StartsWith(t)).ToArray(); }; + //string input = ReadLine.Read("(prompt)> "); + //Console.Write(input); + + ReadLine.RollingComplete = false; string input = ReadLine.Read("(prompt)> "); - Console.Write(input); + Console.Write($"input was : [{input}]"); } } } diff --git a/src/ReadLine/KeyHandler.cs b/src/ReadLine/KeyHandler.cs index 539f810..b39a3f9 100644 --- a/src/ReadLine/KeyHandler.cs +++ b/src/ReadLine/KeyHandler.cs @@ -8,6 +8,9 @@ namespace Internal.ReadLine { internal class KeyHandler { + private const int AutoCompleteColumns = 5; + private const int AutoCompleteColumnLength = 15; + private int _cursorPos; private int _cursorLimit; private StringBuilder _text; @@ -29,6 +32,10 @@ internal class KeyHandler private bool IsEndOfBuffer() => Console2.CursorLeft == Console2.BufferWidth - 1; private bool IsInAutoCompleteMode() => _completions != null; + public bool RollingAutoComplete { get; set; } = true; + + public string Prompt { get; set; } + private void MoveCursorLeft() { if (IsStartOfLine()) @@ -169,6 +176,79 @@ private void PreviousAutoComplete() WriteString(_completions[_completionsIndex]); } + private void AllAutoComplete() + { + if (_completions.Length == 1) + { + while (_cursorPos > _completionStart) + Backspace(); + + WriteString(_completions[0]); + return; + } + + // max length + + int maxLen = 0; + for (int i = 0; i < _completions.Length; i++) + { + if (maxLen < _completions[i].Length) + { + maxLen = _completions[i].Length; + } + } + + Console.WriteLine(); + + // display in columns + if (maxLen < AutoCompleteColumnLength) + { + StringBuilder line = new StringBuilder(); + for (int i = 0; i < _completions.Length; i++) + { + line.Append(_completions[i]); + + int fill = AutoCompleteColumnLength - (line.Length % AutoCompleteColumnLength); + line.Append(' ', fill); + + if (line.Length >= AutoCompleteColumns * AutoCompleteColumnLength) + { + Console.WriteLine(line.ToString()); + line.Clear(); + } + } + + if (line.Length > 0) + { + Console.WriteLine(line.ToString()); + line.Clear(); + } + } + // display in lines + else + { + for (int i = 0; i < _completions.Length; i++) + { + Console.WriteLine(_completions[i]); + } + } + + _completions = null; + string prevText = _text.ToString(); + + // rewrite prompt and reset + + ClearLine(); + Console.WriteLine(); + Console.Write(this.Prompt); + _cursorPos = 0; + _cursorLimit = 0; + + // write previous string + + WriteString(prevText); + } + private void PrevHistory() { if (_historyIndex > 0) @@ -269,7 +349,14 @@ public KeyHandler(IConsole console, List history, Func(); + RollingComplete = true; } public static void AddHistory(params string[] text) => _history.AddRange(text); @@ -20,12 +21,15 @@ static ReadLine() public static void ClearHistory() => _history = new List(); public static Func AutoCompletionHandler { private get; set; } public static bool PasswordMode { private get; set; } + public static bool RollingComplete { get; set; } public static string Read(string prompt = "", string defaultInput = "") { Console.Write(prompt); _keyHandler = new KeyHandler(new Console2() { PasswordMode = PasswordMode }, _history, AutoCompletionHandler); + _keyHandler.RollingAutoComplete = RollingComplete; + _keyHandler.Prompt = prompt; ConsoleKeyInfo keyInfo = Console.ReadKey(true); while (keyInfo.Key != ConsoleKey.Enter)