1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 namespace NUnit.Common
34 /// Sets the console color in the constructor and resets it in the dispose
36 public class ColorConsole : IDisposable
38 #if !SILVERLIGHT && !NETCF
39 private ConsoleColor _originalColor;
43 /// Initializes a new instance of the <see cref="ColorConsole"/> class.
45 /// <param name="style">The color style to use.</param>
46 public ColorConsole(ColorStyle style)
48 #if !SILVERLIGHT && !NETCF
49 _originalColor = Console.ForegroundColor;
50 Console.ForegroundColor = GetColor(style);
54 #if !SILVERLIGHT && !NETCF
56 /// By using styles, we can keep everything consistent
58 /// <param name="style"></param>
59 /// <returns></returns>
60 public static ConsoleColor GetColor(ColorStyle style)
62 ConsoleColor color = GetColorForStyle(style);
63 ConsoleColor bg = Console.BackgroundColor;
65 if (color == bg || color == ConsoleColor.Red && bg == ConsoleColor.Magenta)
66 return bg == ConsoleColor.Black
73 private static ConsoleColor GetColorForStyle(ColorStyle style)
75 switch (Console.BackgroundColor)
77 case ConsoleColor.White:
80 case ColorStyle.Header:
81 return ConsoleColor.Black;
82 case ColorStyle.SubHeader:
83 return ConsoleColor.Black;
84 case ColorStyle.SectionHeader:
85 return ConsoleColor.Blue;
86 case ColorStyle.Label:
87 return ConsoleColor.Black;
88 case ColorStyle.Value:
89 return ConsoleColor.Blue;
91 return ConsoleColor.Green;
92 case ColorStyle.Failure:
93 return ConsoleColor.Red;
94 case ColorStyle.Warning:
95 return ConsoleColor.Black;
96 case ColorStyle.Error:
97 return ConsoleColor.Red;
98 case ColorStyle.Output:
99 return ConsoleColor.Black;
100 case ColorStyle.Help:
101 return ConsoleColor.Black;
102 case ColorStyle.Default:
104 return ConsoleColor.Black;
107 case ConsoleColor.Cyan:
108 case ConsoleColor.Green:
109 case ConsoleColor.Red:
110 case ConsoleColor.Magenta:
111 case ConsoleColor.Yellow:
114 case ColorStyle.Header:
115 return ConsoleColor.Black;
116 case ColorStyle.SubHeader:
117 return ConsoleColor.Black;
118 case ColorStyle.SectionHeader:
119 return ConsoleColor.Blue;
120 case ColorStyle.Label:
121 return ConsoleColor.Black;
122 case ColorStyle.Value:
123 return ConsoleColor.Black;
124 case ColorStyle.Pass:
125 return ConsoleColor.Black;
126 case ColorStyle.Failure:
127 return ConsoleColor.Red;
128 case ColorStyle.Warning:
129 return ConsoleColor.Yellow;
130 case ColorStyle.Error:
131 return ConsoleColor.Red;
132 case ColorStyle.Output:
133 return ConsoleColor.Black;
134 case ColorStyle.Help:
135 return ConsoleColor.Black;
136 case ColorStyle.Default:
138 return ConsoleColor.Black;
144 case ColorStyle.Header:
145 return ConsoleColor.White;
146 case ColorStyle.SubHeader:
147 return ConsoleColor.Gray;
148 case ColorStyle.SectionHeader:
149 return ConsoleColor.Cyan;
150 case ColorStyle.Label:
151 return ConsoleColor.Green;
152 case ColorStyle.Value:
153 return ConsoleColor.White;
154 case ColorStyle.Pass:
155 return ConsoleColor.Green;
156 case ColorStyle.Failure:
157 return ConsoleColor.Red;
158 case ColorStyle.Warning:
159 return ConsoleColor.Yellow;
160 case ColorStyle.Error:
161 return ConsoleColor.Red;
162 case ColorStyle.Output:
163 return ConsoleColor.Gray;
164 case ColorStyle.Help:
165 return ConsoleColor.Green;
166 case ColorStyle.Default:
168 return ConsoleColor.Green;
174 #region Implementation of IDisposable
177 /// If color is enabled, restores the console colors to their defaults
179 public void Dispose()
181 #if !SILVERLIGHT && !NETCF
182 Console.ForegroundColor = _originalColor;