diff --git a/.gitignore b/.gitignore index 68d27ef..7aacc9a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,32 @@ [Oo]bj/ .vscode .DS_Store -project.lock.json \ No newline at end of file +project.lock.json +#ignore thumbnails created by windows +Thumbs.db +#Ignore files build by Visual Studio +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* diff --git a/src/ReadLine.sln b/src/ReadLine.sln new file mode 100644 index 0000000..d8131bb --- /dev/null +++ b/src/ReadLine.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadLine", "ReadLine\ReadLine.csproj", "{EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/ReadLine/Abstractions/Console2.cs b/src/ReadLine/Abstractions/Console2.cs index 6dd292b..4f8ee6c 100644 --- a/src/ReadLine/Abstractions/Console2.cs +++ b/src/ReadLine/Abstractions/Console2.cs @@ -1,23 +1,59 @@ -using System; - -namespace Internal.ReadLine.Abstractions +using System; + +namespace ReadLine.Abstractions { internal class Console2 : IConsole - { - public int CursorLeft => Console.CursorLeft; - - public int CursorTop => Console.CursorTop; - - public int BufferWidth => Console.BufferWidth; - - public int BufferHeight => Console.BufferHeight; - - public void SetBufferSize(int width, int height) => Console.SetBufferSize(width, height); - - public void SetCursorPosition(int left, int top) => Console.SetCursorPosition(left, top); - - public void Write(string value) => Console.Write(value); - - public void WriteLine(string value) => Console.WriteLine(value); + { + public int CursorLeft + { + get + { + return Console.CursorLeft; + } + } + + public int CursorTop + { + get + { + return Console.CursorTop; + } + } + + public int BufferWidth + { + get + { + return Console.BufferWidth; + } + } + + public int BufferHeight + { + get + { + return Console.BufferHeight; + } + } + + public void SetBufferSize(int width, int height) + { + Console.SetBufferSize(width, height); + } + + public void SetCursorPosition(int left, int top) + { + Console.SetCursorPosition(left, top); + } + + public void Write(string value) + { + Console.Write(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 5241515..df29a86 100644 --- a/src/ReadLine/Abstractions/IConsole.cs +++ b/src/ReadLine/Abstractions/IConsole.cs @@ -1,4 +1,4 @@ -namespace Internal.ReadLine.Abstractions +namespace ReadLine.Abstractions { internal interface IConsole { diff --git a/src/ReadLine/KeyHandler.cs b/src/ReadLine/KeyHandler.cs index 5c8fe56..9ab05fc 100644 --- a/src/ReadLine/KeyHandler.cs +++ b/src/ReadLine/KeyHandler.cs @@ -1,33 +1,49 @@ -using Internal.ReadLine.Abstractions; - -using System; -using System.Collections.Generic; -using System.Text; - -namespace Internal.ReadLine +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +using ReadLine.Abstractions; + +namespace ReadLine { internal class KeyHandler { private int _cursorPos; private int _cursorLimit; - private StringBuilder _text; - private List _history; + private readonly StringBuilder _text; + private readonly List _history; private int _historyIndex; private ConsoleKeyInfo _keyInfo; - private Dictionary _keyActions; + private readonly Dictionary _keyActions; private string[] _completions; private int _completionStart; private int _completionsIndex; - private IConsole Console2; + private readonly IConsole Console2; - private bool IsStartOfLine() => _cursorPos == 0; + private bool IsStartOfLine() + { + return _cursorPos == 0; + } - private bool IsEndOfLine() => _cursorPos == _cursorLimit; + private bool IsEndOfLine() + { + return _cursorPos == _cursorLimit; + } - private bool IsStartOfBuffer() => Console2.CursorLeft == 0; + private bool IsStartOfBuffer() + { + return Console2.CursorLeft == 0; + } - private bool IsEndOfBuffer() => Console2.CursorLeft == Console2.BufferWidth - 1; - private bool IsInAutoCompleteMode() => _completions != null; + private bool IsEndOfBuffer() + { + return Console2.CursorLeft == Console2.BufferWidth - 1; + } + private bool IsInAutoCompleteMode() + { + return _completions != null; + } private void MoveCursorLeft() { @@ -51,7 +67,7 @@ private void MoveCursorHome() private string BuildKeyInput() { return (_keyInfo.Modifiers != ConsoleModifiers.Control && _keyInfo.Modifiers != ConsoleModifiers.Shift) ? - _keyInfo.Key.ToString() : _keyInfo.Modifiers.ToString() + _keyInfo.Key.ToString(); + _keyInfo.Key.ToString() : _keyInfo.Modifiers + _keyInfo.Key.ToString(); } private void MoveCursorRight() @@ -83,33 +99,36 @@ private void ClearLine() private void WriteNewString(string str) { ClearLine(); - foreach (char character in str) + foreach (var character in str) WriteChar(character); } private void WriteString(string str) - { - foreach (char character in str) + { + foreach (var character in str) WriteChar(character); } - private void WriteChar() => WriteChar(_keyInfo.KeyChar); + private void WriteChar() + { + WriteChar(_keyInfo.KeyChar); + } private void WriteChar(char character) { if (IsEndOfLine()) { _text.Append(character); - Console2.Write(character.ToString()); + Console2.Write(character.ToString(CultureInfo.InvariantCulture)); _cursorPos++; } else - { - int left = Console2.CursorLeft; - int top = Console2.CursorTop; - string str = _text.ToString().Substring(_cursorPos); + { + var left = Console2.CursorLeft; + var top = Console2.CursorTop; + var str = _text.ToString().Substring(_cursorPos); _text.Insert(_cursorPos, character); - Console2.Write(character.ToString() + str); + Console2.Write(character + str); Console2.SetCursorPosition(left, top); MoveCursorRight(); } @@ -118,19 +137,19 @@ private void WriteChar(char character) } private void Backspace() - { - if (!IsStartOfLine()) - { - MoveCursorLeft(); - int index = _cursorPos; - _text.Remove(index, 1); - string replacement = _text.ToString().Substring(index); - int left = Console2.CursorLeft; - int top = Console2.CursorTop; - Console2.Write(string.Format("{0} ", replacement)); - Console2.SetCursorPosition(left, top); - _cursorLimit--; - } + { + 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(string.Format("{0} ", replacement)); + Console2.SetCursorPosition(left, top); + _cursorLimit--; } private void StartAutoComplete() @@ -170,24 +189,24 @@ private void PreviousAutoComplete() } private void PrevHistory() - { - if (_historyIndex > 0) - { - _historyIndex--; - WriteNewString(_history[_historyIndex]); - } + { + if (_historyIndex <= 0) + return; + + _historyIndex--; + WriteNewString(_history[_historyIndex]); } private void NextHistory() - { - if (_historyIndex < _history.Count) - { - _historyIndex++; - if (_historyIndex == _history.Count) - ClearLine(); - else - WriteNewString(_history[_historyIndex]); - } + { + if (_historyIndex >= _history.Count) + return; + + _historyIndex++; + if (_historyIndex == _history.Count) + ClearLine(); + else + WriteNewString(_history[_historyIndex]); } private void ResetAutoComplete() @@ -235,7 +254,7 @@ public KeyHandler(IConsole console, List history, Func { - int pos = _cursorPos; + var pos = _cursorPos; MoveCursorEnd(); while (_cursorPos > pos) Backspace(); @@ -257,8 +276,8 @@ public KeyHandler(IConsole console, List history, Func(); } - public static void AddHistory(params string[] text) => _history.AddRange(text); - public static List GetHistory() => _history; - public static void ClearHistory() => _history = new List(); + public static void AddHistory(params string[] text) + { + _history.AddRange(text); + } + + public static List GetHistory() + { + return _history; + } + + public static void ClearHistory() + { + _history = new List(); + } public static string Read(string prompt = "") { Console.Write(prompt); _keyHandler = new KeyHandler(new Console2(), _history, AutoCompletionHandler); - ConsoleKeyInfo keyInfo = Console.ReadKey(true); + var keyInfo = Console.ReadKey(true); while (keyInfo.Key != ConsoleKey.Enter) { diff --git a/src/ReadLine/ReadLine.csproj b/src/ReadLine/ReadLine.csproj new file mode 100644 index 0000000..de68e07 --- /dev/null +++ b/src/ReadLine/ReadLine.csproj @@ -0,0 +1,57 @@ + + + + + Debug + AnyCPU + {EB5CCB00-7FBF-4ACE-AAF2-E994F518DAF7} + Library + Properties + ReadLine + ReadLine + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file