Skip to content

Commit 7daea0a

Browse files
committed
pull logic out of Program.cs
There aren't any user-facing changes in this commit, just pulling logic out of Program.cs. All that remains in Program.cs is command line parsing. - The functions that wrote to the registry, the console, and the virtual terminal (--xterm) are now in their own files, implementing the `IConsoleTarget` interface - Move the utility method UIntToColor into ColorScheme, where it can be used as an indexer, e.g. myColorScheme[i] returns a System.Drawing.Color - The "export to INI" functionality is now in a "SchemeWriters" namespace; Parsers are now in a "SchemeParsers" namespace - Printing the color table is now in the ColorTable class.
1 parent 619a80e commit 7daea0a

File tree

16 files changed

+617
-547
lines changed

16 files changed

+617
-547
lines changed

tools/ColorTool/ColorTool/ColorScheme.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//
2-
// Copyright (C) Microsoft. All rights reserved.
2+
// Copyright (C) Microsoft. All rights reserved.
33
// Licensed under the terms described in the LICENSE file in the root of this project.
44
//
55

66
using System;
7+
using System.Drawing;
78
using System.Linq;
89

910
namespace ColorTool
@@ -13,6 +14,15 @@ public class ColorScheme
1314
public uint[] colorTable = null;
1415
public ConsoleAttributes consoleAttributes;
1516

17+
public Color this[int index] => UIntToColor(colorTable[index]);
18+
19+
private static Color UIntToColor(uint color)
20+
{
21+
byte r = (byte)(color >> 0);
22+
byte g = (byte)(color >> 8);
23+
byte b = (byte)(color >> 16);
24+
return Color.FromArgb(r, g, b);
25+
}
1626

1727
public int CalculateIndex(uint value) =>
1828
colorTable.Select((color, idx) => Tuple.Create(color, idx))
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
//
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the terms described in the LICENSE file in the root of this project.
4+
//
5+
6+
using System;
7+
8+
namespace ColorTool
9+
{
10+
class ColorTable
11+
{
12+
static int DARK_BLACK = 0;
13+
static int DARK_BLUE = 1;
14+
static int DARK_GREEN = 2;
15+
static int DARK_CYAN = 3;
16+
static int DARK_RED = 4;
17+
static int DARK_MAGENTA = 5;
18+
static int DARK_YELLOW = 6;
19+
static int DARK_WHITE = 7;
20+
static int BRIGHT_BLACK = 8;
21+
static int BRIGHT_BLUE = 9;
22+
static int BRIGHT_GREEN = 10;
23+
static int BRIGHT_CYAN = 11;
24+
static int BRIGHT_RED = 12;
25+
static int BRIGHT_MAGENTA = 13;
26+
static int BRIGHT_YELLOW = 14;
27+
static int BRIGHT_WHITE = 15;
28+
29+
// This is the order of colors when output by the table.
30+
static int[] outputFgs = {
31+
BRIGHT_WHITE ,
32+
DARK_BLACK ,
33+
BRIGHT_BLACK ,
34+
DARK_RED ,
35+
BRIGHT_RED ,
36+
DARK_GREEN ,
37+
BRIGHT_GREEN ,
38+
DARK_YELLOW ,
39+
BRIGHT_YELLOW ,
40+
DARK_BLUE ,
41+
BRIGHT_BLUE ,
42+
DARK_MAGENTA ,
43+
BRIGHT_MAGENTA ,
44+
DARK_CYAN ,
45+
BRIGHT_CYAN ,
46+
DARK_WHITE ,
47+
BRIGHT_WHITE
48+
};
49+
50+
51+
static int[] saneBgs = {
52+
DARK_BLACK ,
53+
DARK_RED ,
54+
DARK_GREEN ,
55+
DARK_YELLOW ,
56+
DARK_BLUE ,
57+
DARK_MAGENTA ,
58+
DARK_CYAN ,
59+
DARK_WHITE
60+
};
61+
62+
63+
public static void PrintTable()
64+
{
65+
ConsoleColor[] colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
66+
// Save the current background and foreground colors.
67+
ConsoleColor currentBackground = Console.BackgroundColor;
68+
ConsoleColor currentForeground = Console.ForegroundColor;
69+
string test = " gYw ";
70+
string[] FGs = {
71+
"m",
72+
"1m",
73+
"30m",
74+
"1;30m",
75+
"31m",
76+
"1;31m",
77+
"32m",
78+
"1;32m",
79+
"33m",
80+
"1;33m",
81+
"34m",
82+
"1;34m",
83+
"35m",
84+
"1;35m",
85+
"36m",
86+
"1;36m",
87+
"37m",
88+
"1;37m"
89+
};
90+
string[] BGs = {
91+
"m",
92+
"40m",
93+
"41m",
94+
"42m",
95+
"43m",
96+
"44m",
97+
"45m",
98+
"46m",
99+
"47m"
100+
};
101+
102+
Console.Write("\t");
103+
for (int bg = 0; bg < BGs.Length; bg++)
104+
{
105+
if (bg > 0) Console.Write(" ");
106+
Console.Write(" ");
107+
Console.Write(bg == 0 ? " " : BGs[bg]);
108+
Console.Write(" ");
109+
}
110+
Console.WriteLine();
111+
112+
for (int fg = 0; fg < FGs.Length; fg++)
113+
{
114+
Console.ForegroundColor = currentForeground;
115+
Console.BackgroundColor = currentBackground;
116+
117+
if (fg >= 0) Console.Write(FGs[fg] + "\t");
118+
119+
if (fg == 0) Console.ForegroundColor = currentForeground;
120+
else Console.ForegroundColor = colors[outputFgs[fg - 1]];
121+
122+
for (int bg = 0; bg < BGs.Length; bg++)
123+
{
124+
if (bg > 0) Console.Write(" ");
125+
if (bg == 0)
126+
Console.BackgroundColor = currentBackground;
127+
else Console.BackgroundColor = colors[saneBgs[bg - 1]];
128+
Console.Write(test);
129+
Console.BackgroundColor = currentBackground;
130+
}
131+
Console.Write("\n");
132+
133+
}
134+
Console.Write("\n");
135+
136+
// Reset foreground and background colors
137+
Console.ForegroundColor = currentForeground;
138+
Console.BackgroundColor = currentBackground;
139+
}
140+
141+
public static void PrintTableWithVt()
142+
{
143+
// Save the current background and foreground colors.
144+
string test = " gYw ";
145+
string[] FGs = {
146+
"m",
147+
"1m",
148+
"30m",
149+
"1;30m",
150+
"31m",
151+
"1;31m",
152+
"32m",
153+
"1;32m",
154+
"33m",
155+
"1;33m",
156+
"34m",
157+
"1;34m",
158+
"35m",
159+
"1;35m",
160+
"36m",
161+
"1;36m",
162+
"37m",
163+
"1;37m"
164+
};
165+
string[] BGs = {
166+
"m",
167+
"40m",
168+
"41m",
169+
"42m",
170+
"43m",
171+
"44m",
172+
"45m",
173+
"46m",
174+
"47m"
175+
};
176+
177+
Console.Write("\t");
178+
for (int bg = 0; bg < BGs.Length; bg++)
179+
{
180+
if (bg > 0) Console.Write(" ");
181+
Console.Write(" ");
182+
Console.Write(bg == 0 ? " " : BGs[bg]);
183+
Console.Write(" ");
184+
}
185+
Console.WriteLine();
186+
187+
for (int fg = 0; fg < FGs.Length; fg++)
188+
{
189+
Console.Write("\x1b[m");
190+
191+
if (fg >= 0)
192+
{
193+
Console.Write(FGs[fg] + "\t");
194+
}
195+
196+
if (fg == 0)
197+
{
198+
Console.Write("\x1b[39m");
199+
}
200+
else
201+
{
202+
Console.Write("\x1b[" + FGs[fg]);
203+
}
204+
205+
for (int bg = 0; bg < BGs.Length; bg++)
206+
{
207+
if (bg > 0)
208+
{
209+
Console.Write(" ");
210+
}
211+
if (bg == 0)
212+
{
213+
Console.Write("\x1b[49m");
214+
}
215+
else
216+
{
217+
Console.Write("\x1b[" + BGs[bg]);
218+
}
219+
220+
Console.Write(test);
221+
Console.Write("\x1b[49m");
222+
}
223+
Console.Write("\n");
224+
225+
}
226+
Console.Write("\n");
227+
228+
// Reset foreground and background colors
229+
Console.Write("\x1b[m");
230+
}
231+
}
232+
}

tools/ColorTool/ColorTool/ConsoleAPI.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) Microsoft. All rights reserved.
2+
// Copyright (C) Microsoft. All rights reserved.
33
// Licensed under the terms described in the LICENSE file in the root of this project.
44
//
55

@@ -60,6 +60,9 @@ public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()
6060

6161
public static int STD_OUTPUT_HANDLE = -11;
6262

63+
64+
public static IntPtr GetStdOutputHandle() => GetStdHandle(STD_OUTPUT_HANDLE);
65+
6366
[DllImport("kernel32.dll", SetLastError = true)]
6467
public static extern IntPtr GetStdHandle(int nStdHandle);
6568

tools/ColorTool/ColorTool/ConsoleAttributes.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) Microsoft. All rights reserved.
2+
// Copyright (C) Microsoft. All rights reserved.
33
// Licensed under the terms described in the LICENSE file in the root of this project.
44
//
55

@@ -12,6 +12,5 @@ public struct ConsoleAttributes
1212

1313
public uint? popupForeground;
1414
public uint? popupBackground;
15-
1615
}
1716
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the terms described in the LICENSE file in the root of this project.
4+
//
5+
6+
using System;
7+
using static ColorTool.ConsoleAPI;
8+
9+
namespace ColorTool.ConsoleTargets
10+
{
11+
/// <summary>
12+
/// A console target that writes to the currently open console.
13+
/// </summary>
14+
class CurrentConsoleTarget : IConsoleTarget
15+
{
16+
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
17+
{
18+
CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
19+
IntPtr hOut = GetStdOutputHandle();
20+
bool success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);
21+
if (!success)
22+
{
23+
throw new InvalidOperationException("Could not obtain console screen buffer");
24+
}
25+
26+
csbiex.srWindow.Bottom++;
27+
for (int i = 0; i < 16; i++)
28+
{
29+
csbiex.ColorTable[i] = colorScheme.colorTable[i];
30+
}
31+
if (colorScheme.consoleAttributes.background != null && colorScheme.consoleAttributes.foreground != null)
32+
{
33+
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.foreground.Value);
34+
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.background.Value);
35+
csbiex.wAttributes = (ushort)(fgidx | (bgidx << 4));
36+
}
37+
if (colorScheme.consoleAttributes.popupBackground != null && colorScheme.consoleAttributes.popupForeground != null)
38+
{
39+
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupForeground.Value);
40+
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupBackground.Value);
41+
csbiex.wPopupAttributes = (ushort)(fgidx | (bgidx << 4));
42+
}
43+
SetConsoleScreenBufferInfoEx(hOut, ref csbiex);
44+
45+
if (!quietMode)
46+
{
47+
ColorTable.PrintTable();
48+
}
49+
}
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the terms described in the LICENSE file in the root of this project.
4+
//
5+
6+
using Microsoft.Win32;
7+
using System;
8+
9+
namespace ColorTool.ConsoleTargets
10+
{
11+
/// <summary>
12+
/// A console target that writes to the Windows registry to modify system defaults
13+
/// </summary>
14+
class DefaultConsoleTarget : IConsoleTarget
15+
{
16+
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
17+
{
18+
//TODO
19+
RegistryKey consoleKey = Registry.CurrentUser.OpenSubKey("Console", true);
20+
for (int i = 0; i < colorScheme.colorTable.Length; i++)
21+
{
22+
string valueName = "ColorTable" + (i < 10 ? "0" : "") + i;
23+
consoleKey.SetValue(valueName, colorScheme.colorTable[i], RegistryValueKind.DWord);
24+
}
25+
Console.WriteLine(Resources.WroteToDefaults);
26+
}
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the terms described in the LICENSE file in the root of this project.
4+
//
5+
6+
namespace ColorTool.ConsoleTargets
7+
{
8+
/// <summary>
9+
/// A console that can have a color scheme applied to it.
10+
/// </summary>
11+
interface IConsoleTarget
12+
{
13+
void ApplyColorScheme(ColorScheme colorScheme, bool quietMode);
14+
}
15+
}

0 commit comments

Comments
 (0)