Skip to content

Commit

Permalink
Merge pull request #12 from Guad/add_backwards_autocomplete
Browse files Browse the repository at this point in the history
Add backwards AutoComplete
  • Loading branch information
tonerdo authored Feb 19, 2017
2 parents eac1a96 + b730fe6 commit 7a3a291
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ It is cross platform and runs anywhere .NET is supported, targeting `netstandard
| `Ctrl`+`F` / `` | Forward one character |
| `Ctrl`+`H` / `Backspace` | Delete previous character |
| `Tab` | Command line completion |
| `Shift`+`Tab` | Backwards command line completion |
| `Ctrl`+`J` / `Enter` | Line feed |
| `Ctrl`+`K` | Cut text to the end of line |
| `Ctrl`+`L` | Clear line |
Expand Down
40 changes: 36 additions & 4 deletions src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void MoveCursorHome()

private string BuildKeyInput()
{
return _keyInfo.Modifiers != ConsoleModifiers.Control ?
return (_keyInfo.Modifiers != ConsoleModifiers.Control && _keyInfo.Modifiers != ConsoleModifiers.Shift) ?
_keyInfo.Key.ToString() : _keyInfo.Modifiers.ToString() + _keyInfo.Key.ToString();
}

Expand Down Expand Up @@ -133,16 +133,40 @@ private void Backspace()
}
}

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

_completionsIndex = 0;

WriteString(_completions[_completionsIndex]);
}

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

_completionsIndex++;

if (_completionsIndex == _completions.Length)
_completionsIndex = 0;

WriteString(_completions[_completionsIndex]);
}

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

_completionsIndex--;

if (_completionsIndex == -1)
_completionsIndex = _completions.Length - 1;

WriteString(_completions[_completionsIndex]);
}

private void PrevHistory()
Expand Down Expand Up @@ -226,7 +250,7 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
{
if (IsInAutoCompleteMode())
{
AutoComplete();
NextAutoComplete();
}
else
{
Expand All @@ -243,7 +267,15 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
if (_completions == null || _completions.Length == 0)
return;

AutoComplete();
StartAutoComplete();
}
};

_keyActions["ShiftTab"] = () =>
{
if (IsInAutoCompleteMode())
{
PreviousAutoComplete();
}
};
}
Expand Down
33 changes: 33 additions & 0 deletions test/ReadLine.Tests/KeyHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,38 @@ public void TestTab()
Assert.Equal($"Hi {_completions[i]}", _keyHandler.Text);
}
}

[Fact]
public void TestBackwardsTab()
{
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false);
_keyHandler.Handle(_keyInfo);

// Nothing happens when no auto complete handler is set
Assert.Equal("Hello", _keyHandler.Text);

_keyHandler = new KeyHandler(new Console2(), _history, (t, s) => _completions);

_keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false);
_keyHandler.Handle(_keyInfo);

_keyInfo = new ConsoleKeyInfo('i', ConsoleKey.I, false, false, false);
_keyHandler.Handle(_keyInfo);

_keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false);
_keyHandler.Handle(_keyInfo);

// Bring up the first Autocomplete
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false);
_keyHandler.Handle(_keyInfo);

for (int i = _completions.Length - 1; i >= 0; i--)
{
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, true, false, false);
_keyHandler.Handle(_keyInfo);

Assert.Equal($"Hi {_completions[i]}", _keyHandler.Text);
}
}
}
}

0 comments on commit 7a3a291

Please # to comment.