Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/ReadLine.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace ConsoleApplication
{
Expand All @@ -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}]");
}
}
}
89 changes: 88 additions & 1 deletion src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -269,7 +349,14 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
if (_completions == null)
return;

StartAutoComplete();
if (this.RollingAutoComplete)
{
StartAutoComplete();
}
else
{
AllAutoComplete();
}
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/ReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ public static class ReadLine
static ReadLine()
{
_history = new List<string>();
RollingComplete = true;
}

public static void AddHistory(params string[] text) => _history.AddRange(text);
public static List<string> GetHistory() => _history;
public static void ClearHistory() => _history = new List<string>();
public static Func<string, int, string[]> 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)
Expand Down