Skip to content

Commit

Permalink
Merge pull request #22 from tsolarin/password-input
Browse files Browse the repository at this point in the history
Add password mode for console input
  • Loading branch information
tonerdo authored May 21, 2017
2 parents 5ca4a44 + 500cd08 commit 8375cd3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Install-Package ReadLine
string input = ReadLine.Read("(prompt)> ");
```

### Read password from the console

```csharp
ReadLine.PasswordMode = true;
string password = ReadLine.Read("(prompt)> ");
```

_Note: The `(prompt>)` is optional_

### History management
Expand Down
16 changes: 14 additions & 2 deletions src/ReadLine/Abstractions/Console2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ internal class Console2 : IConsole

public int BufferHeight => Console.BufferHeight;

public bool PasswordMode { get; set; }

public void SetBufferSize(int width, int height) => Console.SetBufferSize(width, height);

public void SetCursorPosition(int left, int top) => 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) => Console.Write(value);
Console.Write(value);
}

public void WriteLine(string value) => Console.WriteLine(value);
}
Expand Down
36 changes: 18 additions & 18 deletions src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ private void WriteString(string str)

private void WriteChar() => WriteChar(_keyInfo.KeyChar);

private void WriteChar(char character)
private void WriteChar(char c)
{
if (IsEndOfLine())
{
_text.Append(character);
Console2.Write(character.ToString());
_text.Append(c);
Console2.Write(c.ToString());
_cursorPos++;
}
else
{
int left = Console2.CursorLeft;
int top = Console2.CursorTop;
string str = _text.ToString().Substring(_cursorPos);
_text.Insert(_cursorPos, character);
Console2.Write(character.ToString() + str);
_text.Insert(_cursorPos, c);
Console2.Write(c.ToString() + str);
Console2.SetCursorPosition(left, top);
MoveCursorRight();
}
Expand All @@ -119,18 +119,18 @@ 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();
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--;
}

private void StartAutoComplete()
Expand Down Expand Up @@ -265,7 +265,7 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri

_completions = autoCompleteHandler.Invoke(text, _completionStart);
_completions = _completions?.Length == 0 ? null : _completions;

if (_completions == null)
return;

Expand Down
11 changes: 4 additions & 7 deletions src/ReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public static class ReadLine
private static KeyHandler _keyHandler;
private static List<string> _history;

public static Func<string, int, string[]> AutoCompletionHandler { private get; set; }

static ReadLine()
{
_history = new List<string>();
Expand All @@ -20,12 +18,14 @@ static ReadLine()
public static void AddHistory(params string[] text) => _history.AddRange(text);
public static List<string> GetHistory() => _history;
public static void ClearHistory() => _history = new List<string>();
public static Func<string, int, string[]> 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(), _history, AutoCompletionHandler);
_keyHandler = new KeyHandler(new Console2() { PasswordMode = PasswordMode }, _history, AutoCompletionHandler);
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

while (keyInfo.Key != ConsoleKey.Enter)
Expand All @@ -35,15 +35,12 @@ public static string Read(string prompt = "", string defaultInput = "")
}

Console.WriteLine();

string text = _keyHandler.Text;
if (String.IsNullOrWhiteSpace(text) && !String.IsNullOrWhiteSpace(defaultInput))
{
text = defaultInput;
}
else
{
_history.Add(text);
}

return text;
}
Expand Down

0 comments on commit 8375cd3

Please sign in to comment.