Add text prompt support

This commit is contained in:
Patrik Svensson
2020-10-30 14:58:17 +01:00
committed by Patrik Svensson
parent 380c6aca45
commit 0d209d8f18
27 changed files with 1326 additions and 72 deletions

View File

@@ -0,0 +1,49 @@
using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="IAnsiConsole"/>.
/// </summary>
public static partial class AnsiConsoleExtensions
{
internal static string ReadLine(this IAnsiConsole console, Style? style, bool secret)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}
style ??= Style.Plain;
var result = string.Empty;
while (true)
{
var key = console.Input.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
return result;
}
if (key.Key == ConsoleKey.Backspace)
{
if (result.Length > 0)
{
result = result.Substring(0, result.Length - 1);
console.Write("\b \b");
}
continue;
}
result += key.KeyChar.ToString();
if (!char.IsControl(key.KeyChar))
{
console.Write(secret ? "*" : key.KeyChar.ToString(), style);
}
}
}
}
}