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

Commit

Permalink
Added custom prompt handler (thanks walljm!)
Browse files Browse the repository at this point in the history
  • Loading branch information
AptiviCEO committed May 15, 2022
1 parent 66b1c56 commit eeac902
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/ReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public static class ReadLine
/// </summary>
public static IAutoCompleteHandler AutoCompletionHandler { private get; set; }

/// <summary>
/// The prompt writing handler.
/// </summary>
public static Action<string> WritePrompt { private get; set; } = (prompt) => Console.Write(prompt);

private static readonly List<string> _history = new List<string>();

/// <summary>
Expand Down Expand Up @@ -83,7 +88,7 @@ public static void SetHistory(List<string> 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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions test/ReadLine.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit eeac902

Please sign in to comment.