Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 1.92 KB

File metadata and controls

46 lines (36 loc) · 1.92 KB

ConsoleInteractive Nuget

A C# library that allows you to write and read from the console simultaneously

Usage

See ConsoleInteractiveDemo for a sample, but the gist of it is

// Create a CancellationToken so we can cancel the reader later.
CancellationTokenSource cts = new CancellationTokenSource();

// Create a new Thread that will print for us.
Thread PrintingThread = new Thread(new ThreadStart(() => {
    ConsoleWriter.WriteLine("Hello World!");
    Thread.Sleep(5000);
    ConsoleWriter.WriteLine("Hello World after 5 seconds!");
}));

// Create a new Reader Thread. This thread will start listening for console input
ConsoleReader.BeginReadThread(cts.Token);

// Handle incoming messages from the user (Enter key pressed)
ConsoleReader.MessageReceived += (sender, s) => {
    // We got a cancellation command! Let's cancel the CancellationTokenSource.
    if (s.Equals("cancel"))
        cts.Cancel();
};

// Start the printing thread.
PrintingThread.Start();

For formatted output, use Minecraft's color and formatting code format.

    ConsoleWriter.WriteLineFormatted("§aText §cwith §bMixed §1C§2o§3l§4o§5r§6s§a!");

will show up as the following: image

Note

It is important that you use the ConsoleInteractive.ConsoleWriter and ConsoleInteractive.ConsoleReader classes only.

Mixing and matching with System.Console is not supported nor recommended.

Demo

Check out an asciicast demo here! asciicast