57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace Qrakhen.Qamp.Core.IO;
|
|
|
|
public static class Console
|
|
{
|
|
public static OutputStreamMessageHandler StdOut;
|
|
public static ErrorStreamMessageHandler StdErr;
|
|
public static InputStreamListenHandler StdIn;
|
|
public static MessageFormatter Formatter;
|
|
|
|
static Console()
|
|
{
|
|
StdOut = System.Console.Out.WriteLine;
|
|
StdErr = System.Console.Error.WriteLine;
|
|
StdIn = System.Console.In.ReadLine;
|
|
Formatter = (p) => {
|
|
if (p.Length == 0)
|
|
return [];
|
|
return p.Select(x => x.ToString()).ToArray() ?? [];
|
|
};
|
|
}
|
|
|
|
public static void Write(params object[] parameters)
|
|
{
|
|
if (StdOut == null)
|
|
return;
|
|
|
|
var lines = Formatter(parameters);
|
|
foreach (var line in lines)
|
|
StdOut(line);
|
|
}
|
|
|
|
public static string? Read()
|
|
{
|
|
if (StdIn == null)
|
|
throw new InvalidOperationException($"No stdin provided, can not read input stream.");
|
|
|
|
return StdIn();
|
|
}
|
|
|
|
public static void Error(params object[] parameters)
|
|
{
|
|
if (StdErr == null)
|
|
Write(parameters);
|
|
|
|
var lines = Formatter(parameters);
|
|
foreach (var line in lines)
|
|
StdErr(line);
|
|
}
|
|
}
|
|
|
|
public delegate string? InputStreamListenHandler();
|
|
public delegate void OutputStreamMessageHandler(string? message);
|
|
public delegate void ErrorStreamMessageHandler(string? message);
|
|
|
|
public delegate string?[] MessageFormatter(params object[] parameters); |