namespace Unosquare.Swan
{
using System;
using System.Linq;
///
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
///
public static partial class Terminal
{
#region Helper Methods
///
/// Prints all characters in the current code page.
/// This is provided for debugging purposes only.
///
public static void PrintCurrentCodePage()
{
if (!IsConsolePresent) return;
lock (SyncLock)
{
$"Output Encoding: {OutputEncoding}".WriteLine();
for (byte byteValue = 0; byteValue < byte.MaxValue; byteValue++)
{
var charValue = OutputEncoding.GetChars(new[] { byteValue })[0];
switch (byteValue)
{
case 8: // Backspace
case 9: // Tab
case 10: // Line feed
case 13: // Carriage return
charValue = '.';
break;
}
$"{byteValue:000} {charValue} ".Write();
// 7 is a beep -- Console.Beep() also works
if (byteValue == 7) " ".Write();
if ((byteValue + 1) % 8 == 0)
WriteLine();
}
WriteLine();
}
}
#endregion
#region Write Methods
///
/// Writes a character a number of times, optionally adding a new line at the end.
///
/// The character code.
/// The color.
/// The count.
/// if set to true [new line].
/// The writer flags.
public static void Write(this byte charCode, ConsoleColor? color = null, int count = 1, bool newLine = false, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
lock (SyncLock)
{
var bytes = new byte[count];
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = charCode;
}
if (newLine)
{
var newLineBytes = OutputEncoding.GetBytes(Environment.NewLine);
bytes = bytes.Union(newLineBytes).ToArray();
}
var buffer = OutputEncoding.GetChars(bytes);
var context = new OutputContext
{
OutputColor = color ?? Settings.DefaultColor,
OutputText = buffer,
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
///
/// Writes the specified character in the default color.
///
/// The character code.
/// The color.
/// The writer flags.
public static void Write(this char charCode, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
lock (SyncLock)
{
var context = new OutputContext
{
OutputColor = color ?? Settings.DefaultColor,
OutputText = new[] { charCode },
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
///
/// Writes the specified text in the given color.
///
/// The text.
/// The color.
/// The writer flags.
public static void Write(this string text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
if (text == null) return;
lock (SyncLock)
{
var buffer = OutputEncoding.GetBytes(text);
var context = new OutputContext
{
OutputColor = color ?? Settings.DefaultColor,
OutputText = OutputEncoding.GetChars(buffer),
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
#endregion
#region WriteLine Methods
///
/// Writes a New Line Sequence to the standard output.
///
/// The writer flags.
public static void WriteLine(TerminalWriters writerFlags = TerminalWriters.StandardOutput)
=> Environment.NewLine.Write(Settings.DefaultColor, writerFlags);
///
/// Writes a line of text in the current console foreground color
/// to the standard output.
///
/// The text.
/// The color.
/// The writer flags.
public static void WriteLine(this string text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
=> Write($"{text ?? string.Empty}{Environment.NewLine}", color, writerFlags);
///
/// As opposed to WriteLine methods, it prepends a Carriage Return character to the text
/// so that the console moves the cursor one position up after the text has been written out.
///
/// The text.
/// The color.
/// The writer flags.
public static void OverwriteLine(this string text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
Write($"\r{text ?? string.Empty}", color, writerFlags);
Flush();
CursorLeft = 0;
}
#endregion
}
}