How to return to 'main menu' after multi-level SelectionPrompt #1007
-
I'm trying to create a multi-level menu with Spectre.Console that returns to the 'main menu' after completion
When I go into Option 1, I get the 'sub menu'
I'm trying to get it to go back to the main menu if I select My code so far (feels quite clumsy with these nested switch statements)
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This has come up before in another issue. There isn't any built in support for this, and I think people have been looking for work arounds to get the selection prompt to kinda work. I think if I had the need I'd look at the source of the selection prompt as the basis of a new control designed specifically to do this rather than trying to get the selection prompt to do something it wasn't designed for. Lots more features could be baked in for a consistent API to be reused too |
Beta Was this translation helpful? Give feedback.
-
Resurrecting this thread since this is where my search brought me. I created multiple different "Menu" classes with .Show methods on them. Each one looks basically like this. internal class MainMenu
{
public static int? Show()
{
int? result = null;
while (result == null)
{
AnsiConsole.Clear();
AnsiConsole.Write(new FigletText("Main Menu").Centered());
AnsiConsole.Write(new Rule());
var prompt = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Choose a function")
.AddChoices("Foo", "Bar", "Exit"));
switch (prompt)
{
case "Foo":
result = FooMenu.Show();
break;
case "Bar":
result = BarMenu.Show();
break;
default:
result = 0;
break;
}
}
return result;
}
} Each sub-menu has a "Return" option, which returns null from the method, and an "Exit" option, which returns 0. So, if I go to the "Bar" menu, and then select "Return", tl;dr - Put your menu in a loop and call some separate sub-menu method in the same class or some other. Look for meaningful return values and loop the menu until you see them. |
Beta Was this translation helpful? Give feedback.
This has come up before in another issue. There isn't any built in support for this, and I think people have been looking for work arounds to get the selection prompt to kinda work.
I think if I had the need I'd look at the source of the selection prompt as the basis of a new control designed specifically to do this rather than trying to get the selection prompt to do something it wasn't designed for.
Lots more features could be baked in for a consistent API to be reused too