Files
SabreTools/SabreTools.Core/Prepare.cs

48 lines
1.8 KiB
C#
Raw Normal View History

using System;
2020-12-08 13:23:59 -08:00
namespace SabreTools.Core
{
2019-02-08 20:32:49 -08:00
/// <summary>
/// Generic console preparation for program output
/// </summary>
2020-08-01 23:10:23 -07:00
public static class Prepare
2019-02-08 20:32:49 -08:00
{
/// <summary>
/// Readies the console and outputs the header
/// </summary>
2020-08-01 23:10:23 -07:00
/// <param name="program">The name to be displayed as the program</param>
public static void SetConsoleHeader(string program)
2019-02-08 20:32:49 -08:00
{
// Dynamically create the header string, adapted from http://stackoverflow.com/questions/8200661/how-to-align-string-in-fixed-length-string
int width = Console.WindowWidth - 3;
string border = $"+{new string('-', width)}+";
2024-03-13 00:06:17 -04:00
string mid = $"{program} {Globals.Version}";
mid = $"|{mid.PadLeft(((width - mid.Length) / 2) + mid.Length).PadRight(width)}|";
2016-04-18 20:04:38 -07:00
2019-02-08 20:32:49 -08:00
// If we're outputting to console, do fancy things
2024-02-28 19:49:09 -05:00
#if NET452_OR_GREATER || NETCOREAPP
2019-02-08 20:32:49 -08:00
if (!Console.IsOutputRedirected)
{
// Set the console to ready state
2020-12-14 16:24:04 -08:00
ConsoleColor formertext = Console.ForegroundColor;
ConsoleColor formerback = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
2016-04-20 11:50:03 -07:00
2024-03-13 00:06:17 -04:00
Console.Title = $"{program} {Globals.Version}";
2016-04-18 20:04:38 -07:00
2019-02-08 20:32:49 -08:00
// Output the header
Console.WriteLine(border);
Console.WriteLine(mid);
Console.WriteLine(border);
Console.WriteLine();
2016-04-18 20:04:38 -07:00
2019-02-08 20:32:49 -08:00
// Return the console to the original text and background colors
2020-12-14 16:24:04 -08:00
Console.ForegroundColor = formertext;
Console.BackgroundColor = formerback;
2020-08-01 23:10:23 -07:00
}
2024-02-28 19:49:09 -05:00
#endif
2020-08-01 23:10:23 -07:00
}
2019-02-08 20:32:49 -08:00
}
}