Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectionPrompt - Set default value not available #508

Open
vanheesk opened this issue Aug 9, 2021 · 14 comments · May be fixed by #1541
Open

SelectionPrompt - Set default value not available #508

vanheesk opened this issue Aug 9, 2021 · 14 comments · May be fixed by #1541
Labels
feature ⭐ top feature Top feature request. ⭐ top issue Top issue.

Comments

@vanheesk
Copy link

vanheesk commented Aug 9, 2021

Is your feature request related to a problem? Please describe.
SelectionPrompt does not contain a "Select" -method. It is however available on MultiSelectionPrompt.

Describe the solution you'd like
Option 1:
Add the same "Select" and "IsSelected" -methods (as available in MultiSelectionPromptExtensions) to the SelectionPromptExtensions -class.

Option 2:
Add a "DefaultValue" (as available in TextPrompt) to the SelectionPromptExtensions -class.

Describe alternatives you've considered
I'm currently planning to work around this by setting the selected item as the first one in the list of choices. But that might be confusing for users.


Please upvote 👍 this issue if you are interested in it.

@patriksvensson
Copy link
Contributor

@vanheesk SelectionPrompt does not have a default value, since that would mean that the selection prompt is never shown.
Could you explain a bit more in detail what your use case is? Maybe I'm misunderstanding something.

@vanheesk
Copy link
Author

vanheesk commented Aug 9, 2021

Say I have a SelectionPrompt asking "What is your favorite fruit", and I provide the user with a list of choices. (Apple, Banana, Cherry, Coconut, Dragonfruit). I select "Cherry" and store this somewhere.
Now, I'm asking the user that same question again and I want to put the cursor on his/her previously selected item, so they know what was previously selected, but can still change it.

@ancailliau
Copy link

I second this. I use MultiSelectionPrompt to ask the user for a list of permission assigned to a role. When the user is editing the role, I do want to have the already-assigned permissions checked by default.

What would be the best way to implement that? I can write the code and make a pull request.

@thoemmi
Copy link
Contributor

thoemmi commented Sep 29, 2021

You can pre-check items. However, you have to add the items one by one. In my code I use something like this:

var prompt = new MultiSelectionPrompt<MyItem>()
    .Title("Choose items:")
    .UseConverter(item => item.Name);
foreach (var item in myItems)
{
    if (IsChecked(item))
        prompt.AddChoices(item, r => r.Select());
    else
        prompt.AddChoices(item);
}

@vanheesk
Copy link
Author

vanheesk commented Sep 29, 2021

The original question was about SingleSelect specifically, which I solved / worked around using:

var singleSelect = new SelectionPrompt<string>();

var singleSelectOptions = options;
if (currentValue != null)
{
    singleSelectOptions = new Dictionary<string, string>();

    // Add the selected item first, then add the other options as provided
    singleSelectOptions.Add(currentValue, options[currentValue]);
    foreach (var entry in options)
    {
        if (entry.Key == currentValue)
            continue;

        singleSelectOptions.Add(entry.Key, entry.Value);
     }
}

singleSelect.AddChoices(singleSelectOptions.Values);

var selection = AnsiConsole.Prompt(singleSelect);
var optionId = options.FirstOrDefault(x => x.Value == selection).Key;

return new List<string> { optionId };

@Scordo
Copy link

Scordo commented Mar 24, 2022

I would like to see that feature, too. My usecase is:
I'm showing a "menu" usign the Single-Selection-Feature. When a user selects an entry some action is executed. After the execution the menu is shown again where last action executed is selected

@fritogotlayed
Copy link

I would also love the support of Select or DefaultValue on the SelectionPrompt. My use case involves a list of items (5 in my case) where a selection of a single item of the list is required as they are all mutually exclusive. Re-ordering the list is not an option for given the desire to have the options listed alphabetically.

The workaround for us is a bit ugly since we're also having to drop the previous option on the prompt and force the user to verify the select before pressing enter and again at the end of the workflow before changes are saved.

Just including a DefaultValue would help alleviate these issues.

For reference this is what we're having to do at the prompt (simplified)

// Omitted is some reflection iterating over custom attributes on a settings DTO style object.
var previous = (bool?)propertyInfo.GetValue(config);
var promptTitle = $"{attribute.QueryPrompt ?? "MISSING: QueryPrompt attribute value"} [green](Previously: {previous})[/]";

var prompt = new SelectionPrompt<string>()
                        .Title(promptTitle)
                        .AddChoices(attribute.QueryExclusiveOptions);

var newValue = AnsiConsole.Prompt(prompt);

propertyInfo.SetValue(config, newValue);

this.AnsiConsole.MarkupLine($"{prompt}: {newValue}");

Once all the settings have been re-confirmed a table is once again displayed with the options from before and after. We're highlighting the "after" options that differ from the originals in yellow and asking the users to verify before selecting yes to save. It's a less than ideal solution but given some users like to use the wizard vs set individual options having a way to default a single select list would be greatly impactful.

@adamfk
Copy link

adamfk commented Aug 5, 2023

First of all, thanks for a wonderful library! It's working really well.

Being able to set a selection default would be really helpful for my purposes too. I'm creating a tool that helps generate StateSmith state machine projects and I want to remember the user's choices from last time.

My current thinking for a workaround is to put it at the top with a "remembered" prefix or postfix.

image

image

@reduckted
Copy link

Are you accepting pull requests for this? I've prototyped a change, and it works well (plus it's a reasonably small change!). It adds a DefaultValue property and extension method to SelectionPrompt.

var prompt = new SelectionPrompt<string>()
        .Title("Select one")
        .AddChoices("First", "Second", "Third")
        .DefaultValue("Second");

AnsiConsole.Prompt(prompt);
Select one
              
  First   
> Second  
  Third   

@reduckted
Copy link

@patriksvensson Are you accepting pull requests for this?

@awaescher
Copy link

Dear @reduckted, why wouldn't you just submit it?

@reduckted
Copy link

@awaescher
Copy link

You are referring to this, right?

Once you get a nod from someone in the Spectre.Console Team, you can start on the feature.

Can we please have a nod, @patriksvensson @phil-scott-78?

@patriksvensson
Copy link
Contributor

nod

@reduckted reduckted linked a pull request May 9, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⭐ top feature Top feature request. ⭐ top issue Top issue.
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

9 participants