UnifiedCommand 1.0.5

dotnet add package UnifiedCommand --version 1.0.5
                    
NuGet\Install-Package UnifiedCommand -Version 1.0.5
                    
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="UnifiedCommand" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UnifiedCommand" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="UnifiedCommand" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add UnifiedCommand --version 1.0.5
                    
#r "nuget: UnifiedCommand, 1.0.5"
                    
#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.
#addin nuget:?package=UnifiedCommand&version=1.0.5
                    
Install UnifiedCommand as a Cake Addin
#tool nuget:?package=UnifiedCommand&version=1.0.5
                    
Install UnifiedCommand as a Cake Tool

<h1 align="center">Welcome to Unified Command Terminal 👋</h1> <p> <img alt="Version" src="https://img.shields.io/badge/version-1.0.5-blue.svg?cacheSeconds=2592000" /> </p>

Unified Command is a custom WPF terminal control designed to simulate a console-like interface within your WPF applications. It supports various features such as writing colored text, showing progress bars, and reading input asynchronously, all while maintaining a consistent and customizable UI.

Features

  • Console-like Interface: Simulates a command-line environment within your WPF application.
  • Text Coloring: Write colored text to indicate success, error, or other custom statuses.
  • Progress Bars: Display both indeterminate and determinate progress bars directly within the terminal.
  • Asynchronous Input: Read user input asynchronously while continuing to update the UI.
  • Customizable: Easily customize colors and styles to match your application's theme.
  • Sound Feedback: Play beep sounds to provide auditory feedback.

Installation

To use Unified Command in your WPF application, you can install it via NuGet:

dotnet add package UnifiedCommand --version 1.0.3

Exception Handling

The Terminal control may throw exceptions in certain scenarios. It's recommended to wrap initialization and usage of the Terminal control in try-catch blocks to handle these exceptions gracefully.

  • Non-Asynchronous Example:
try
{
    var terminal = new Terminal();
    terminal.Restart();
}
catch (Exception ex)
{
    // Handle other exceptions
    Console.WriteLine("An error occurred: " + ex.Message);
}

How To Use

Overview

The Terminal control is a custom WPF user control designed to simulate a terminal or command-line interface within your application. It allows you to write and read text, display progress bars, and handle text with different colors to indicate success or error states.

How to Use the Terminal Control

1. Initialization

You can initialize the Terminal control in your XAML or code-behind.

Example in XAML:

<local:Terminal x:Name="TerminalControl" />

Example Code-Behind:

  • Non-Asynchronous Example:
var terminal = Terminal.Create();
  • Asynchronous Example:
var terminal = await Terminal.CreateAsync();

2. Writing Text

The Terminal control provides several methods to write text to the terminal.

Write a Line of Text

Use the WriteLine method to write a line of text to the terminal. The text will automatically move to the next line.

  • Non-Asynchronous Example:
terminal.WriteLine("Hello, World!");
  • Asynchronous Example:
await terminal.WriteLineAsync("Hello, World!");
Write Text Without a New Line

Use the Write method to write text without moving to the next line.

  • Non-Asynchronous Example:
terminal.Write("This text will be ");

terminal.Write("on the same line.");
  • Asynchronous Example:
await terminal.WriteAsync("This text will be ");

await terminal.WriteAsync("on the same line.");
Write Text with Specific Colors

You can specify a color for the text using a SolidColorBrush.

  • Non-Asynchronous Example:
terminal.WriteLine("This is a success message.", new SolidColorBrush(Colors.Green));

terminal.WriteLine("This is an error message.", new SolidColorBrush(Colors.Red))
await terminal.WriteLineAsync("This is a success message.", new SolidColorBrush(Colors.Green));

await terminal.WriteLineAsync("This is an error message.", new SolidColorBrush(Colors.Red))

3. Reading Text

The Terminal control allows you to read user input asynchronously.

  • Asynchronous Example:
string userInput = await terminal.ReadLineAsync();

terminal.WriteLine($"You entered: {userInput}");

4. Clearing the Terminal

You can clear all content from the terminal using the Clear method.

  • Non-Asynchronous Example:
terminal.Clear();
  • Asynchronous Example:
await terminal.ClearAsync();

5. Setting Colors

You can customize the default colors for text, success messages, and error messages.

Set the Foreground Color:
  • Non-Asynchronous Example:
terminal.SetForegroundColor(new SolidColorBrush(Colors.White));
  • Asynchronous Example:
await terminal.SetForegroundColorAsync(new SolidColorBrush(Colors.White));
Set the Success Color:
  • Non-Asynchronous Example:
terminal.SetOnSuccessColor(new SolidColorBrush(Colors.Green));
  • Asynchronous Example:
await terminal.SetOnSuccessColorAsync(new SolidColorBrush(Colors.Green));
Set the Error Color:
  • Non-Asynchronous Example:
terminal.SetOnErrorColor(new SolidColorBrush(Colors.Red));
  • Asynchronous Example:
await terminal.SetOnErrorColorAsync(new SolidColorBrush(Colors.Red));

6. Displaying Progress Bars

The Terminal control supports displaying both indeterminate and determinate progress bars.

Indeterminate Progress Bar:

This type of progress bar continues to move, indicating that a process is ongoing without showing exact progress.

  • Non-Asynchronous Example:
terminal.ShowIndeterminateProgressBar();
Determinate Progress Bar:

This progress bar shows a specific percentage of completion.

  • Non-Asynchronous Example:
var progressBar = terminal.ShowDeterminateProgressBar();

progressBar.Value = 50; // Set progress to 50%
  • Asynchronous Example:
var progressBar = await terminal.ShowDeterminateProgressBarAsync();

progressBar.Value = 50; // Set progress to 50%
Running a Process with Progress Reporting:

You can run a process that reports progress to the terminal.

This method displays a determinate progress bar and allows you to track the progress of an asynchronous operation by passing a progress handler.

await terminal.RunWithProgressAsync(async progress =>
{
    for (int i = 0; i <= 100; i++)
    {
        await Task.Delay(50); // Simulate work
        progress.Report(i);    // Report progress
    }
});

7. Sound Feedback

The Terminal control can play a beep sound to provide auditory feedback.

  • Non-Asynchronous Example:
terminal.Beep();
  • Asynchronous Example:
await terminal.BeepAsync();

Full Example

Non-Asynchronous:

// Create the terminal
var terminal = Terminal.Create();
terminal.WriteLine("Starting process...");
var userName = await terminal.ReadLineAsync();
terminal.WriteSuccess($"Hello, {userName}!");
var progressBar = terminal.ShowDeterminateProgressBar();

for (int i = 0; i <= 100; i += 10)
{
    await Task.Delay(500); // Simulate work
    progressBar.Value = i; // Report progress
}

// Clear the terminal
terminal.Clear();
terminal.WriteLine("Process completed successfully.");
terminal.Beep(); // Play a beep sound

Asynchronous:

// Create the terminal asynchronously
var terminal = await Terminal.CreateAsync();
await terminal.WriteLineAsync("Starting process...");
var userName = await terminal.ReadLineAsync();
await terminal.WriteSuccessAsync($"Hello, {userName}!");

await terminal.RunWithProgressAsync(async progress =>
{
    for (int i = 0; i <= 100; i++)
    {
        await Task.Delay(50); // Simulate work
        progress.Report(i);    // Report progress
    }
});

// Clear the terminal asynchronously
await terminal.ClearAsync();
await terminal.WriteLineAsync("Process completed successfully.");
await terminal.BeepAsync(); // Play a beep sound asynchronously

Author

👤 Jonathan Newman

Show your support

Give a ⭐️ if this project helped you!

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.5 158 9/17/2024
1.0.4 116 9/17/2024