diff --git a/README.md b/README.md index 5a91e61..c455942 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ReadLine/Abstractions/Console2.cs b/src/ReadLine/Abstractions/Console2.cs index 6dd292b..8536b8b 100644 --- a/src/ReadLine/Abstractions/Console2.cs +++ b/src/ReadLine/Abstractions/Console2.cs @@ -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); } diff --git a/src/ReadLine/KeyHandler.cs b/src/ReadLine/KeyHandler.cs index b839382..539f810 100644 --- a/src/ReadLine/KeyHandler.cs +++ b/src/ReadLine/KeyHandler.cs @@ -95,12 +95,12 @@ 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 @@ -108,8 +108,8 @@ private void WriteChar(char character) 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(); } @@ -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() @@ -265,7 +265,7 @@ public KeyHandler(IConsole console, List history, Func _history; - public static Func AutoCompletionHandler { private get; set; } - static ReadLine() { _history = new List(); @@ -20,12 +18,14 @@ static ReadLine() 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(), _history, AutoCompletionHandler); + _keyHandler = new KeyHandler(new Console2() { PasswordMode = PasswordMode }, _history, AutoCompletionHandler); ConsoleKeyInfo keyInfo = Console.ReadKey(true); while (keyInfo.Key != ConsoleKey.Enter) @@ -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; }