~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Platform.Windows/src/Do.Platform/ConsoleCrayon.cs

  • Committer: Chris S.
  • Date: 2009-06-21 03:37:34 UTC
  • Revision ID: chris@szikszoy.com-20090621033734-ud2jdcd5pq9r3ue9
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ConsoleCrayon.cs
 
2
//
 
3
// Author:
 
4
//   Aaron Bockover <abockover@novell.com>
 
5
//
 
6
// Copyright (C) 2008 Novell, Inc.
 
7
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be
 
17
// included in all copies or substantial portions of the Software.
 
18
//
 
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.
 
26
//
 
27
 
 
28
using System;
 
29
 
 
30
namespace Do.Platform
 
31
{
 
32
        public static class ConsoleCrayon
 
33
        {
 
34
 
 
35
#region Public API
 
36
 
 
37
                private static ConsoleColor foreground_color;
 
38
                public static ConsoleColor ForegroundColor {
 
39
                        get { return foreground_color; }
 
40
                        set {
 
41
                                foreground_color = value;
 
42
                                SetColor (foreground_color, true);
 
43
                        }
 
44
                }
 
45
 
 
46
                private static ConsoleColor background_color;
 
47
                public static ConsoleColor BackgroundColor {
 
48
                        get { return background_color; }
 
49
                        set {
 
50
                                background_color = value;
 
51
                                SetColor (background_color, false);
 
52
                        }
 
53
                }
 
54
 
 
55
                public static void ResetColor ()
 
56
                {
 
57
                        if (XtermColors) {
 
58
                                Console.Write (GetAnsiResetControlCode ());
 
59
                        } else if (Environment.OSVersion.Platform != PlatformID.Unix &&
 
60
                                                !RuntimeIsMono) {
 
61
                                Console.ResetColor ();
 
62
                        }
 
63
                }
 
64
 
 
65
                private static void SetColor (ConsoleColor color, bool isForeground)
 
66
                {
 
67
                        if (color < ConsoleColor.Black || color > ConsoleColor.White) {
 
68
                                throw new ArgumentOutOfRangeException ("color",
 
69
                                        "Not a ConsoleColor value.");
 
70
                        }
 
71
 
 
72
                        if (XtermColors) {
 
73
                                Console.Write (GetAnsiColorControlCode (color, isForeground));
 
74
                        } else if (Environment.OSVersion.Platform != PlatformID.Unix &&
 
75
                                                !RuntimeIsMono) {
 
76
                                if (isForeground) {
 
77
                                        Console.ForegroundColor = color;
 
78
                                } else {
 
79
                                        Console.BackgroundColor = color;
 
80
                                }
 
81
                        }
 
82
                }
 
83
 
 
84
#endregion
 
85
 
 
86
#region Ansi/VT Code Calculation
 
87
 
 
88
                // Modified from Mono's System.TermInfoDriver
 
89
                // License: MIT/X11
 
90
                // Authors: Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
91
                // (C) 2005-2006 Novell, Inc <http://www.novell.com>
 
92
 
 
93
                private static int TranslateColor (ConsoleColor desired, out bool light)
 
94
                {
 
95
                        light = false;
 
96
                        switch (desired) {
 
97
                                // Dark colors
 
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;
 
106
 
 
107
                                                                                // Light colors
 
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;
 
116
                        }
 
117
                }
 
118
 
 
119
                private static string GetAnsiColorControlCode (ConsoleColor color, bool isForeground)
 
120
                {
 
121
                        // lighter fg colours are 90 -> 97 rather than 30 -> 37
 
122
                        // lighter bg colours are 100 -> 107 rather than 40 -> 47
 
123
                        bool light;
 
124
                        int code = TranslateColor (color, out light) + (isForeground ? 30 : 40) + (light ? 60 : 0);
 
125
                        return String.Format ("\x001b[{0}m", code);
 
126
                }
 
127
 
 
128
                private static string GetAnsiResetControlCode ()
 
129
                {
 
130
                        return "\x001b[0m";
 
131
                }
 
132
 
 
133
#endregion
 
134
 
 
135
#region xterm Detection
 
136
 
 
137
                private static bool? xterm_colors = null;
 
138
                public static bool XtermColors { 
 
139
                        get {
 
140
                                if (xterm_colors == null) {
 
141
                                        DetectXtermColors ();
 
142
                                }
 
143
 
 
144
                                return xterm_colors.Value;
 
145
                        }
 
146
                }
 
147
 
 
148
                [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
 
149
                        private extern static int _isatty (int fd);
 
150
 
 
151
                private static bool isatty (int fd)
 
152
                {
 
153
                        try {
 
154
                                return _isatty (fd) == 1;
 
155
                        } catch {
 
156
                                return false;
 
157
                        }
 
158
                }
 
159
 
 
160
                private static void DetectXtermColors ()
 
161
                {
 
162
                        bool _xterm_colors = false;
 
163
 
 
164
                        switch (Environment.GetEnvironmentVariable ("TERM")) {
 
165
                                case "xterm":
 
166
                                        case "rxvt":
 
167
                                        case "rxvt-unicode": 
 
168
                                        if (Environment.GetEnvironmentVariable ("COLORTERM") != null) {
 
169
                                                _xterm_colors = true;
 
170
                                        }
 
171
                                break;
 
172
                                case "xterm-color":
 
173
                                        _xterm_colors = true;
 
174
                                break;
 
175
                        }
 
176
 
 
177
                        xterm_colors = _xterm_colors && isatty (1) && isatty (2);
 
178
                }
 
179
 
 
180
#endregion
 
181
 
 
182
#region Runtime Detection
 
183
 
 
184
                private static bool? runtime_is_mono;
 
185
                public static bool RuntimeIsMono {
 
186
                        get { 
 
187
                                if (runtime_is_mono == null) {
 
188
                                        runtime_is_mono = Type.GetType ("System.MonoType") != null;
 
189
                                }
 
190
 
 
191
                                return runtime_is_mono.Value;
 
192
                        }
 
193
                }
 
194
 
 
195
#endregion
 
196
        }
 
197
}
 
198
 
 
199
 
 
200