Skip to content

Commit

Permalink
Merge pull request #2891 from Guillermo-Santos/console
Browse files Browse the repository at this point in the history
Update Console Plugs
  • Loading branch information
zarlo authored Jan 16, 2024
2 parents d39911c + 2531bfc commit 464a9d4
Show file tree
Hide file tree
Showing 14 changed files with 1,086 additions and 513 deletions.
131 changes: 131 additions & 0 deletions Tests/Kernels/ConsoleTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using Cosmos.System.ExtendedASCII;
using Cosmos.System.ScanMaps;
using System.IO;

/*
* Please note this is an atypical TestRunner:
Expand Down Expand Up @@ -32,6 +33,8 @@ protected override void Run()
{
try
{
TestStandardInOutError();

TestConsoleEncoding();

TestVGAResolutions();
Expand Down Expand Up @@ -134,6 +137,8 @@ public void TestConsoleEncoding()
Console.WriteLine("Press any key to terminate this test...");

Console.ReadKey();


}

public void TestVGAResolutions()
Expand Down Expand Up @@ -167,5 +172,131 @@ public void TestVariousConsoleFunctions()
var cursor = Console.GetCursorPosition();
Console.SetCursorPosition(cursor.Left, cursor.Top - 1);
}

public void TestStandardInOutError()
{
TestInstantiation();
TestRedirection();

Console.WriteLine("Let's put the StdOutput on a variable.");
Console.WriteLine("Hello World from Console!");
var stdout = Console.Out;
stdout.WriteLine("Hello World from variable!");

Console.Error.WriteLine("Hellow World from Console.Error!");
var stderr = Console.Error;
Console.WriteLine("Now the StdError");
stderr.WriteLine("Hellow World from stderr variable!");

Console.WriteLine("Now let's test reading.");
var line = Console.ReadLine();
Console.WriteLine($"You wrote {line}");
var stdin = Console.In;

Console.Write("Type Something Again: ");
line = stdin.ReadLine();
Console.WriteLine($"You wrote {line}");
}

public void TestInstantiation()
{
TextWriter stdout, stderror;
TextReader stdin;
stdout = new StreamWriter(
stream: Console.OpenStandardOutput(),
encoding: Console.OutputEncoding)
{ AutoFlush = true };
stderror = new StreamWriter(
stream: Console.OpenStandardError(),
encoding: Console.OutputEncoding)
{ AutoFlush = true };

stdin = new StreamReader(
stream: Console.OpenStandardInput(),
encoding: Console.InputEncoding
);

stdout.WriteLine("Hellow this test is to show that you can make standalone reference to StdIn/Out/Error");
stderror.WriteLine("This two messages were writed from a standalone StdOut");
stderror.WriteLine("This is from StdError");

stdout.WriteLine("Now let's try reading from a standalone StdIn variable");
string line = stdin.ReadLine();
stdout.WriteLine($"You wrote {line}");
}
public void TestRedirection()
{
TextWriter redStdOut, redStdError;
TextReader redStdIn;
string expectedValue = "This is a redirected string";

Assert.IsFalse(Console.IsInputRedirected, "Standard Input was wrongly detected as redirected");
Assert.IsFalse(Console.IsErrorRedirected, "Standard Error was wrongly detected as redirected");
Assert.IsFalse(Console.IsOutputRedirected, "Standard Output was wrongly detected as redirected");

redStdOut = new StringWriter();
redStdError = StreamWriter.Null; // Empty Stream Writer.
redStdIn = new StringReader(expectedValue);

Console.SetIn(redStdIn);
Console.SetOut(redStdOut);
Console.SetError(redStdError);

Assert.IsTrue(Console.IsInputRedirected, "Standard Input was wrongly detected as NOT redirected");
Assert.IsTrue(Console.IsErrorRedirected, "Standard Error was wrongly detected as NOT redirected");
Assert.IsTrue(Console.IsOutputRedirected, "Standard Output was wrongly detected as NOT redirected");

// Now let's test that we can use the redirected versions.
Console.WriteLine("Hello world to redirected Out!");
Console.Error.WriteLine("Hellow world to redirected Error!");

string line = Console.ReadLine();

Assert.IsTrue(line == expectedValue, "Console.ReadLine brought unexpected Result.");

// Now Let's return to the true values;

// For StdOut and StdIn you can make a StreamWriter on your own or use the method on the System.Cosmos.Global.Console public field;
Console.SetOut(new StreamWriter(
stream: Console.OpenStandardOutput(),
encoding: Console.OutputEncoding)
{ AutoFlush = true } // if you want the content to be writed inmediatly on the console, then AutoFlush should be true.
);
Assert.IsFalse(Console.IsOutputRedirected, "Standard Output was wrongly detected as redirected");
Console.SetOut(
Sys.Global.Console.CreateOutputWriter(
Console.OpenStandardOutput())
);
Assert.IsFalse(Console.IsOutputRedirected, "Standard Output was wrongly detected as redirected");

// Now with Error
Console.SetError(new StreamWriter(
stream: Console.OpenStandardError(),
encoding: Console.OutputEncoding)
{ AutoFlush = true } // if you want the content to be writed inmediatly on the console, then AutoFlush should be true.
);
Assert.IsFalse(Console.IsErrorRedirected, "Standard Error was wrongly detected as redirected");
Console.SetError(
Sys.Global.Console.CreateOutputWriter(
Console.OpenStandardError())
);
Assert.IsFalse(Console.IsErrorRedirected, "Standard Error was wrongly detected as redirected");

// Different to Out and Error StdIn is never overrided by the methods SetIn and SetOut
// , SetIn will be considered as redirected if you use StreamReader (even if the stream is the console Stream)
// This is because the ReadKey comes from the internal implementation.
Console.SetIn(new StreamReader(
stream: Console.OpenStandardInput(),
encoding: Console.InputEncoding
));
Assert.IsTrue(Console.IsInputRedirected, "Standard Input was wrongly detected as NOT redirected");

// If you want to get the orginal TextReader, you can do it by using this method.
Console.SetIn(Sys.Global.Console.GetOrCreateReader()); // Get or create the original Console Reader.
Assert.IsFalse(Console.IsInputRedirected, "Standard Input was wrongly detected as redirected");

// The field can actually be set to null, but it would be filled againg the next time that a read method from the console is called.
Sys.Global.Console.ResetInternalStdIn(); // This method is called when InputEncoding is changed.
}
}
}
162 changes: 0 additions & 162 deletions source/Cosmos.Core_Plugs/System/StringImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,168 +151,6 @@ public static bool IsAscii(string aThis)
return true;
}

public static string Format(string aFormat, object aArg0)
{
if (aArg0 == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(null, aFormat, aArg0);
}

public static string Format(string aFormat, object aArg0, object aArg1)
{
if (aFormat == null)
{
throw new ArgumentNullException(nameof(aFormat));
}
if (aArg0 == null)
{
throw new ArgumentNullException(nameof(aArg0));
}
if (aArg1 == null)
{
throw new ArgumentNullException(nameof(aArg1));
}

return FormatHelper(null, aFormat, aArg0, aArg1);
}

public static string Format(string aFormat, object aArg0, object aArg1, object aArg2)
{
if (aArg0 == null || aArg1 == null || aArg2 == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(null, aFormat, aArg0, aArg1, aArg2);
}

public static string Format(string aFormat, params object[] aArgs)
{
if (aArgs == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(null, aFormat, aArgs);
}

public static string Format(IFormatProvider aProvider, string aFormat, object aArg0)
{
if (aArg0 == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(aProvider, aFormat, aArg0);
}

public static string Format(IFormatProvider aProvider, string aFormat, object aArg0, object aArg1)
{
if (aArg0 == null | aArg1 == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(aProvider, aFormat, aArg0, aArg1);
}

public static string Format(IFormatProvider aProvider, string aFormat, object aArg0, object aArg1, object aArg2)
{
if (aArg0 == null | aArg1 == null || aArg2 == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(aProvider, aFormat, aArg0, aArg1, aArg2);
}

public static string Format(IFormatProvider aProvider, string aFormat, params object[] aArgs)
{
if (aArgs == null)
{
throw new ArgumentNullException(aFormat == null ? "aFormat" : "aArgs");
}

return FormatHelper(aProvider, aFormat, aArgs);
}

internal static string FormatHelper(IFormatProvider aFormatProvider, string aFormat, params object[] aArgs)
{
char[] xCharArray = aFormat.ToCharArray();
string xFormattedString = string.Empty, xStaticString;
bool xFoundPlaceholder = false, xParamNumberDone = true;
int xStartParamNumber = -1, xEndParamNumber = -1, xLastPlaceHolder = 0;

for (int i = 0; i < xCharArray.Length; i++)
{
if (xFoundPlaceholder)
{
if (xCharArray[i] == '{')
{
throw new FormatException("The format string provided is invalid.");
}
if (xCharArray[i] == '}')
{
mDebugger.SendInternal("Found closing placeholder.");
if (xEndParamNumber < 0)
{
xEndParamNumber = i;
}
string xParamNumber = aFormat.Substring(xStartParamNumber, xEndParamNumber - xStartParamNumber);
mDebugger.SendInternal("Calling StringHelper.GetStringToNumber");
mDebugger.SendInternal(xParamNumber);
int xParamIndex = StringHelper.GetStringToNumber(xParamNumber);
mDebugger.SendInternal("Converted paramindex to a number.");
if (xParamIndex < aArgs.Length && aArgs[xParamIndex] != null)
{
string xParamValue = aArgs[xParamIndex].ToString();
xFormattedString = string.Concat(xFormattedString, xParamValue);
mDebugger.SendInternal("xParamValue =");
mDebugger.SendInternal(xParamValue);
mDebugger.SendInternal("xFormattedString =");
mDebugger.SendInternal(xFormattedString);

}
xFoundPlaceholder = false;
xParamNumberDone = true;
xStartParamNumber = -1;
xEndParamNumber = -1;
xLastPlaceHolder = i + 1;
}
else if (xCharArray[i] == ':')
{
xParamNumberDone = true;
xEndParamNumber = i;
// TODO: Need to handle different formats. (X, N, etc)
}
else if (char.IsDigit(xCharArray[i]) && !xParamNumberDone)
{
mDebugger.SendInternal("Getting param number.");
if (xStartParamNumber < 0)
{
xStartParamNumber = i;
}
}
}
else if (xCharArray[i] == '{')
{
mDebugger.SendInternal("Found opening placeholder");
xStaticString = aFormat.Substring(xLastPlaceHolder, i - xLastPlaceHolder);
xFormattedString = string.Concat(xFormattedString, xStaticString);
xFoundPlaceholder = true;
xParamNumberDone = false;
}
}

xStaticString = aFormat.Substring(xLastPlaceHolder, aFormat.Length - xLastPlaceHolder);
xFormattedString = string.Concat(xFormattedString, xStaticString);

return xFormattedString;
}

public static bool StartsWith(string aThis, string aSubstring, StringComparison aComparison)
{
var di = aThis.AsSpan();
Expand Down
Loading

0 comments on commit 464a9d4

Please sign in to comment.