wasSharpNET – Rev 27

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  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 WriteLine(this object data)
        {
            System.Console.WriteLine(data);
        }

        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 Write(this object data, ConsoleColor foreground)
        {
            var cFG = System.Console.ForegroundColor;
            System.Console.ForegroundColor = foreground;
            System.Console.Write(data);
            System.Console.ForegroundColor = cFG;
        }

        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);
                    System.Console.WriteLine(data);
                    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);
                    break;

                case ConsoleTextAlignment.TOP_LEFT:
                    WriteLine(data, foregroundColor);
                    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);
                    System.Console.Write(data);
                    break;

                case ConsoleTextAlignment.TOP_LEFT:
                    System.Console.Write(data);
                    break;

                default:
                    throw new NotImplementedException();
            }
        }

        public static void WriteLine(this IEnumerable<object> data, ConsoleTextAlignment alignment)
        {
            switch (alignment)
            {
                case ConsoleTextAlignment.TOP_CENTER:
                    var enumerable = data as IList<object> ?? data.ToList();
                    var textBlock = enumerable.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 enumerable)
                    {
                        System.Console.CursorLeft = padding;
                        WriteLine(line, System.Console.ForegroundColor);
                    }
                    break;

                case ConsoleTextAlignment.TOP_LEFT:
                    foreach (var line in data)
                        WriteLine(line, System.Console.ForegroundColor);
                    break;

                default:
                    throw new NotImplementedException();
            }
        }
    }
}

Generated by GNU Enscript 1.6.5.90.