When you need to update output without scrolling, use AnsiConsole.Live().
Caution
Live rendering is not thread safe. Using it together with other interactive components such as prompts, progress displays, or status displays is not supported.
Update in Place
To modify content and refresh the display, call ctx.Refresh().
var table = new Table().AddColumn("Metric").AddColumn("Value");
AnsiConsole.Live(table)
.Start(ctx =>
{
for (int i = 1; i <= 5; i++)
{
table.AddRow($"Item {i}", $"{i * 10}%");
ctx.Refresh();
Thread.Sleep(500);
}
});
Replace Content
To swap the entire display, use ctx.UpdateTarget().
AnsiConsole.Live(new Panel("Starting..."))
.Start(ctx =>
{
Thread.Sleep(1000);
ctx.UpdateTarget(new Panel("[yellow]Processing...[/]"));
Thread.Sleep(1000);
ctx.UpdateTarget(new Panel("[green]Complete![/]"));
Thread.Sleep(500);
});
Clear on Complete
If you want the display cleared after, use .AutoClear(true).
var table = new Table().AddColumn("Status");
AnsiConsole.Live(table)
.AutoClear(true)
.Start(ctx =>
{
table.AddRow("Working...");
ctx.Refresh();
Thread.Sleep(2000);
});
AnsiConsole.MarkupLine("[green]Done![/]");
Use Async
To use with async operations, call .StartAsync().
var table = new Table().AddColumn("Task").AddColumn("Status");
await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
table.AddRow("Fetching data", "[yellow]...[/]");
ctx.Refresh();
await Task.Delay(1000);
table.Rows.Update(0, 1, new Markup("[green]Done[/]"));
ctx.Refresh();
});
See Also
- Live Display - Full Live API reference