Welcome to Unified Command Terminal š
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)
{
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();
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!");
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.");
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.
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();
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));
await terminal.SetForegroundColorAsync(new SolidColorBrush(Colors.White));
Set the Success Color:
- Non-Asynchronous Example:
terminal.SetOnSuccessColor(new SolidColorBrush(Colors.Green));
await terminal.SetOnSuccessColorAsync(new SolidColorBrush(Colors.Green));
Set the Error Color:
- Non-Asynchronous Example:
terminal.SetOnErrorColor(new SolidColorBrush(Colors.Red));
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;
var progressBar = await terminal.ShowDeterminateProgressBarAsync();
progressBar.Value = 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);
progress.Report(i);
}
});
7. Sound Feedback
The Terminal control can play a beep sound to provide auditory feedback.
- Non-Asynchronous Example:
terminal.Beep();
await terminal.BeepAsync();
Full Example
Non-Asynchronous:
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);
progressBar.Value = i;
}
terminal.Clear();
terminal.WriteLine("Process completed successfully.");
terminal.Beep();
Asynchronous:
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);
progress.Report(i);
}
});
await terminal.ClearAsync();
await terminal.WriteLineAsync("Process completed successfully.");
await terminal.BeepAsync();
Author
š¤ Jonathan Newman
Show your support
Give a āļø if this project helped you!