Repository navigation
Remove widget from console? #2198
Replies: 2 comments 1 reply
|
You can do part of this with For example, a layout shown as a live renderable can remove itself when the live context finishes: AnsiConsole.Live(layout)
.AutoClear(true)
.Start(ctx =>
{
// render/update the layout here
});
AutoClear(true) clears the live renderable when the context completes.
The catch is that SelectionPrompt and a live display cannot be active at the same time. Spectre.Console's interactive components are not designed to be nested together, so you can't keep the layout live, show a SelectionPrompt inside it, and then have both disappear afterwards.
If you only need the layout to disappear before the prompt appears, this works:
AnsiConsole.Live(layout)
.AutoClear(true)
.Start(ctx =>
{
ctx.Refresh();
// finish live rendering here
});
var choice = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Choose an option")
.AddChoices("One", "Two", "Three"));
If your goal is specifically:
show the layout
keep it visible while the selection prompt is active
remove both after the user selects something
then I don't think the built-in widgets support that directly.
For a simple application where clearing everything is acceptable, one workaround is:
AnsiConsole.Write(layout);
var choice = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Choose an option")
.AddChoices("One", "Two", "Three"));
AnsiConsole.Clear();
That clears the whole console though, not just those two renderables.
For more precise control, you'd probably need a custom IRenderable / input loop that handles both the layout and selection UI itself. |
|
If a temporary full-screen view fits your CLI, For example, with Spectre.Console 0.57.2: using Spectre.Console;
var console = AnsiConsole.Console;
var caps = console.Profile.Capabilities;
if (System.Console.IsInputRedirected || System.Console.IsOutputRedirected ||
!caps.Ansi || !caps.Interactive || !caps.AlternateBuffer ||
console.Profile.Width < 60 || console.Profile.Height < 18)
{
console.WriteLine("This view needs a supported interactive terminal, at least 60x18.");
return;
}
var widgets = new Grid()
.AddColumn(new GridColumn().Width(26))
.AddColumn(new GridColumn().Width(26));
widgets.AddRow(
new Panel("Project: demo\nMode: preview").Header("Context"),
new Panel("Items: 42\nReady to choose").Header("Summary"));
string? selected = null;
console.AlternateScreen(() =>
{
console.Write(widgets);
console.WriteLine();
selected = console.Prompt(new SelectionPrompt<string>()
.Title("Choose a destination")
.PageSize(3)
.AddChoices("Alpha", "Beta", "Gamma"));
});
console.WriteLine($"Selected: {selected}");The main screen is hidden while this view is open and restored afterward; this does not remove arbitrary inline renderables. Keep the widgets bounded so the prompt has room, and keep the terminal at the supported size while choosing. A default full-height There is a complete example with capability checks and two consecutive selections. Its scripted PTY/terminal checks observe both widgets and choices together, select Beta then Gamma, and verify that the original main-screen content and first result survive. It also checks smaller/unsupported terminals and redirected input/output without a clearing fallback. The native API is documented by the official example and stable implementation. |
Uh oh!
There was an error while loading. Please reload this page.
Hey, just started using Spectre.Console and I'm really impressed. Wondering if there's a way to remove a widget from the console output in the same way that a text prompt is removed after a selection is made?
Use case is that I want to show a layout with mutliple widgets before showing a selection prompt, and when the user has chosen, I want the layout and the prompt to both disappear. Is this possible?
Thanks in advance!
All reactions