Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoComplete - Faster tab completion #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 28 additions & 23 deletions src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ internal class KeyHandler

private void MoveCursorLeft()
{
if (IsStartOfLine())
return;
MoveCursorLeft(1);
}

private void MoveCursorLeft(int count)
{
if (count > _cursorPos)
count = _cursorPos;

if (IsStartOfBuffer())
if (count > Console2.CursorLeft)
Console2.SetCursorPosition(Console2.BufferWidth - 1, Console2.CursorTop - 1);
else
Console2.SetCursorPosition(Console2.CursorLeft - 1, Console2.CursorTop);
Console2.SetCursorPosition(Console2.CursorLeft - count, Console2.CursorTop);

_cursorPos--;
_cursorPos -= count;
}

private void MoveCursorHome()
Expand Down Expand Up @@ -76,8 +81,7 @@ private void MoveCursorEnd()
private void ClearLine()
{
MoveCursorEnd();
while (!IsStartOfLine())
Backspace();
Backspace(_cursorPos);
}

private void WriteNewString(string str)
Expand Down Expand Up @@ -119,18 +123,24 @@ private void WriteChar(char c)

private void Backspace()
{
if (IsStartOfLine())
return;
Backspace(1);
}

MoveCursorLeft();
private void Backspace(int count)
{
if (count > _cursorPos)
count = _cursorPos;

MoveCursorLeft(count);
int index = _cursorPos;
_text.Remove(index, 1);
_text.Remove(index, count);
string replacement = _text.ToString().Substring(index);
int left = Console2.CursorLeft;
int top = Console2.CursorTop;
Console2.Write(string.Format("{0} ", replacement));
string spaces = new string(' ', count);
Console2.Write(string.Format("{0}{1}", replacement, spaces));
Console2.SetCursorPosition(left, top);
_cursorLimit--;
_cursorLimit -= count;
}

private void Delete()
Expand Down Expand Up @@ -177,8 +187,7 @@ private void TransposeChars()

private void StartAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();
Backspace(_cursorPos - _completionStart);

_completionsIndex = 0;

Expand All @@ -187,8 +196,7 @@ private void StartAutoComplete()

private void NextAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();
Backspace(_cursorPos - _completionStart);

_completionsIndex++;

Expand All @@ -200,8 +208,7 @@ private void NextAutoComplete()

private void PreviousAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();
Backspace(_cursorPos - _completionStart);

_completionsIndex--;

Expand Down Expand Up @@ -275,15 +282,13 @@ public KeyHandler(IConsole console, List<string> history, IAutoCompleteHandler a
_keyActions["ControlN"] = NextHistory;
_keyActions["ControlU"] = () =>
{
while (!IsStartOfLine())
Backspace();
Backspace(_cursorPos);
};
_keyActions["ControlK"] = () =>
{
int pos = _cursorPos;
MoveCursorEnd();
while (_cursorPos > pos)
Backspace();
Backspace(_cursorPos - pos);
};
_keyActions["ControlW"] = () =>
{
Expand Down