RaspberryIO_26/Swan.Tiny/Terminal.cs

208 lines
6.0 KiB
C#
Raw Permalink Normal View History

2019-12-09 17:25:54 +01:00
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using Swan.Threading;
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user.
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
#region Private Declarations
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private const Int32 OutputFlushInterval = 15;
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static readonly ExclusiveTimer DequeueOutputTimer;
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static readonly Object SyncLock = new Object();
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static readonly ConcurrentQueue<OutputContext> OutputQueue = new ConcurrentQueue<OutputContext>();
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static readonly ManualResetEventSlim OutputDone = new ManualResetEventSlim(false);
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static readonly ManualResetEventSlim InputDone = new ManualResetEventSlim(true);
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static Boolean? _isConsolePresent;
#endregion
#region Constructors
/// <summary>
/// Initializes static members of the <see cref="Terminal"/> class.
/// </summary>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
static Terminal() {
lock(SyncLock) {
if(DequeueOutputTimer != null) {
return;
}
if(IsConsolePresent) {
Console.CursorVisible = false;
}
// Here we start the output task, fire-and-forget
DequeueOutputTimer = new ExclusiveTimer(DequeueOutputCycle);
DequeueOutputTimer.Resume(OutputFlushInterval);
}
2019-12-10 20:20:45 +01:00
}
2019-12-09 17:25:54 +01:00
/// <summary>
/// Gets a value indicating whether the Console is present.
/// </summary>
/// <value>
/// <c>true</c> if this instance is console present; otherwise, <c>false</c>.
/// </value>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
public static Boolean IsConsolePresent {
get {
if(_isConsolePresent == null) {
_isConsolePresent = true;
try {
Int32 windowHeight = Console.WindowHeight;
_isConsolePresent = windowHeight >= 0;
} catch {
_isConsolePresent = false;
}
}
return _isConsolePresent.Value;
}
}
/// <summary>
/// Gets the available output writers in a bitwise mask.
/// </summary>
/// <value>
/// The available writers.
/// </value>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
public static TerminalWriters AvailableWriters => IsConsolePresent ? TerminalWriters.StandardError | TerminalWriters.StandardOutput : TerminalWriters.None;
/// <summary>
/// Gets or sets the output encoding for the current console.
/// </summary>
/// <value>
/// The output encoding.
/// </value>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
public static Encoding OutputEncoding {
get => Console.OutputEncoding;
set => Console.OutputEncoding = value;
}
#endregion
#region Methods
/// <summary>
/// Enqueues the output to be written to the console
/// This is the only method that should enqueue to the output
/// Please note that if AvailableWriters is None, then no output will be enqueued.
/// </summary>
/// <param name="context">The context.</param>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static void EnqueueOutput(OutputContext context) {
lock(SyncLock) {
TerminalWriters availableWriters = AvailableWriters;
if(availableWriters == TerminalWriters.None || context.OutputWriters == TerminalWriters.None) {
OutputDone.Set();
return;
}
if((context.OutputWriters & availableWriters) == TerminalWriters.None) {
return;
}
OutputDone.Reset();
OutputQueue.Enqueue(context);
}
}
/// <summary>
/// Runs a Terminal I/O cycle in the <see cref="ThreadPool"/> thread.
/// </summary>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private static void DequeueOutputCycle() {
if(AvailableWriters == TerminalWriters.None) {
OutputDone.Set();
return;
}
InputDone.Wait();
if(OutputQueue.Count <= 0) {
OutputDone.Set();
return;
}
OutputDone.Reset();
while(OutputQueue.Count > 0) {
if(!OutputQueue.TryDequeue(out OutputContext context)) {
continue;
}
// Process Console output and Skip over stuff we can't display so we don't stress the output too much.
if(!IsConsolePresent) {
continue;
}
Console.ForegroundColor = context.OutputColor;
// Output to the standard output
if(context.OutputWriters.HasFlag(TerminalWriters.StandardOutput)) {
Console.Out.Write(context.OutputText);
}
// output to the standard error
if(context.OutputWriters.HasFlag(TerminalWriters.StandardError)) {
Console.Error.Write(context.OutputText);
}
Console.ResetColor();
Console.ForegroundColor = context.OriginalColor;
}
}
#endregion
#region Output Context
/// <summary>
/// Represents an asynchronous output context.
/// </summary>
2019-12-10 20:01:19 +01:00
// [Obsolete("NEED", false)]
2019-12-09 17:25:54 +01:00
private sealed class OutputContext {
/// <summary>
/// Initializes a new instance of the <see cref="OutputContext"/> class.
/// </summary>
public OutputContext() {
this.OriginalColor = Settings.DefaultColor;
this.OutputWriters = IsConsolePresent ? TerminalWriters.StandardOutput : TerminalWriters.None;
}
public ConsoleColor OriginalColor {
get;
}
public ConsoleColor OutputColor {
get; set;
}
public Char[] OutputText {
get; set;
}
public TerminalWriters OutputWriters {
get; set;
}
}
#endregion
}
}