diff --git a/artifacts/ReadLine.1.2.1.nupkg b/artifacts/ReadLine.1.2.1.nupkg index 69eac55..8e18dcd 100644 Binary files a/artifacts/ReadLine.1.2.1.nupkg and b/artifacts/ReadLine.1.2.1.nupkg differ diff --git a/src/ReadLine.Demo/Program.cs b/src/ReadLine.Demo/Program.cs index dda85d6..8961e55 100755 --- a/src/ReadLine.Demo/Program.cs +++ b/src/ReadLine.Demo/Program.cs @@ -1,30 +1,35 @@ using System; -namespace ReadLine.Demo { - public class Program { - public static void Main(string[] args) { - Console.WriteLine("ReadLine Library Demo"); - Console.WriteLine("---------------------"); - Console.WriteLine(); +namespace ReadLine.Demo +{ + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("ReadLine Library Demo"); + Console.WriteLine("---------------------"); + Console.WriteLine(); - string[] history = { - "ls -a", - "dotnet run", - "git init" - }; - ReadLine.AddHistory(history); + string[] history = + { + "ls -a", + "dotnet run", + "git init" + }; + ReadLine.AddHistory(history); - ReadLine.AutoCompletionHandler = (t, s) => t.StartsWith("git ") - ? new[] { - "init", - "clone", - "pull", - "push" - } - : null; + ReadLine.AutoCompletionHandler = (t, s) => t.StartsWith("git ") + ? new[] + { + "init", + "clone", + "pull", + "push" + } + : null; - var input = ReadLine.Read("(prompt)> "); - Console.Write(input); + var input = ReadLine.Read("(prompt)> "); + Console.Write(input); + } } - } } \ No newline at end of file diff --git a/src/ReadLine/Abstractions/Console2.cs b/src/ReadLine/Abstractions/Console2.cs index f7188d2..d4d2aa0 100644 --- a/src/ReadLine/Abstractions/Console2.cs +++ b/src/ReadLine/Abstractions/Console2.cs @@ -1,31 +1,34 @@ using System; -namespace ReadLine.Abstractions { - internal class Console2 : IConsole { - public int CursorLeft => Console.CursorLeft; +namespace ReadLine.Abstractions +{ + internal class Console2 : IConsole + { + public bool PasswordMode { get; set; } + public int CursorLeft => Console.CursorLeft; - public int CursorTop => Console.CursorTop; + public int CursorTop => Console.CursorTop; - public int BufferWidth => Console.BufferWidth; + public int BufferWidth => Console.BufferWidth; - public int BufferHeight => Console.BufferHeight; + public int BufferHeight => Console.BufferHeight; - public bool PasswordMode { get; set; } + public void SetBufferSize(int width, int height) => Console.SetBufferSize(width, height); - public void SetBufferSize(int width, int height) => Console.SetBufferSize(width, height); + public void SetCursorPosition(int left, int top) + { + if (!PasswordMode) + Console.SetCursorPosition(left, top); + } - public void SetCursorPosition(int left, int top) { - if (!PasswordMode) - Console.SetCursorPosition(left, top); - } + public void Write(string value) + { + if (PasswordMode) + value = new string(default(char), value.Length); - public void Write(string value) { - if (PasswordMode) - value = new string(default(char), value.Length); + Console.Write(value); + } - Console.Write(value); + public void WriteLine(string value) => Console.WriteLine(value); } - - public void WriteLine(string value) => Console.WriteLine(value); - } } \ No newline at end of file diff --git a/src/ReadLine/Abstractions/IConsole.cs b/src/ReadLine/Abstractions/IConsole.cs index 6b764bc..c128a00 100644 --- a/src/ReadLine/Abstractions/IConsole.cs +++ b/src/ReadLine/Abstractions/IConsole.cs @@ -1,12 +1,14 @@ -namespace ReadLine.Abstractions { - public interface IConsole { - int CursorLeft { get; } - int CursorTop { get; } - int BufferWidth { get; } - int BufferHeight { get; } - void SetCursorPosition(int left, int top); - void SetBufferSize(int width, int height); - void Write(string value); - void WriteLine(string value); - } +namespace ReadLine.Abstractions +{ + public interface IConsole + { + int CursorLeft { get; } + int CursorTop { get; } + int BufferWidth { get; } + int BufferHeight { get; } + void SetCursorPosition(int left, int top); + void SetBufferSize(int width, int height); + void Write(string value); + void WriteLine(string value); + } } \ No newline at end of file diff --git a/src/ReadLine/KeyHandler.cs b/src/ReadLine/KeyHandler.cs index 5a2a225..fdb40c0 100644 --- a/src/ReadLine/KeyHandler.cs +++ b/src/ReadLine/KeyHandler.cs @@ -3,273 +3,305 @@ using System.Text; using ReadLine.Abstractions; -namespace ReadLine { - public class KeyHandler { - private int _cursorPos; - private int _cursorLimit; - private readonly StringBuilder _text; - private readonly List _history; - private int _historyIndex; - private ConsoleKeyInfo _keyInfo; - private readonly Dictionary _keyActions; - private string[] _completions; - private int _completionStart; - private int _completionsIndex; - private readonly IConsole _console2; - - private bool IsStartOfLine() => _cursorPos == 0; - - private bool IsEndOfLine() => _cursorPos == _cursorLimit; - - private bool IsStartOfBuffer() => _console2.CursorLeft == 0; - - private bool IsEndOfBuffer() => _console2.CursorLeft == _console2.BufferWidth - 1; - private bool IsInAutoCompleteMode() => _completions != null; - - private void MoveCursorLeft() { - if (IsStartOfLine()) - return; - - if (IsStartOfBuffer()) - _console2.SetCursorPosition(_console2.BufferWidth - 1, _console2.CursorTop - 1); - else - _console2.SetCursorPosition(_console2.CursorLeft - 1, _console2.CursorTop); - - _cursorPos--; - } +namespace ReadLine +{ + public class KeyHandler + { + private readonly IConsole _console2; + private readonly List _history; + private readonly Dictionary _keyActions; + private readonly StringBuilder _text; + private string[] _completions; + private int _completionsIndex; + private int _completionStart; + private int _cursorLimit; + private int _cursorPos; + private int _historyIndex; + private ConsoleKeyInfo _keyInfo; + + public KeyHandler(IConsole console, List history, Func autoCompleteHandler) + { + _console2 = console; + + _historyIndex = history.Count; + _history = history; + _text = new StringBuilder(); + _keyActions = new Dictionary + { + ["LeftArrow"] = MoveCursorLeft, + ["Home"] = MoveCursorHome, + ["End"] = MoveCursorEnd, + ["ControlA"] = MoveCursorHome, + ["ControlB"] = MoveCursorLeft, + ["RightArrow"] = MoveCursorRight, + ["ControlF"] = MoveCursorRight, + ["ControlE"] = MoveCursorEnd, + ["Backspace"] = Backspace, + ["Delete"] = Delete, + ["ControlH"] = Backspace, + ["ControlL"] = ClearLine, + ["UpArrow"] = PrevHistory, + ["ControlP"] = PrevHistory, + ["DownArrow"] = NextHistory, + ["ControlN"] = NextHistory, + ["ControlU"] = () => + { + while (!IsStartOfLine()) + Backspace(); + }, + ["ControlK"] = () => + { + var pos = _cursorPos; + MoveCursorEnd(); + while (_cursorPos > pos) + Backspace(); + }, + ["ControlW"] = () => + { + while (!IsStartOfLine() && _text[_cursorPos - 1] != ' ') + Backspace(); + }, + ["Tab"] = () => + { + if (IsInAutoCompleteMode()) + NextAutoComplete(); + else + { + if (autoCompleteHandler == null || !IsEndOfLine()) + return; + + var anyOf = new[] + { + ' ', + '.', + '/', + '\\', + ':' + }; + var text = _text.ToString(); + + _completionStart = text.LastIndexOfAny(anyOf); + _completionStart = _completionStart == -1 ? 0 : _completionStart + 1; + + _completions = autoCompleteHandler.Invoke(text, _completionStart); + _completions = _completions?.Length == 0 ? null : _completions; + + if (_completions == null) + return; + + StartAutoComplete(); + } + }, + ["ShiftTab"] = () => + { + if (IsInAutoCompleteMode()) + PreviousAutoComplete(); + } + }; + } - private void MoveCursorHome() { - while (!IsStartOfLine()) - MoveCursorLeft(); - } + public string Text => _text.ToString(); - private string BuildKeyInput() { - return (_keyInfo.Modifiers != ConsoleModifiers.Control && _keyInfo.Modifiers != ConsoleModifiers.Shift) ? _keyInfo.Key.ToString() : _keyInfo.Modifiers + _keyInfo.Key.ToString(); - } + private bool IsStartOfLine() => _cursorPos == 0; - private void MoveCursorRight() { - if (IsEndOfLine()) - return; + private bool IsEndOfLine() => _cursorPos == _cursorLimit; - if (IsEndOfBuffer()) - _console2.SetCursorPosition(0, _console2.CursorTop + 1); - else - _console2.SetCursorPosition(_console2.CursorLeft + 1, _console2.CursorTop); + private bool IsStartOfBuffer() => _console2.CursorLeft == 0; - _cursorPos++; - } + private bool IsEndOfBuffer() => _console2.CursorLeft == _console2.BufferWidth - 1; + private bool IsInAutoCompleteMode() => _completions != null; - private void MoveCursorEnd() { - while (!IsEndOfLine()) - MoveCursorRight(); - } + private void MoveCursorLeft() + { + if (IsStartOfLine()) + return; - private void ClearLine() { - MoveCursorEnd(); - while (!IsStartOfLine()) - Backspace(); - } + if (IsStartOfBuffer()) + _console2.SetCursorPosition(_console2.BufferWidth - 1, _console2.CursorTop - 1); + else + _console2.SetCursorPosition(_console2.CursorLeft - 1, _console2.CursorTop); - private void WriteNewString(string str) { - ClearLine(); - foreach (var character in str) - WriteChar(character); - } + _cursorPos--; + } - private void WriteString(string str) { - foreach (var character in str) - WriteChar(character); - } + private void MoveCursorHome() + { + while (!IsStartOfLine()) + MoveCursorLeft(); + } - private void WriteChar() => WriteChar(_keyInfo.KeyChar); - - private void WriteChar(char c) { - if (IsEndOfLine()) { - _text.Append(c); - _console2.Write(c.ToString()); - _cursorPos++; - } else { - var left = _console2.CursorLeft; - var top = _console2.CursorTop; - var str = _text.ToString().Substring(_cursorPos); - _text.Insert(_cursorPos, c); - _console2.Write(c + str); - _console2.SetCursorPosition(left, top); - MoveCursorRight(); - } - - _cursorLimit++; - } + private string BuildKeyInput() + { + return _keyInfo.Modifiers != ConsoleModifiers.Control && _keyInfo.Modifiers != ConsoleModifiers.Shift ? _keyInfo.Key.ToString() : _keyInfo.Modifiers + _keyInfo.Key.ToString(); + } - private void Backspace() { - if (IsStartOfLine()) - return; - - MoveCursorLeft(); - var index = _cursorPos; - _text.Remove(index, 1); - var replacement = _text.ToString().Substring(index); - var left = _console2.CursorLeft; - var top = _console2.CursorTop; - _console2.Write($"{replacement} "); - _console2.SetCursorPosition(left, top); - _cursorLimit--; - } + private void MoveCursorRight() + { + if (IsEndOfLine()) + return; - private void Delete() { - if (IsEndOfLine()) - return; - - var index = _cursorPos; - _text.Remove(index, 1); - var replacement = _text.ToString().Substring(index); - var left = _console2.CursorLeft; - var top = _console2.CursorTop; - _console2.Write($"{replacement} "); - _console2.SetCursorPosition(left, top); - _cursorLimit--; - } + if (IsEndOfBuffer()) + _console2.SetCursorPosition(0, _console2.CursorTop + 1); + else + _console2.SetCursorPosition(_console2.CursorLeft + 1, _console2.CursorTop); - private void StartAutoComplete() { - while (_cursorPos > _completionStart) - Backspace(); + _cursorPos++; + } - _completionsIndex = 0; + private void MoveCursorEnd() + { + while (!IsEndOfLine()) + MoveCursorRight(); + } - WriteString(_completions[_completionsIndex]); - } + private void ClearLine() + { + MoveCursorEnd(); + while (!IsStartOfLine()) + Backspace(); + } - private void NextAutoComplete() { - while (_cursorPos > _completionStart) - Backspace(); + private void WriteNewString(string str) + { + ClearLine(); + foreach (var character in str) + WriteChar(character); + } - _completionsIndex++; + private void WriteString(string str) + { + foreach (var character in str) + WriteChar(character); + } - if (_completionsIndex == _completions.Length) - _completionsIndex = 0; + private void WriteChar() => WriteChar(_keyInfo.KeyChar); + + private void WriteChar(char c) + { + if (IsEndOfLine()) + { + _text.Append(c); + _console2.Write(c.ToString()); + _cursorPos++; + } else + { + var left = _console2.CursorLeft; + var top = _console2.CursorTop; + var str = _text.ToString().Substring(_cursorPos); + _text.Insert(_cursorPos, c); + _console2.Write(c + str); + _console2.SetCursorPosition(left, top); + MoveCursorRight(); + } + + _cursorLimit++; + } - WriteString(_completions[_completionsIndex]); - } + private void Backspace() + { + if (IsStartOfLine()) + return; + + MoveCursorLeft(); + var index = _cursorPos; + _text.Remove(index, 1); + var replacement = _text.ToString().Substring(index); + var left = _console2.CursorLeft; + var top = _console2.CursorTop; + _console2.Write($"{replacement} "); + _console2.SetCursorPosition(left, top); + _cursorLimit--; + } - private void PreviousAutoComplete() { - while (_cursorPos > _completionStart) - Backspace(); + private void Delete() + { + if (IsEndOfLine()) + return; + + var index = _cursorPos; + _text.Remove(index, 1); + var replacement = _text.ToString().Substring(index); + var left = _console2.CursorLeft; + var top = _console2.CursorTop; + _console2.Write($"{replacement} "); + _console2.SetCursorPosition(left, top); + _cursorLimit--; + } - _completionsIndex--; + private void StartAutoComplete() + { + while (_cursorPos > _completionStart) + Backspace(); - if (_completionsIndex == -1) - _completionsIndex = _completions.Length - 1; + _completionsIndex = 0; - WriteString(_completions[_completionsIndex]); - } + WriteString(_completions[_completionsIndex]); + } - private void PrevHistory() { - if (_historyIndex > 0) { - _historyIndex--; - WriteNewString(_history[_historyIndex]); - } - } + private void NextAutoComplete() + { + while (_cursorPos > _completionStart) + Backspace(); - private void NextHistory() { - if (_historyIndex < _history.Count) { - _historyIndex++; - if (_historyIndex == _history.Count) - ClearLine(); - else - WriteNewString(_history[_historyIndex]); - } - } + _completionsIndex++; - private void ResetAutoComplete() { - _completions = null; - _completionsIndex = 0; - } + if (_completionsIndex == _completions.Length) + _completionsIndex = 0; - public string Text => _text.ToString(); - - public KeyHandler(IConsole console, List history, Func autoCompleteHandler) { - _console2 = console; - - _historyIndex = history.Count; - _history = history; - _text = new StringBuilder(); - _keyActions = new Dictionary { - ["LeftArrow"] = MoveCursorLeft, - ["Home"] = MoveCursorHome, - ["End"] = MoveCursorEnd, - ["ControlA"] = MoveCursorHome, - ["ControlB"] = MoveCursorLeft, - ["RightArrow"] = MoveCursorRight, - ["ControlF"] = MoveCursorRight, - ["ControlE"] = MoveCursorEnd, - ["Backspace"] = Backspace, - ["Delete"] = Delete, - ["ControlH"] = Backspace, - ["ControlL"] = ClearLine, - ["UpArrow"] = PrevHistory, - ["ControlP"] = PrevHistory, - ["DownArrow"] = NextHistory, - ["ControlN"] = NextHistory, - ["ControlU"] = () => { - while (!IsStartOfLine()) - Backspace(); - }, - ["ControlK"] = () => { - var pos = _cursorPos; - MoveCursorEnd(); - while (_cursorPos > pos) - Backspace(); - }, - ["ControlW"] = () => { - while (!IsStartOfLine() && _text[_cursorPos - 1] != ' ') - Backspace(); - }, - ["Tab"] = () => { - if (IsInAutoCompleteMode()) { - NextAutoComplete(); - } else { - if (autoCompleteHandler == null || !IsEndOfLine()) - return; - - var anyOf = new[] { - ' ', - '.', - '/', - '\\', - ':' - }; - var text = _text.ToString(); + WriteString(_completions[_completionsIndex]); + } - _completionStart = text.LastIndexOfAny(anyOf); - _completionStart = _completionStart == -1 ? 0 : _completionStart + 1; + private void PreviousAutoComplete() + { + while (_cursorPos > _completionStart) + Backspace(); - _completions = autoCompleteHandler.Invoke(text, _completionStart); - _completions = _completions?.Length == 0 ? null : _completions; + _completionsIndex--; - if (_completions == null) - return; + if (_completionsIndex == -1) + _completionsIndex = _completions.Length - 1; - StartAutoComplete(); - } - }, - ["ShiftTab"] = () => { - if (IsInAutoCompleteMode()) { - PreviousAutoComplete(); - } + WriteString(_completions[_completionsIndex]); + } + + private void PrevHistory() + { + if (_historyIndex > 0) + { + _historyIndex--; + WriteNewString(_history[_historyIndex]); + } } - }; - } - public void Handle(ConsoleKeyInfo keyInfo) { - _keyInfo = keyInfo; + private void NextHistory() + { + if (_historyIndex < _history.Count) + { + _historyIndex++; + if (_historyIndex == _history.Count) + ClearLine(); + else + WriteNewString(_history[_historyIndex]); + } + } + + private void ResetAutoComplete() + { + _completions = null; + _completionsIndex = 0; + } - // If in auto complete mode and Tab wasn't pressed - if (IsInAutoCompleteMode() && _keyInfo.Key != ConsoleKey.Tab) - ResetAutoComplete(); + public void Handle(ConsoleKeyInfo keyInfo) + { + _keyInfo = keyInfo; - _keyActions.TryGetValue(BuildKeyInput(), out var action); - action = action ?? WriteChar; - action.Invoke(); + // If in auto complete mode and Tab wasn't pressed + if (IsInAutoCompleteMode() && _keyInfo.Key != ConsoleKey.Tab) + ResetAutoComplete(); + + _keyActions.TryGetValue(BuildKeyInput(), out var action); + action = action ?? WriteChar; + action.Invoke(); + } } - } } \ No newline at end of file diff --git a/src/ReadLine/Properties/AssemblyInfo.cs b/src/ReadLine/Properties/AssemblyInfo.cs index 9d08c04..ba9f49b 100644 --- a/src/ReadLine/Properties/AssemblyInfo.cs +++ b/src/ReadLine/Properties/AssemblyInfo.cs @@ -1,5 +1,7 @@ -[assembly: System.Reflection.AssemblyTitle("ReadLine")] -[assembly: System.Reflection.AssemblyDescription("A GNU-Readline like library for .NET/.NET Core")] -[assembly: System.Reflection.AssemblyFileVersion("1.2.1")] -[assembly: System.Reflection.AssemblyVersion("1.2.1")] -[assembly: System.Reflection.AssemblyInformationalVersion("1.2.1")] \ No newline at end of file +using System.Reflection; + +[assembly: AssemblyTitle("ReadLine")] +[assembly: AssemblyDescription("A GNU-Readline like library for .NET/.NET Core")] +[assembly: AssemblyFileVersion("1.2.1")] +[assembly: AssemblyVersion("1.2.1")] +[assembly: AssemblyInformationalVersion("1.2.1")] \ No newline at end of file diff --git a/src/ReadLine/ReadLine.cs b/src/ReadLine/ReadLine.cs index 67d7a81..a67f7e3 100755 --- a/src/ReadLine/ReadLine.cs +++ b/src/ReadLine/ReadLine.cs @@ -2,43 +2,50 @@ using System.Collections.Generic; using ReadLine.Abstractions; -namespace ReadLine { - public static class ReadLine { - private static KeyHandler _keyHandler; - private static List _history; - - static ReadLine() { - _history = new List(); - } - - public static void AddHistory(params string[] text) => _history.AddRange(text); - public static List GetHistory() => _history; - public static void ClearHistory() => _history = new List(); - public static Func AutoCompletionHandler { private get; set; } - public static bool PasswordMode { private get; set; } - - public static string Read(string prompt = "", string defaultInput = "") { - Console.Write(prompt); - - _keyHandler = new KeyHandler(new Console2() { - PasswordMode = PasswordMode - }, _history, AutoCompletionHandler); - var keyInfo = Console.ReadKey(true); - - while (keyInfo.Key != ConsoleKey.Enter) { - _keyHandler.Handle(keyInfo); - keyInfo = Console.ReadKey(true); - } - - Console.WriteLine(); - - var text = _keyHandler.Text; - if (string.IsNullOrWhiteSpace(text) && !string.IsNullOrWhiteSpace(defaultInput)) - text = defaultInput; - else - _history.Add(text); - - return text; +namespace ReadLine +{ + public static class ReadLine + { + private static KeyHandler _keyHandler; + private static List _history; + + static ReadLine() + { + _history = new List(); + } + + public static Func AutoCompletionHandler { private get; set; } + public static bool PasswordMode { private get; set; } + + public static void AddHistory(params string[] text) => _history.AddRange(text); + public static List GetHistory() => _history; + public static void ClearHistory() => _history = new List(); + + public static string Read(string prompt = "", string defaultInput = "") + { + Console.Write(prompt); + + _keyHandler = new KeyHandler(new Console2 + { + PasswordMode = PasswordMode + }, _history, AutoCompletionHandler); + var keyInfo = Console.ReadKey(true); + + while (keyInfo.Key != ConsoleKey.Enter) + { + _keyHandler.Handle(keyInfo); + keyInfo = Console.ReadKey(true); + } + + Console.WriteLine(); + + var text = _keyHandler.Text; + if (string.IsNullOrWhiteSpace(text) && !string.IsNullOrWhiteSpace(defaultInput)) + text = defaultInput; + else + _history.Add(text); + + return text; + } } - } } \ No newline at end of file diff --git a/src/ReadLine/ReadLine.csproj b/src/ReadLine/ReadLine.csproj index 56295ee..0643dc4 100755 --- a/src/ReadLine/ReadLine.csproj +++ b/src/ReadLine/ReadLine.csproj @@ -3,17 +3,17 @@ A GNU-Readline like library for .NET/.NET Core ReadLine - Toni Solarin-Sodara;Latency McLaughlin + Toni Solarin-Sodara Toni Solarin-Sodara net47;netcoreapp2.0;netstandard2.0 1.2.1 ReadLine ReadLine readline;gnu;console;shell;cui - https://github.com/tsolarin/readline - https://github.com/tsolarin/readline/blob/master/LICENSE + https://github.com/tonerdo/readline + https://github.com/tonerdo/readline/blob/master/LICENSE git - https://github.com/tsolarin/readline + https://github.com/tonerdo/readline false false false @@ -29,7 +29,7 @@ Consolidated redundant code and expressions. Repacked for multi-frameworks. Updated versioning. Updated test projects. - + Toni Solarin-Sodara diff --git a/test/ReadLine.Tests/Abstractions/Console2.cs b/test/ReadLine.Tests/Abstractions/Console2.cs index c84ce25..338e874 100644 --- a/test/ReadLine.Tests/Abstractions/Console2.cs +++ b/test/ReadLine.Tests/Abstractions/Console2.cs @@ -1,43 +1,45 @@ using ReadLine.Abstractions; -namespace ReadLine.Tests.Abstractions { - internal class Console2 : IConsole { - public int CursorLeft => _cursorLeft; - - public int CursorTop => _cursorTop; - - public int BufferWidth => _bufferWidth; - - public int BufferHeight => _bufferHeight; - - private int _cursorLeft; - private int _cursorTop; - private int _bufferWidth; - private int _bufferHeight; - - public Console2() { - _cursorLeft = 0; - _cursorTop = 0; - _bufferWidth = 100; - _bufferHeight = 100; - } - - public void SetBufferSize(int width, int height) { - _bufferWidth = width; - _bufferHeight = height; - } - - public void SetCursorPosition(int left, int top) { - _cursorLeft = left; - _cursorTop = top; - } - - public void Write(string value) { - _cursorLeft += value.Length; - } - - public void WriteLine(string value) { - _cursorLeft += value.Length; +namespace ReadLine.Tests.Abstractions +{ + internal class Console2 : IConsole + { + public Console2() + { + CursorLeft = 0; + CursorTop = 0; + BufferWidth = 100; + BufferHeight = 100; + } + + public int CursorLeft { get; private set; } + + public int CursorTop { get; private set; } + + public int BufferWidth { get; private set; } + + public int BufferHeight { get; private set; } + + public void SetBufferSize(int width, int height) + { + BufferWidth = width; + BufferHeight = height; + } + + public void SetCursorPosition(int left, int top) + { + CursorLeft = left; + CursorTop = top; + } + + public void Write(string value) + { + CursorLeft += value.Length; + } + + public void WriteLine(string value) + { + CursorLeft += value.Length; + } } - } } \ No newline at end of file diff --git a/test/ReadLine.Tests/KeyHandlerTests.cs b/test/ReadLine.Tests/KeyHandlerTests.cs index 259b3f4..90b470f 100644 --- a/test/ReadLine.Tests/KeyHandlerTests.cs +++ b/test/ReadLine.Tests/KeyHandlerTests.cs @@ -1,396 +1,431 @@ using System; using System.Collections.Generic; -using Xunit; using ReadLine.Tests.Abstractions; +using Xunit; namespace ReadLine.Tests { - public class KeyHandlerTests { - private KeyHandler _keyHandler; - private ConsoleKeyInfo _keyInfo; - private readonly List _history; - private readonly string[] _completions; - - public KeyHandlerTests() { - _completions = new[] { - "World", - "Angel", - "Love" - }; - _history = new List(new[] { - "dotnet run", - "git init", - "clear" - }); - _keyHandler = new KeyHandler(new Console2(), _history, null); - - _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('e', ConsoleKey.E, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); - _keyHandler.Handle(_keyInfo); - } + public class KeyHandlerTests + { + public KeyHandlerTests() + { + _completions = new[] + { + "World", + "Angel", + "Love" + }; + _history = new List(new[] + { + "dotnet run", + "git init", + "clear" + }); + _keyHandler = new KeyHandler(new Console2(), _history, null); + + _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('e', ConsoleKey.E, false, false, false); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); + _keyHandler.Handle(_keyInfo); + } + + private KeyHandler _keyHandler; + private ConsoleKeyInfo _keyInfo; + private readonly List _history; + private readonly string[] _completions; + + [Fact] + public void TestBackspace() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Backspace, false, false, false); + _keyHandler.Handle(_keyInfo); + + Assert.Equal("Hell", _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 (var 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); + } + } + + [Fact] + public void TestControlA() + { + _keyInfo = new ConsoleKeyInfo('A', ConsoleKey.A, false, false, true); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('S', ConsoleKey.S, false, false, false); + _keyHandler.Handle(_keyInfo); + + Assert.Equal("SHello", _keyHandler.Text); + } + + [Fact] + public void TestControlB() + { + _keyInfo = new ConsoleKeyInfo('B', ConsoleKey.B, false, false, true); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, false); + _keyHandler.Handle(_keyInfo); + + Assert.Equal("Hell No", _keyHandler.Text); + } + + [Fact] + public void TestControlE() + { + _keyInfo = new ConsoleKeyInfo('A', ConsoleKey.A, false, false, true); + _keyHandler.Handle(_keyInfo); + + _keyInfo = new ConsoleKeyInfo('E', ConsoleKey.E, false, false, true); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestWriteChar() { - Assert.Equal("Hello", _keyHandler.Text); + _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hello!", _keyHandler.Text); + } - _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlF() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('F', ConsoleKey.F, false, false, true); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('r', ConsoleKey.R, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hello!", _keyHandler.Text); + } - _keyInfo = new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlH() + { + _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, true); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hello World", _keyHandler.Text); - } + Assert.Equal("Hell", _keyHandler.Text); + } - [Fact] - public void TestBackspace() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Backspace, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlK() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hell", _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('K', ConsoleKey.K, false, false, true); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestDelete() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Delete, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hell", _keyHandler.Text); - Assert.Equal("Hell", _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestDelete_EndOfLine() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Delete, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('K', ConsoleKey.K, false, false, true); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hello", _keyHandler.Text); - } + Assert.Equal(string.Empty, _keyHandler.Text); + } - [Fact] - public void TestControlH() { - _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, true); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlL() + { + _keyInfo = new ConsoleKeyInfo('L', ConsoleKey.L, false, false, true); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hell", _keyHandler.Text); - } + Assert.Equal(string.Empty, _keyHandler.Text); + } - [Fact] - public void TestHome() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlN() + { + for (var i = _history.Count - 1; i >= 0; i--) + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); + _keyHandler.Handle(_keyInfo); + } - _keyInfo = new ConsoleKeyInfo('S', ConsoleKey.S, false, false, false); - _keyHandler.Handle(_keyInfo); + foreach (var t in _history) + { + Assert.Equal(t, _keyHandler.Text); - Assert.Equal("SHello", _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, true); + _keyHandler.Handle(_keyInfo); + } + } - [Fact] - public void TestControlA() { - _keyInfo = new ConsoleKeyInfo('A', ConsoleKey.A, false, false, true); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlP() + { + for (var i = _history.Count - 1; i >= 0; i--) + { + _keyInfo = new ConsoleKeyInfo('P', ConsoleKey.P, false, false, true); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('S', ConsoleKey.S, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal(_history[i], _keyHandler.Text); + } + } + + [Fact] + public void TestControlU() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal("SHello", _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('U', ConsoleKey.U, false, false, true); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestEnd() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("o", _keyHandler.Text); - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.End, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.End, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('U', ConsoleKey.U, false, false, true); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hello!", _keyHandler.Text); - } + Assert.Equal(string.Empty, _keyHandler.Text); + } - [Fact] - public void TestControlE() { - _keyInfo = new ConsoleKeyInfo('A', ConsoleKey.A, false, false, true); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestControlW() + { + _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('E', ConsoleKey.E, false, false, true); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hello!", _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('r', ConsoleKey.R, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestLeftArrow() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, true); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hell No", _keyHandler.Text); - } + Assert.Equal("Hello ", _keyHandler.Text); - [Fact] - public void TestControlB() { - _keyInfo = new ConsoleKeyInfo('B', ConsoleKey.B, false, false, true); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Backspace, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, true); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal(string.Empty, _keyHandler.Text); + } + + [Fact] + public void TestDelete() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Delete, false, false, false); + _keyHandler.Handle(_keyInfo); + + Assert.Equal("Hell", _keyHandler.Text); + } - Assert.Equal("Hell No", _keyHandler.Text); - } - - [Fact] - public void TestRightArrow() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.RightArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestDelete_EndOfLine() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Delete, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hello", _keyHandler.Text); + } - Assert.Equal("Hello!", _keyHandler.Text); - } - - [Fact] - public void TestControlF() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + [Fact] + public void TestDownArrow() + { + for (var i = _history.Count - 1; i >= 0; i--) + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); + _keyHandler.Handle(_keyInfo); + } - _keyInfo = new ConsoleKeyInfo('F', ConsoleKey.F, false, false, true); - _keyHandler.Handle(_keyInfo); + foreach (var t in _history) + { + Assert.Equal(t, _keyHandler.Text); - _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.DownArrow, false, false, false); + _keyHandler.Handle(_keyInfo); + } + } - Assert.Equal("Hello!", _keyHandler.Text); - } + [Fact] + public void TestEnd() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestControlL() { - _keyInfo = new ConsoleKeyInfo('L', ConsoleKey.L, false, false, true); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.End, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal(string.Empty, _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestUpArrow() { - for (int i = _history.Count - 1; i >= 0; i--) { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hello!", _keyHandler.Text); + } - Assert.Equal(_history[i], _keyHandler.Text); - } - } + [Fact] + public void TestHome() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestControlP() { - for (int i = _history.Count - 1; i >= 0; i--) { - _keyInfo = new ConsoleKeyInfo('P', ConsoleKey.P, false, false, true); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('S', ConsoleKey.S, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal(_history[i], _keyHandler.Text); - } - } + Assert.Equal("SHello", _keyHandler.Text); + } - [Fact] - public void TestDownArrow() { - for (int i = _history.Count - 1; i >= 0; i--) { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); - _keyHandler.Handle(_keyInfo); - } + [Fact] + public void TestLeftArrow() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - foreach (var t in _history) { - Assert.Equal(t, _keyHandler.Text); + _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.DownArrow, false, false, false); - _keyHandler.Handle(_keyInfo); - } - } + _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestControlN() { - for (int i = _history.Count - 1; i >= 0; i--) { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); - _keyHandler.Handle(_keyInfo); - } + Assert.Equal("Hell No", _keyHandler.Text); + } - foreach (var t in _history) { - Assert.Equal(t, _keyHandler.Text); + [Fact] + public void TestRightArrow() + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('N', ConsoleKey.N, false, false, true); - _keyHandler.Handle(_keyInfo); - } - } + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.RightArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestControlU() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('!', ConsoleKey.D0, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('U', ConsoleKey.U, false, false, true); - _keyHandler.Handle(_keyInfo); + Assert.Equal("Hello!", _keyHandler.Text); + } - Assert.Equal("o", _keyHandler.Text); + [Fact] + public void TestTab() + { + _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); - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.End, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyHandler = new KeyHandler(new Console2(), _history, (t, s) => _completions); - _keyInfo = new ConsoleKeyInfo('U', ConsoleKey.U, false, false, true); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal(string.Empty, _keyHandler.Text); - } + _keyInfo = new ConsoleKeyInfo('i', ConsoleKey.I, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestControlK() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.LeftArrow, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('K', ConsoleKey.K, false, false, true); - _keyHandler.Handle(_keyInfo); + foreach (var t in _completions) + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal("Hell", _keyHandler.Text); + Assert.Equal($"Hi {t}", _keyHandler.Text); + } + } - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Home, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('K', ConsoleKey.K, false, false, true); - _keyHandler.Handle(_keyInfo); - - Assert.Equal(string.Empty, _keyHandler.Text); - } - - [Fact] - public void TestControlW() { - _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('r', ConsoleKey.R, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, true); - _keyHandler.Handle(_keyInfo); - - Assert.Equal("Hello ", _keyHandler.Text); - - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Backspace, false, false, false); - _keyHandler.Handle(_keyInfo); - - _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, true); - _keyHandler.Handle(_keyInfo); - - Assert.Equal(string.Empty, _keyHandler.Text); - } - - [Fact] - public void TestTab() { - _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); - - foreach (var t in _completions) { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false); - _keyHandler.Handle(_keyInfo); - - Assert.Equal($"Hi {t}", _keyHandler.Text); - } - } + [Fact] + public void TestUpArrow() + { + for (var i = _history.Count - 1; i >= 0; i--) + { + _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.UpArrow, false, false, false); + _keyHandler.Handle(_keyInfo); - [Fact] - public void TestBackwardsTab() { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false); - _keyHandler.Handle(_keyInfo); + Assert.Equal(_history[i], _keyHandler.Text); + } + } - // Nothing happens when no auto complete handler is set - Assert.Equal("Hello", _keyHandler.Text); + [Fact] + public void TestWriteChar() + { + Assert.Equal("Hello", _keyHandler.Text); - _keyHandler = new KeyHandler(new Console2(), _history, (t, s) => _completions); + _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('W', ConsoleKey.W, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo('i', ConsoleKey.I, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('o', ConsoleKey.O, false, false, false); + _keyHandler.Handle(_keyInfo); - _keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('r', ConsoleKey.R, false, false, false); + _keyHandler.Handle(_keyInfo); - // Bring up the first Autocomplete - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('l', ConsoleKey.L, false, false, false); + _keyHandler.Handle(_keyInfo); - for (var i = _completions.Length - 1; i >= 0; i--) { - _keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, true, false, false); - _keyHandler.Handle(_keyInfo); + _keyInfo = new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false); + _keyHandler.Handle(_keyInfo); - Assert.Equal($"Hi {_completions[i]}", _keyHandler.Text); - } + Assert.Equal("Hello World", _keyHandler.Text); + } } - } } \ No newline at end of file diff --git a/test/ReadLine.Tests/ReadLine.Tests.csproj b/test/ReadLine.Tests/ReadLine.Tests.csproj index c22ae78..bf74bf8 100755 --- a/test/ReadLine.Tests/ReadLine.Tests.csproj +++ b/test/ReadLine.Tests/ReadLine.Tests.csproj @@ -10,9 +10,6 @@ - - - diff --git a/test/ReadLine.Tests/ReadLineTests.cs b/test/ReadLine.Tests/ReadLineTests.cs index fdcd3a2..9634a64 100755 --- a/test/ReadLine.Tests/ReadLineTests.cs +++ b/test/ReadLine.Tests/ReadLineTests.cs @@ -2,40 +2,48 @@ using System.Linq; using Xunit; -namespace ReadLine.Tests { - public class ReadLineTests : IDisposable { - public ReadLineTests() { - string[] history = { - "ls -a", - "dotnet run", - "git init" - }; - ReadLine.AddHistory(history); - } +namespace ReadLine.Tests +{ + public class ReadLineTests : IDisposable + { + public ReadLineTests() + { + string[] history = + { + "ls -a", + "dotnet run", + "git init" + }; + ReadLine.AddHistory(history); + } - [Fact] - public void TestNoInitialHistory() { - Assert.Equal(3, ReadLine.GetHistory().Count); - } + public void Dispose() + { + // If all above tests pass + // clear history works + ReadLine.ClearHistory(); + } - [Fact] - public void TestUpdatesHistory() { - ReadLine.AddHistory("mkdir"); - Assert.Equal(4, ReadLine.GetHistory().Count); - Assert.Equal("mkdir", ReadLine.GetHistory().Last()); - } + [Fact] + public void TestGetCorrectHistory() + { + Assert.Equal("ls -a", ReadLine.GetHistory()[0]); + Assert.Equal("dotnet run", ReadLine.GetHistory()[1]); + Assert.Equal("git init", ReadLine.GetHistory()[2]); + } - [Fact] - public void TestGetCorrectHistory() { - Assert.Equal("ls -a", ReadLine.GetHistory()[0]); - Assert.Equal("dotnet run", ReadLine.GetHistory()[1]); - Assert.Equal("git init", ReadLine.GetHistory()[2]); - } + [Fact] + public void TestNoInitialHistory() + { + Assert.Equal(3, ReadLine.GetHistory().Count); + } - public void Dispose() { - // If all above tests pass - // clear history works - ReadLine.ClearHistory(); + [Fact] + public void TestUpdatesHistory() + { + ReadLine.AddHistory("mkdir"); + Assert.Equal(4, ReadLine.GetHistory().Count); + Assert.Equal("mkdir", ReadLine.GetHistory().Last()); + } } - } } \ No newline at end of file