diff --git a/README.md b/README.md index b414bda..b2d3633 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,19 @@ _Note: The `(prompt>)` is optional_ string input = ReadLine.Read("(prompt)> "); ``` +#### Read input with default + +```csharp +string input = ReadLine.Read("(prompt)> ", "default"); +``` + +#### Read input with custom prompt handler + +```csharp +ReadLine.WritePrompt = (prompt) => Console.Write($">> {prompt}"); +string input = ReadLine.Read("(prompt)> ", "default"); +``` + #### Read password ```csharp diff --git a/src/ReadLine/ReadLine.cs b/src/ReadLine/ReadLine.cs index e9b6334..592c8f8 100755 --- a/src/ReadLine/ReadLine.cs +++ b/src/ReadLine/ReadLine.cs @@ -47,6 +47,11 @@ public static class ReadLine /// public static IAutoCompleteHandler AutoCompletionHandler { private get; set; } + /// + /// The prompt writing handler. + /// + public static Action WritePrompt { private get; set; } = (prompt) => Console.Write(prompt); + private static readonly List _history = new List(); /// @@ -83,7 +88,7 @@ public static void SetHistory(List history) public static string Read(string prompt = "", string defaultText = "") { // Prepare the prompt - Console.Write(prompt); + WritePrompt.Invoke(prompt); KeyHandler keyHandler = new KeyHandler(new ConsoleWrapper(), _history, AutoCompletionHandler); // Get the written text @@ -114,7 +119,7 @@ public static string Read(string prompt = "", string defaultText = "") public static string ReadPassword(string prompt = "", char mask = default) { // Prepare the prompt - Console.Write(prompt); + WritePrompt.Invoke(prompt); KeyHandler keyHandler = new KeyHandler(new ConsoleWrapper() { PasswordMode = true, PasswordMaskChar = mask }, null, null); // Get the written text diff --git a/test/ReadLine.Demo/Program.cs b/test/ReadLine.Demo/Program.cs index 8906501..358e7e1 100644 --- a/test/ReadLine.Demo/Program.cs +++ b/test/ReadLine.Demo/Program.cs @@ -50,16 +50,22 @@ public static void Main() Console.WriteLine(input); // Enter the prompt with default - string input2 = ReadLine.Read("(prompt2)> [def] ", "def"); - Console.WriteLine(input2); + input = ReadLine.Read("(prompt2)> [def] ", "def"); + Console.WriteLine(input); + + // Enter the prompt with custom prompt handler + ReadLine.WritePrompt = (prompt) => Console.Write($">> {prompt}"); + input = ReadLine.Read("(prompt3)> "); + Console.WriteLine(input); + ReadLine.WritePrompt = (prompt) => Console.Write(prompt); // Enter the masked prompt - string input3 = ReadLine.ReadPassword("Enter Password> "); - Console.WriteLine(input3); + input = ReadLine.ReadPassword("Enter Password> "); + Console.WriteLine(input); // Enter the masked prompt with password mask - string input4 = ReadLine.ReadPassword("Enter Password> ", '*'); - Console.WriteLine(input4); + input = ReadLine.ReadPassword("Enter Password> ", '*'); + Console.WriteLine(input); } } }