When you have operations with measurable progress, use AnsiConsole.Progress().
Caution
Progress bars are not thread safe. Using them together with other interactive components such as prompts, status displays, or other progress displays is not supported.
Create a Progress Bar
To show progress, call ctx.AddTask() and update with task.Increment().
AnsiConsole.Progress()
.Start(ctx =>
{
var task = ctx.AddTask("Processing files", maxValue: 125);
while (!ctx.IsFinished)
{
task.Increment(1);
Thread.Sleep(50);
}
});
Track Multiple Tasks
To track several operations at once, add multiple tasks to the context.
AnsiConsole.Progress()
.Start(ctx =>
{
var download = ctx.AddTask("Downloading", maxValue: 125);
var extract = ctx.AddTask("Extracting", maxValue: 150);
var install = ctx.AddTask("Installing"); // maxValue defaults to 100
while (!ctx.IsFinished)
{
download.Increment(1.5);
extract.Increment(0.8);
install.Increment(1.2);
Thread.Sleep(50);
}
});
Customize Columns
If you want different columns, use .Columns() to configure the display.
AnsiConsole.Progress()
.Columns(
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new SpinnerColumn())
.Start(ctx =>
{
var task = ctx.AddTask("Building project");
while (!ctx.IsFinished)
{
task.Increment(1);
Thread.Sleep(50);
}
});
Style the Progress Bar
To change colors, set styles on ProgressBarColumn.
AnsiConsole.Progress()
.Columns(
new TaskDescriptionColumn(),
new ProgressBarColumn
{
CompletedStyle = new Style(Color.Green),
RemainingStyle = new Style(Color.Grey)
},
new PercentageColumn())
.Start(ctx =>
{
var task = ctx.AddTask("Compiling");
while (!ctx.IsFinished)
{
task.Increment(1);
Thread.Sleep(40);
}
});
See Also
- Progress Display - Full Progress API reference
- Spinner Styles Reference - All spinner animations