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
30 changes: 29 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,32 @@
[Oo]bj/
.vscode
.DS_Store
project.lock.json
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*
20 changes: 20 additions & 0 deletions src/ReadLine.sln
Original file line number Diff line number Diff line change
@@ -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
74 changes: 55 additions & 19 deletions src/ReadLine/Abstractions/Console2.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/ReadLine/Abstractions/IConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Internal.ReadLine.Abstractions
namespace ReadLine.Abstractions
{
internal interface IConsole
{
Expand Down
135 changes: 77 additions & 58 deletions src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
@@ -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<string> _history;
private readonly StringBuilder _text;
private readonly List<string> _history;
private int _historyIndex;
private ConsoleKeyInfo _keyInfo;
private Dictionary<string, Action> _keyActions;
private readonly Dictionary<string, Action> _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()
{
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -235,7 +254,7 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
};
_keyActions["ControlK"] = () =>
{
int pos = _cursorPos;
var pos = _cursorPos;
MoveCursorEnd();
while (_cursorPos > pos)
Backspace();
Expand All @@ -257,8 +276,8 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
if (autoCompleteHandler == null || !IsEndOfLine())
return;

char[] anyOf = new char[] { ' ', '.', '/', '\\', ':' };
string text = _text.ToString();
var anyOf = new[] { ' ', '.', '/', '\\', ':' };
var text = _text.ToString();

_completionStart = text.LastIndexOfAny(anyOf);
_completionStart = _completionStart == -1 ? 0 : _completionStart + 1;
Expand Down
1 change: 0 additions & 1 deletion src/ReadLine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
[assembly:System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly:System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
[assembly:System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly:System.Runtime.Versioning.TargetFrameworkAttribute(".NETStandard,Version=v1.3")]
[assembly:System.Runtime.CompilerServices.InternalsVisibleTo("ReadLine.Tests")]
Loading