šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
DemoInstallSign in
Socket

UnifiedCommand

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

UnifiedCommand

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.

1.0.5
NuGet
Version published
Maintainers
1
Created
Source

Welcome to Unified Command Terminal šŸ‘‹

Version

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

  • Github: @Jonathan-Newman

Show your support

Give a ā­ļø if this project helped you!

Keywords

wpf

FAQs

Package last updated on 17 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts