Skip to content

Allow to add handlers for prompts #230

@angelobreuer

Description

@angelobreuer

Is your feature request related to a problem? Please describe.
Allow users to add hooks to prompts.

Describe the solution you'd like
For example, adding the support of doing the following:

using System.Collections.Generic;
using System.Linq;
using Spectre.Console;

void Update(IReadOnlyList<int> items, PromptAction action)
{
    var bits = items.Aggregate(seed: 0, (a, b) => a | b);

    AnsiConsole.Cursor.SetPosition(20, 1);
    AnsiConsole.WriteLine($"Bits set: {bits:X8}");
}

var prompt = new MultiSelectionPrompt<int>()
    .Handler((list, action) => Update(list));

internal enum PromptAction : byte
{
    ItemSelected,
    ItemDeselected,

    // ..
}

Why is this needed?
I think adding hooks would be helpful to inform the user about what is set.

This would also add the ability to make something like the following:

using System;
using System.Collections.Generic;
using Spectre.Console;

void Update(IReadOnlyList<Item> items, PromptAction action)
{
    if (action is PromptAction.ItemSelected)
    {
        AnsiConsole.Cursor.SetPosition(20, 1);
        AnsiConsole.WriteLine(items[items.Count - 1].Description);
    }
}

var prompt = new SelectionPrompt<int>()
    .Handler((list, action) => Update(list));

internal enum PromptAction : byte
{
    ItemSelected,
    ItemDeselected,

    // ..
}

internal class Item
{
    public Item(string name, string description)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
        Description = description ?? throw new ArgumentNullException(nameof(description));
    }

    public string Name { get; }

    public string Description { get; }
}

Describe alternatives you've considered
---

Additional context
I would be able to grab this if this is considered a feature.


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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions