4
// Aaron Bockover <abockover@novell.com>
6
// Copyright (C) 2008 Novell, Inc.
8
// Permission is hereby granted, free of charge, to any person obtaining
9
// a copy of this software and associated documentation files (the
10
// "Software"), to deal in the Software without restriction, including
11
// without limitation the rights to use, copy, modify, merge, publish,
12
// distribute, sublicense, and/or sell copies of the Software, and to
13
// permit persons to whom the Software is furnished to do so, subject to
14
// the following conditions:
16
// The above copyright notice and this permission notice shall be
17
// included in all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
public static class ConsoleCrayon
37
private static ConsoleColor foreground_color;
38
public static ConsoleColor ForegroundColor {
39
get { return foreground_color; }
41
foreground_color = value;
42
SetColor (foreground_color, true);
46
private static ConsoleColor background_color;
47
public static ConsoleColor BackgroundColor {
48
get { return background_color; }
50
background_color = value;
51
SetColor (background_color, false);
55
public static void ResetColor ()
58
Console.Write (GetAnsiResetControlCode ());
59
} else if (Environment.OSVersion.Platform != PlatformID.Unix &&
61
Console.ResetColor ();
65
private static void SetColor (ConsoleColor color, bool isForeground)
67
if (color < ConsoleColor.Black || color > ConsoleColor.White) {
68
throw new ArgumentOutOfRangeException ("color",
69
"Not a ConsoleColor value.");
73
Console.Write (GetAnsiColorControlCode (color, isForeground));
74
} else if (Environment.OSVersion.Platform != PlatformID.Unix &&
77
Console.ForegroundColor = color;
79
Console.BackgroundColor = color;
86
#region Ansi/VT Code Calculation
88
// Modified from Mono's System.TermInfoDriver
90
// Authors: Gonzalo Paniagua Javier <gonzalo@ximian.com>
91
// (C) 2005-2006 Novell, Inc <http://www.novell.com>
93
private static int TranslateColor (ConsoleColor desired, out bool light)
98
case ConsoleColor.Black: return 0;
99
case ConsoleColor.DarkRed: return 1;
100
case ConsoleColor.DarkGreen: return 2;
101
case ConsoleColor.DarkYellow: return 3;
102
case ConsoleColor.DarkBlue: return 4;
103
case ConsoleColor.DarkMagenta: return 5;
104
case ConsoleColor.DarkCyan: return 6;
105
case ConsoleColor.Gray: return 7;
108
case ConsoleColor.DarkGray: light = true; return 0;
109
case ConsoleColor.Red: light = true; return 1;
110
case ConsoleColor.Green: light = true; return 2;
111
case ConsoleColor.Yellow: light = true; return 3;
112
case ConsoleColor.Blue: light = true; return 4;
113
case ConsoleColor.Magenta: light = true; return 5;
114
case ConsoleColor.Cyan: light = true; return 6;
115
case ConsoleColor.White: default: light = true; return 7;
119
private static string GetAnsiColorControlCode (ConsoleColor color, bool isForeground)
121
// lighter fg colours are 90 -> 97 rather than 30 -> 37
122
// lighter bg colours are 100 -> 107 rather than 40 -> 47
124
int code = TranslateColor (color, out light) + (isForeground ? 30 : 40) + (light ? 60 : 0);
125
return String.Format ("\x001b[{0}m", code);
128
private static string GetAnsiResetControlCode ()
135
#region xterm Detection
137
private static bool? xterm_colors = null;
138
public static bool XtermColors {
140
if (xterm_colors == null) {
141
DetectXtermColors ();
144
return xterm_colors.Value;
148
[System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
149
private extern static int _isatty (int fd);
151
private static bool isatty (int fd)
154
return _isatty (fd) == 1;
160
private static void DetectXtermColors ()
162
bool _xterm_colors = false;
164
switch (Environment.GetEnvironmentVariable ("TERM")) {
168
if (Environment.GetEnvironmentVariable ("COLORTERM") != null) {
169
_xterm_colors = true;
173
_xterm_colors = true;
177
xterm_colors = _xterm_colors && isatty (1) && isatty (2);
182
#region Runtime Detection
184
private static bool? runtime_is_mono;
185
public static bool RuntimeIsMono {
187
if (runtime_is_mono == null) {
188
runtime_is_mono = Type.GetType ("System.MonoType") != null;
191
return runtime_is_mono.Value;