spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
9.1k stars 465 forks source link

Text prompt with editable default value #595

Open digitaldias opened 2 years ago

digitaldias commented 2 years ago

Console input is a chore, it'd be nice to be able to pre-fill the user's answer with a default value so that the user can just hit ENTER to proceed to the next question.

I would love the ability to have a prompt that takes a default value, and that makes that editable to the user, which works on PC, Mac, and Linux.


Please upvote :+1: this issue if you are interested in it.

patriksvensson commented 2 years ago

@digitaldias It would make sense to add something like that. Perhaps adding a property called InsertDefaultValue or similar to TextPrompt and ConfirmationPrompt?

@spectreconsole/maintainers What do you think?

nils-a commented 2 years ago

I like the idea. I think InitialValue would make more sense as a name. Something like:

new TextPrompt<string>("What's your [green]favorite fruit[/]?")
        .InvalidChoiceMessage("[red]That's not a valid fruit[/]")
        .InitialValue("Orange")
        .AddChoice("Apple")
        .AddChoice("Banana")
        .AddChoice("Orange"));

Should it be possible to set both, the editable default (whatever we call it) and the DefaultValue. I think they are technically not mutually exclusive: The user gets an initial value to edit, and if he removes that completely he'll end up with the default. But does that make sense? Should we disable setting both?

seiter commented 2 years ago

Like the suggestion, I wonder if it would provide a solution to an issue where validation logic is bypassed when when a DefaultValue is set.

In my case, I expected to be able to provide a default value but have the validation logic run against it, however, with the TextPrompt, validation is bypassed when using DefaultValue as shown in the TextPrompt code listed below:

if (DefaultValue != null)
{
    console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
    console.WriteLine();
    return DefaultValue.Value;
}

Love to see this issue move forward in a way that provides flexibility to use validation logic even when providing an initial (or default) value!