Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit eeac902

Browse files
committed
Added custom prompt handler (thanks walljm!)
Merged from tonerdo/readline#62
1 parent 66b1c56 commit eeac902

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ _Note: The `(prompt>)` is optional_
7272
string input = ReadLine.Read("(prompt)> ");
7373
```
7474

75+
#### Read input with default
76+
77+
```csharp
78+
string input = ReadLine.Read("(prompt)> ", "default");
79+
```
80+
81+
#### Read input with custom prompt handler
82+
83+
```csharp
84+
ReadLine.WritePrompt = (prompt) => Console.Write($">> {prompt}");
85+
string input = ReadLine.Read("(prompt)> ", "default");
86+
```
87+
7588
#### Read password
7689

7790
```csharp

src/ReadLine/ReadLine.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public static class ReadLine
4747
/// </summary>
4848
public static IAutoCompleteHandler AutoCompletionHandler { private get; set; }
4949

50+
/// <summary>
51+
/// The prompt writing handler.
52+
/// </summary>
53+
public static Action<string> WritePrompt { private get; set; } = (prompt) => Console.Write(prompt);
54+
5055
private static readonly List<string> _history = new List<string>();
5156

5257
/// <summary>
@@ -83,7 +88,7 @@ public static void SetHistory(List<string> history)
8388
public static string Read(string prompt = "", string defaultText = "")
8489
{
8590
// Prepare the prompt
86-
Console.Write(prompt);
91+
WritePrompt.Invoke(prompt);
8792
KeyHandler keyHandler = new KeyHandler(new ConsoleWrapper(), _history, AutoCompletionHandler);
8893

8994
// Get the written text
@@ -114,7 +119,7 @@ public static string Read(string prompt = "", string defaultText = "")
114119
public static string ReadPassword(string prompt = "", char mask = default)
115120
{
116121
// Prepare the prompt
117-
Console.Write(prompt);
122+
WritePrompt.Invoke(prompt);
118123
KeyHandler keyHandler = new KeyHandler(new ConsoleWrapper() { PasswordMode = true, PasswordMaskChar = mask }, null, null);
119124

120125
// Get the written text

test/ReadLine.Demo/Program.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,22 @@ public static void Main()
5050
Console.WriteLine(input);
5151

5252
// Enter the prompt with default
53-
string input2 = ReadLine.Read("(prompt2)> [def] ", "def");
54-
Console.WriteLine(input2);
53+
input = ReadLine.Read("(prompt2)> [def] ", "def");
54+
Console.WriteLine(input);
55+
56+
// Enter the prompt with custom prompt handler
57+
ReadLine.WritePrompt = (prompt) => Console.Write($">> {prompt}");
58+
input = ReadLine.Read("(prompt3)> ");
59+
Console.WriteLine(input);
60+
ReadLine.WritePrompt = (prompt) => Console.Write(prompt);
5561

5662
// Enter the masked prompt
57-
string input3 = ReadLine.ReadPassword("Enter Password> ");
58-
Console.WriteLine(input3);
63+
input = ReadLine.ReadPassword("Enter Password> ");
64+
Console.WriteLine(input);
5965

6066
// Enter the masked prompt with password mask
61-
string input4 = ReadLine.ReadPassword("Enter Password> ", '*');
62-
Console.WriteLine(input4);
67+
input = ReadLine.ReadPassword("Enter Password> ", '*');
68+
Console.WriteLine(input);
6369
}
6470
}
6571
}

0 commit comments

Comments
 (0)