wasSharpNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 8  →  ?path2? @ 9
/Console/ConsoleExtensions.cs
@@ -0,0 +1,112 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace wasSharpNET.Console
{
public static class ConsoleExtensions
{
public enum ConsoleTextAlignment
{
TOP_LEFT,
TOP_CENTER,
TOP_RIGHT
}
 
 
public static void WriteLine(this object data, ConsoleColor foreground,
ConsoleColor background)
{
var cFG = System.Console.ForegroundColor;
var cBG = System.Console.BackgroundColor;
System.Console.ForegroundColor = foreground;
System.Console.BackgroundColor = background;
System.Console.WriteLine(data);
System.Console.ForegroundColor = cFG;
System.Console.BackgroundColor = cBG;
}
 
public static void WriteLine(this object data, ConsoleColor foreground)
{
var cFG = System.Console.ForegroundColor;
System.Console.ForegroundColor = foreground;
System.Console.WriteLine(data);
System.Console.ForegroundColor = cFG;
}
 
public static void Write(this object data, ConsoleColor foreground,
ConsoleColor background)
{
var cFG = System.Console.ForegroundColor;
var cBG = System.Console.BackgroundColor;
System.Console.ForegroundColor = foreground;
System.Console.BackgroundColor = background;
System.Console.Write(data);
System.Console.ForegroundColor = cFG;
System.Console.BackgroundColor = cBG;
}
 
public static void WriteLine(this object data, ConsoleTextAlignment alignment)
{
switch (alignment)
{
case ConsoleTextAlignment.TOP_CENTER:
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0);
WriteLine(data, System.Console.ForegroundColor, System.Console.BackgroundColor);
break;
default:
throw new NotImplementedException();
}
}
 
public static void WriteLine(this object data, ConsoleTextAlignment alignment, ConsoleColor foregroundColor)
{
switch (alignment)
{
case ConsoleTextAlignment.TOP_CENTER:
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth / 2 - data.ToString().Length / 2, 0);
WriteLine(data, foregroundColor, System.Console.BackgroundColor);
break;
default:
throw new NotImplementedException();
}
}
 
public static void Write(this object data, ConsoleTextAlignment alignment)
{
switch (alignment)
{
case ConsoleTextAlignment.TOP_CENTER:
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0);
Write(data, System.Console.ForegroundColor, System.Console.BackgroundColor);
break;
default:
throw new NotImplementedException();
}
}
 
public static void WriteLine(this IEnumerable<object> data, ConsoleTextAlignment alignment)
{
switch (alignment)
{
case ConsoleTextAlignment.TOP_CENTER:
var textBlock = data.Select(o => o.ToString()).ToArray();
var padding = Math.Max(System.Console.WindowWidth/2 - textBlock.Select(o => o.Length).Max()/2, 0);
foreach (var line in data)
{
System.Console.CursorLeft = padding;
WriteLine(line, System.Console.ForegroundColor, System.Console.BackgroundColor);
}
break;
default:
throw new NotImplementedException();
}
}
}
}