Skip to content

Conversation

reduckted
Copy link

fixes #508

  • I have read the Contribution Guidelines
  • I have commented on the issue above and discussed the intended changes
  • A maintainer has signed off on the changes and the issue was assigned to me
  • All newly added code is adequately covered by tests
  • All existing tests are still running without errors
  • The documentation was modified to reflect the changes OR no documentation changes are required.

Changes

SelectionPrompt now has a DefaultValue property that can be used to set the item that should be initially selected.


Please upvote 👍 this pull request if you are interested in it.

@github-actions github-actions bot added the ⭐ top pull request Top pull request. label May 10, 2024
@timothyparez
Copy link

timothyparez commented Oct 2, 2024

This, for me, is the number one missing feature.
Often we presents the users with a large list of operations
until this PR they had to scroll through the list every time (because they often repeat commands).
Now we simply set the last command they executed as the default.

Just tested this and it works great:

var fruit = AnsiConsole.Prompt(
    new SelectionPrompt<string>()
        .Title("What's your [green]favorite fruit[/]?")
        .PageSize(10)```
+       .DefaultValue("Banana") // <--- Setting the default value
        .MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
        .AddChoices(new[] {
            "Apple", "Apricot", "Avocado", 
            "Banana", "Blackcurrant", "Blueberry",
            "Cherry", "Cloudberry", "Cocunut",
        }));

AnsiConsole.WriteLine($"I agree. {fruit} is tasty!");

@reduckted reduckted force-pushed the feature/508-selection-prompt-default branch from 1aef241 to e7cb663 Compare October 2, 2024 10:49
@thomas-optimove
Copy link

thomas-optimove commented Mar 21, 2025

Its not pretty but as a workaround (like if you need this feature now) you can order your default to the top and even use converter to highlight to user why it is not in the expected order (i.e. alphabetical or whatever)

var mode = AnsiConsole.Prompt(
    new SelectionPrompt<MyEnum>()
        .Title("Pick an enum")
        .AddChoices(Enum.GetValues<MyEnum>()
            // Make SomeValue default by having it first
            .OrderByDescending(e=>e == MyEnum.SomeValue)
            .ThenBy(e=>e))
        .UseConverter(mode =>mode == MyEnum.SomeValue ? mode.ToString() +" (Default)" : mode.ToString())
        .WrapAround(true));

@timothyparez
Copy link

This PR really works and should be merged, it's been nearly a year.
(But there's not much activity on this project anymore)

@ShalokShalom
Copy link

This could close one of the highest ranking issues on the priority board.

#508

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐ top pull request Top pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SelectionPrompt - Set default value not available
6 participants