Last active
December 23, 2017 20:20
-
-
Save jrgcubano/ab689cb53a5f28dbbb8547a5c772f5b8 to your computer and use it in GitHub Desktop.
build.cake ASCII Colored Logo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Start Colors.Net | |
/// paste code to because the original libray is using NetStandard 2.0 | |
/// https://github.com/ahmelsayed/Colors.Net | |
public static class StringStaticMethods | |
{ | |
public static string Black(string value) | |
{ | |
var color = Data.ConsoleColorToUnicode[ConsoleColor.Black]; | |
return $"{color}{value}{color}"; | |
} | |
internal static RichString ColorString(string value, ConsoleColor color) | |
{ | |
if (!string.IsNullOrEmpty(value)) | |
{ | |
var colorChar = Data.ConsoleColorToUnicode[color]; | |
if (value[0] >= '\uE000') | |
{ | |
value = value.Trim(value[0]); | |
} | |
return new RichString($"{colorChar}{value}{colorChar}"); | |
} | |
else | |
{ | |
return new RichString(string.Empty); | |
} | |
} | |
public static RichString DarkBlue(string value) => ColorString(value, ConsoleColor.DarkBlue); | |
public static RichString DarkGreen(string value) => ColorString(value, ConsoleColor.DarkGreen); | |
public static RichString DarkCyan(string value) => ColorString(value, ConsoleColor.DarkCyan); | |
public static RichString DarkRed(string value) => ColorString(value, ConsoleColor.DarkRed); | |
public static RichString DarkMagenta(string value) => ColorString(value, ConsoleColor.DarkMagenta); | |
public static RichString DarkYellow(string value) => ColorString(value, ConsoleColor.DarkYellow); | |
public static RichString Gray(string value) => ColorString(value, ConsoleColor.Gray); | |
public static RichString DarkGray(string value) => ColorString(value, ConsoleColor.DarkGray); | |
public static RichString Blue(string value) => ColorString(value, ConsoleColor.Blue); | |
public static RichString Green(string value) => ColorString(value, ConsoleColor.Green); | |
public static RichString Cyan(string value) => ColorString(value, ConsoleColor.Cyan); | |
public static RichString Red(string value) => ColorString(value, ConsoleColor.Red); | |
public static RichString Magenta(string value) => ColorString(value, ConsoleColor.Magenta); | |
public static RichString Yellow(string value) => ColorString(value, ConsoleColor.Yellow); | |
public static RichString White(string value) => ColorString(value, ConsoleColor.White); | |
} | |
public static class StringColorExtensions | |
{ | |
public static RichString Black(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Black); | |
public static RichString DarkBlue(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkBlue); | |
public static RichString DarkGreen(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkGreen); | |
public static RichString DarkCyan(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkCyan); | |
public static RichString DarkRed(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkRed); | |
public static RichString DarkMagenta(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkMagenta); | |
public static RichString DarkYellow(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkYellow); | |
public static RichString Gray(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Gray); | |
public static RichString DarkGray(string value) => StringStaticMethods.ColorString(value, ConsoleColor.DarkGray); | |
public static RichString Blue(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Blue); | |
public static RichString Green(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Green); | |
public static RichString Cyan(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Cyan); | |
public static RichString Red(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Red); | |
public static RichString Magenta(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Magenta); | |
public static RichString Yellow(string value) => StringStaticMethods.ColorString(value, ConsoleColor.Yellow); | |
public static RichString White(string value) => StringStaticMethods.ColorString(value, ConsoleColor.White); | |
} | |
public static class Data | |
{ | |
// Some characters from the private use range of unicode. | |
// https://en.wikipedia.org/wiki/Private_Use_Areas | |
public static IDictionary<ConsoleColor, char> ConsoleColorToUnicode = new Dictionary<ConsoleColor, char>() | |
{ | |
{ ConsoleColor.Black, '\uE000' }, | |
{ ConsoleColor.DarkBlue, '\uE001' }, | |
{ ConsoleColor.DarkGreen, '\uE002' }, | |
{ ConsoleColor.DarkCyan, '\uE003' }, | |
{ ConsoleColor.DarkRed, '\uE004' }, | |
{ ConsoleColor.DarkMagenta, '\uE005' }, | |
{ ConsoleColor.DarkYellow, '\uE006' }, | |
{ ConsoleColor.Gray, '\uE007' }, | |
{ ConsoleColor.DarkGray, '\uE008' }, | |
{ ConsoleColor.Blue, '\uE009' }, | |
{ ConsoleColor.Green, '\uE00A' }, | |
{ ConsoleColor.Cyan, '\uE00B' }, | |
{ ConsoleColor.Red, '\uE00C' }, | |
{ ConsoleColor.Magenta, '\uE00D' }, | |
{ ConsoleColor.Yellow, '\uE00E' }, | |
{ ConsoleColor.White, '\uE00F' } | |
}; | |
public static IDictionary<char, ConsoleColor> UnicodeToConsoleColor = ConsoleColorToUnicode.ToDictionary(k => k.Value, v => v.Key); | |
} | |
public interface IConsoleWriter | |
{ | |
IConsoleWriter Write(object value); | |
IConsoleWriter WriteLine(); | |
IConsoleWriter WriteLine(object value); | |
} | |
public static class ColoredConsole | |
{ | |
public static IConsoleWriter Write(object value) | |
{ | |
Out.Write(value); | |
return Out; | |
} | |
public static IConsoleWriter WriteLine() | |
{ | |
Out.WriteLine(); | |
return Out; | |
} | |
public static IConsoleWriter WriteLine(object value) | |
{ | |
Out.WriteLine(value); | |
return Out; | |
} | |
public static IConsoleWriter Out { get; set; } = new ColoredConsoleWriter(System.Console.Out); | |
public static IConsoleWriter Error { get; set; } = new ColoredConsoleWriter(System.Console.Error); | |
} | |
public struct RichString | |
{ | |
private readonly string _value; | |
public RichString(string value) | |
{ | |
_value = value; | |
} | |
public override int GetHashCode() | |
{ | |
return _value.GetHashCode(); | |
} | |
public override string ToString() | |
{ | |
return _value; | |
} | |
public string Value | |
{ | |
get | |
{ | |
return _value.Length > 0 && _value[0] > '\uE000' && _value[0] == _value[_value.Length - 1] | |
? _value.Trim(_value[0]) | |
: _value; | |
} | |
} | |
public override bool Equals(object obj) | |
{ | |
return ToString().Equals(obj); | |
} | |
public bool Equals(RichString other) | |
{ | |
return ToString().Equals(other.ToString()); | |
} | |
public static bool operator ==(RichString rs1, RichString rs2) | |
{ | |
return rs1.Equals(rs2); | |
} | |
public static bool operator !=(RichString rs1, RichString rs2) | |
{ | |
return !rs1.Equals(rs2); | |
} | |
public static bool operator ==(string str, RichString rs2) | |
{ | |
return str.Equals(rs2.ToString()); | |
} | |
public static bool operator !=(string str, RichString rs2) | |
{ | |
return !str.Equals(rs2.ToString()); | |
} | |
public static RichString operator +(RichString rs1, RichString rs2) | |
{ | |
return new RichString(rs1.ToString() + rs2.ToString()); | |
} | |
public static RichString operator +(string str, RichString rs2) | |
{ | |
return new RichString(str + rs2.ToString()); | |
} | |
} | |
public class ColoredConsoleWriter : IConsoleWriter | |
{ | |
private static readonly object _colorLock = new object(); | |
private static readonly object _writeLock = new object(); | |
private static readonly ConsoleColor OriginalConsoleColor = Console.ForegroundColor; | |
private static readonly Stack<ConsoleColor> _colorStack = new Stack<ConsoleColor>(new[] { OriginalConsoleColor }); | |
private readonly TextWriter _writer; | |
public ColoredConsoleWriter(TextWriter writer) | |
{ | |
_writer = writer; | |
} | |
public IConsoleWriter Write(object ovalue) | |
{ | |
lock (_writeLock) | |
{ | |
var value = ovalue?.ToString() ?? string.Empty; | |
var startIndex = 0; | |
var endIndex = 0; | |
for (var i = 0; i < value.Length; i++) | |
{ | |
ConsoleColor nextColor; | |
if (Data.UnicodeToConsoleColor.TryGetValue(value[i], out nextColor)) | |
{ | |
var currentColor = _colorStack.Count == 0 ? OriginalConsoleColor : _colorStack.Pop(); | |
WriteColor(value.Substring(startIndex, endIndex - startIndex), currentColor); | |
if (currentColor != nextColor) | |
{ | |
_colorStack.Push(currentColor); | |
_colorStack.Push(nextColor); | |
} | |
startIndex = i + 1; | |
endIndex = i + 1; | |
} | |
else | |
{ | |
endIndex++; | |
} | |
} | |
WriteColor(value.Substring(startIndex, endIndex - startIndex), OriginalConsoleColor); | |
return this; | |
} | |
} | |
private void WriteColor(string value, ConsoleColor color) | |
{ | |
lock(_colorLock) | |
{ | |
var originalColor = Console.ForegroundColor; | |
Console.ForegroundColor = color; | |
_writer.Write(value); | |
Console.ForegroundColor = originalColor; | |
} | |
} | |
public IConsoleWriter WriteLine() | |
{ | |
return Write(Environment.NewLine); | |
} | |
public IConsoleWriter WriteLine(object value) | |
{ | |
Write(value); | |
return Write(Environment.NewLine); | |
} | |
} | |
/// End Colors.Net | |
Task("Play") | |
.Does(() => | |
{ | |
ColoredConsole.WriteLine($@" | |
, | |
,, ,~, | |
,~~, ,~~~, | |
,~~, ,~~~~~, | |
,~~, ,~~~~~~~,, | |
,,~~~~~~~~~~~~~, ,, | |
,,~~~~,~,~,~~~~, ,, | |
,, ,,~~~~~~~~~~,, ,, | |
,, ,,~~~~~~,?? ,, | |
,, ,,~~~~~????????????,, | |
,,???????????????,~~~~~:??????????, | |
,?????????????????,~~~~~,?????????, | |
,????????????????,;~~~~~~,:::::::?, | |
,??????????:::::::,;~~~~~~,::::::?, | |
,:::::::::::::::::,,;~~~~~~,::::::, | |
,:::::::::::::::::,,,;~~~~~~,:::::, | |
,:::::::::::::::::,,,,;~~~~~~,::::, | |
,:::::::::::::::::,,,,,;~~~~~~,:::, | |
,:::::::::::::::::,,,,, ;~~~~~~,::, | |
,:::::::::::::::::,,,,,, ;~~~~~~,,, | |
,:::::::::::::::::,,,,,,, ;~~~~~~, | |
,:::::::::::::::::,,,,,,,,,;~~~~~~, | |
,:::::::::::: ,,,,,,,,,,;~~~~~~, | |
´´´´´´´´´´´´´´´´´´´ ,,,,,,;~~~~~, | |
,,,,;~~~~, | |
´´´´" | |
.Replace(":", StringColorExtensions.DarkYellow(":").ToString()) | |
.Replace("~", StringColorExtensions.DarkGray("~").ToString()) | |
.Replace(";", StringColorExtensions.DarkGray(":").ToString()) | |
.Replace("?", StringColorExtensions.Yellow("?").ToString()) | |
.Replace(",", StringColorExtensions.DarkRed(",").ToString()) | |
.Replace("´", StringColorExtensions.DarkRed("´").ToString())); | |
}); | |
RunTarget("Play"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment