Files
SabreTools/SabreTools.Library/Data/Build.cs

63 lines
2.3 KiB
C#
Raw Normal View History

using System;
2017-05-04 02:41:11 -07:00
namespace SabreTools.Library.Data
{
2019-02-08 20:32:49 -08:00
/// <summary>
/// Generic console preparation for program output
/// </summary>
public static class Build
{
/// <summary>
/// Returns true if running in a Mono environment
/// </summary>
public static bool MonoEnvironment
{
get { return (Type.GetType("Mono.Runtime") != null); }
}
2016-04-12 16:24:38 -07:00
2019-02-08 20:32:49 -08:00
/// <summary>
/// Readies the console and outputs the header
/// </summary>
/// <param name="name">The name to be displayed as the program</param>
2019-02-08 20:32:49 -08:00
public static void PrepareConsole(string name)
{
// 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)}+";
string mid = $"{name} {Constants.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
if (!Console.IsOutputRedirected)
{
// Set the console to ready state
ConsoleColor formertext = ConsoleColor.White;
ConsoleColor formerback = ConsoleColor.Black;
if (!MonoEnvironment)
{
Console.SetBufferSize(Console.BufferWidth, 999);
formertext = Console.ForegroundColor;
formerback = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
}
2016-04-20 11:50:03 -07:00
Console.Title = $"{name} {Constants.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
if (!MonoEnvironment)
{
Console.ForegroundColor = formertext;
Console.BackgroundColor = formerback;
}
}
}
}
}