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

Extending Select Prompt to Support KeyValuePair #1753

Closed
jwsadler opened this issue Feb 5, 2025 · 2 comments
Closed

Extending Select Prompt to Support KeyValuePair #1753

jwsadler opened this issue Feb 5, 2025 · 2 comments

Comments

@jwsadler
Copy link

jwsadler commented Feb 5, 2025

Currently the select prompt only supports simple types, it would be great if there was a specialized version that allowed for a key and a description. So that when checking what was selected we can go by the key value rather than a description.

@jmigual
Copy link

jmigual commented Feb 13, 2025

Hey, maybe it's not exactly what you want to use but you can try using the UseConverter method: https://spectreconsole.net/api/spectre.console/selectionpromptextensions/c6320835 . Basically, given the key, you can decide how the text renders in the screen.

This is a small example in how I used it:

var keySelection = console.Prompt(
    new MultiSelectionPrompt<int>()
        .Title("Select the API keys to delete")
        .PageSize(10)
        .NotRequired()
        .AddChoices(account.APIKeys.Select((_, i) => i))
        .Select(account.SelectedAPIKeyIndex)
        .UseConverter(keyIndex =>
        {
            var key = account.APIKeys[keyIndex];
            return $"{key.Name}: {key.Key.Value}";
        })
);

@patriksvensson
Copy link
Contributor

Like @jmigual points out, the UseConverter is what you're supposed to use in this situation.

@github-project-automation github-project-automation bot moved this from Todo 🕑 to Done 🚀 in Spectre Console Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done 🚀
Development

No branches or pull requests

3 participants