ConsoleSpinner 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ConsoleSpinner --version 1.1.0                
NuGet\Install-Package ConsoleSpinner -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConsoleSpinner" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConsoleSpinner --version 1.1.0                
#r "nuget: ConsoleSpinner, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ConsoleSpinner as a Cake Addin
#addin nuget:?package=ConsoleSpinner&version=1.1.0

// Install ConsoleSpinner as a Cake Tool
#tool nuget:?package=ConsoleSpinner&version=1.1.0                

ConsoleSpinner

This library is a simple console spinner for .NET Core Console applications. It can be used for synchronous blocks of code or async tasks.

[demo.gif]

Styles

The spinner can be displayed in the following styles: | Style | Description | | ----- | ----------- | | Lines | Displays a line spinner | | Dots | Displays a dot spinner | | Boxes| Displays a box spinner | | Arrows | Displays an arrow spinner | | VerticalBars | Displays a vertical bar spinner | | HorizontalBars | Displays a horizontal bar spinner | | Triangles | Displays a triangle spinner | | QuarterBalls | Displays a quarter ball spinner | | HalfBalls | Displays a half ball spinner | | Balloons | Displays a balloon spinner | | Arcs | Displays an arc spinner | | BouncingBalls | Displays a bouncing ball spinner | | Wave | Displays a wave spinner | | Braille | Displays a braille spinner |

Usage

The library adds a new helper function ConsoleEx.WriteSpinner(). This function returns an IDisposable that will display the spinner until it is disposed, or you can pass in a task and it will display the spinner until the task is complete.

This allows the spinner to be used in two ways, with long running synchronous code or with async tasks.

Usage with synchronous code

With synchronous code you can simply wrap the code in a using statement and the spinner will be displayed until the code block is complete.

using(_ = Console.WriteSpinner())
{
	// long running non-task based code 
}

Usage with async Tasks

With async tasks you simply pass the task into ConsoleEx.WriteSpinner() and it will animate the spinner until the task is complete.

var task = Task.Delay(delay);
lock (Console.Out)
{
    ConsoleEx.WriteSpinner(SpinnerStyle.Lines, task);
    Console.WriteLine();
}

Adding status text with spinner.

You can customize the spinner by passing in a CustomFrame function. The custom frame function takes in the current frame, and a boolean for whether the task is done or not. It returns a string that represents the frame.

Arguments are

  • frame - the current frame of the animation
  • done - a boolean indicating whether the task is done or not Return:
  • A formatted string representing the frame.

NOTE: The width of the output and position of the frame in the output needs to be consistent for the animation to look correct.

i = 10;
using (var _ = ConsoleEx.WriteSpinner(SpinnerStyle.Lines, customFrame: (frame, done) => $"{frame} Counter: {i} "))
{
    for (; i > 0; i--)
    {
        // simulate doing stuff...
        await Task.Delay(1000);
    }
}

This allows you to have the spinner act like a progress bar.

Animation Notes

The animation is asynchronously using Console.SetPosition to position the cursor to the location of the animation so that it can change it.

While there are active spinners you should put a lock(Console.Out) around any Console output API call, preventing any background animations from interacting with the current cursor position.

Example:

using (var _ = ConsoleEx.WriteSpinner())
{
    ...long running code..
    lock(Console.Out)
    {
        Console.Write(...);
    }
    ...long running code..
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 233 5/2/2023
2.0.0 149 5/2/2023
1.1.0 149 5/1/2023
1.0.0 161 5/1/2023