Skip to content

Commit

Permalink
pull logic out of Program.cs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
waf committed Apr 23, 2019
1 parent 619a80e commit 7daea0a
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 547 deletions.
12 changes: 11 additions & 1 deletion tools/ColorTool/ColorTool/ColorScheme.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

using System;
using System.Drawing;
using System.Linq;

namespace ColorTool
Expand All @@ -13,6 +14,15 @@ public class ColorScheme
public uint[] colorTable = null;
public ConsoleAttributes consoleAttributes;

public Color this[int index] => UIntToColor(colorTable[index]);

private static Color UIntToColor(uint color)
{
byte r = (byte)(color >> 0);
byte g = (byte)(color >> 8);
byte b = (byte)(color >> 16);
return Color.FromArgb(r, g, b);
}

public int CalculateIndex(uint value) =>
colorTable.Select((color, idx) => Tuple.Create(color, idx))
Expand Down
232 changes: 232 additions & 0 deletions tools/ColorTool/ColorTool/ColorTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

using System;

namespace ColorTool
{
class ColorTable
{
static int DARK_BLACK = 0;
static int DARK_BLUE = 1;
static int DARK_GREEN = 2;
static int DARK_CYAN = 3;
static int DARK_RED = 4;
static int DARK_MAGENTA = 5;
static int DARK_YELLOW = 6;
static int DARK_WHITE = 7;
static int BRIGHT_BLACK = 8;
static int BRIGHT_BLUE = 9;
static int BRIGHT_GREEN = 10;
static int BRIGHT_CYAN = 11;
static int BRIGHT_RED = 12;
static int BRIGHT_MAGENTA = 13;
static int BRIGHT_YELLOW = 14;
static int BRIGHT_WHITE = 15;

// This is the order of colors when output by the table.
static int[] outputFgs = {
BRIGHT_WHITE ,
DARK_BLACK ,
BRIGHT_BLACK ,
DARK_RED ,
BRIGHT_RED ,
DARK_GREEN ,
BRIGHT_GREEN ,
DARK_YELLOW ,
BRIGHT_YELLOW ,
DARK_BLUE ,
BRIGHT_BLUE ,
DARK_MAGENTA ,
BRIGHT_MAGENTA ,
DARK_CYAN ,
BRIGHT_CYAN ,
DARK_WHITE ,
BRIGHT_WHITE
};


static int[] saneBgs = {
DARK_BLACK ,
DARK_RED ,
DARK_GREEN ,
DARK_YELLOW ,
DARK_BLUE ,
DARK_MAGENTA ,
DARK_CYAN ,
DARK_WHITE
};


public static void PrintTable()
{
ConsoleColor[] colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
// Save the current background and foreground colors.
ConsoleColor currentBackground = Console.BackgroundColor;
ConsoleColor currentForeground = Console.ForegroundColor;
string test = " gYw ";
string[] FGs = {
"m",
"1m",
"30m",
"1;30m",
"31m",
"1;31m",
"32m",
"1;32m",
"33m",
"1;33m",
"34m",
"1;34m",
"35m",
"1;35m",
"36m",
"1;36m",
"37m",
"1;37m"
};
string[] BGs = {
"m",
"40m",
"41m",
"42m",
"43m",
"44m",
"45m",
"46m",
"47m"
};

Console.Write("\t");
for (int bg = 0; bg < BGs.Length; bg++)
{
if (bg > 0) Console.Write(" ");
Console.Write(" ");
Console.Write(bg == 0 ? " " : BGs[bg]);
Console.Write(" ");
}
Console.WriteLine();

for (int fg = 0; fg < FGs.Length; fg++)
{
Console.ForegroundColor = currentForeground;
Console.BackgroundColor = currentBackground;

if (fg >= 0) Console.Write(FGs[fg] + "\t");

if (fg == 0) Console.ForegroundColor = currentForeground;
else Console.ForegroundColor = colors[outputFgs[fg - 1]];

for (int bg = 0; bg < BGs.Length; bg++)
{
if (bg > 0) Console.Write(" ");
if (bg == 0)
Console.BackgroundColor = currentBackground;
else Console.BackgroundColor = colors[saneBgs[bg - 1]];
Console.Write(test);
Console.BackgroundColor = currentBackground;
}
Console.Write("\n");

}
Console.Write("\n");

// Reset foreground and background colors
Console.ForegroundColor = currentForeground;
Console.BackgroundColor = currentBackground;
}

public static void PrintTableWithVt()
{
// Save the current background and foreground colors.
string test = " gYw ";
string[] FGs = {
"m",
"1m",
"30m",
"1;30m",
"31m",
"1;31m",
"32m",
"1;32m",
"33m",
"1;33m",
"34m",
"1;34m",
"35m",
"1;35m",
"36m",
"1;36m",
"37m",
"1;37m"
};
string[] BGs = {
"m",
"40m",
"41m",
"42m",
"43m",
"44m",
"45m",
"46m",
"47m"
};

Console.Write("\t");
for (int bg = 0; bg < BGs.Length; bg++)
{
if (bg > 0) Console.Write(" ");
Console.Write(" ");
Console.Write(bg == 0 ? " " : BGs[bg]);
Console.Write(" ");
}
Console.WriteLine();

for (int fg = 0; fg < FGs.Length; fg++)
{
Console.Write("\x1b[m");

if (fg >= 0)
{
Console.Write(FGs[fg] + "\t");
}

if (fg == 0)
{
Console.Write("\x1b[39m");
}
else
{
Console.Write("\x1b[" + FGs[fg]);
}

for (int bg = 0; bg < BGs.Length; bg++)
{
if (bg > 0)
{
Console.Write(" ");
}
if (bg == 0)
{
Console.Write("\x1b[49m");
}
else
{
Console.Write("\x1b[" + BGs[bg]);
}

Console.Write(test);
Console.Write("\x1b[49m");
}
Console.Write("\n");

}
Console.Write("\n");

// Reset foreground and background colors
Console.Write("\x1b[m");
}
}
}
5 changes: 4 additions & 1 deletion tools/ColorTool/ColorTool/ConsoleAPI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

Expand Down Expand Up @@ -60,6 +60,9 @@ public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()

public static int STD_OUTPUT_HANDLE = -11;


public static IntPtr GetStdOutputHandle() => GetStdHandle(STD_OUTPUT_HANDLE);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);

Expand Down
3 changes: 1 addition & 2 deletions tools/ColorTool/ColorTool/ConsoleAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

Expand All @@ -12,6 +12,5 @@ public struct ConsoleAttributes

public uint? popupForeground;
public uint? popupBackground;

}
}
51 changes: 51 additions & 0 deletions tools/ColorTool/ColorTool/ConsoleTargets/CurrentConsoleTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

using System;
using static ColorTool.ConsoleAPI;

namespace ColorTool.ConsoleTargets
{
/// <summary>
/// A console target that writes to the currently open console.
/// </summary>
class CurrentConsoleTarget : IConsoleTarget
{
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
{
CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
IntPtr hOut = GetStdOutputHandle();
bool success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);
if (!success)
{
throw new InvalidOperationException("Could not obtain console screen buffer");
}

csbiex.srWindow.Bottom++;
for (int i = 0; i < 16; i++)
{
csbiex.ColorTable[i] = colorScheme.colorTable[i];
}
if (colorScheme.consoleAttributes.background != null && colorScheme.consoleAttributes.foreground != null)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.foreground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.background.Value);
csbiex.wAttributes = (ushort)(fgidx | (bgidx << 4));
}
if (colorScheme.consoleAttributes.popupBackground != null && colorScheme.consoleAttributes.popupForeground != null)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupForeground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupBackground.Value);
csbiex.wPopupAttributes = (ushort)(fgidx | (bgidx << 4));
}
SetConsoleScreenBufferInfoEx(hOut, ref csbiex);

if (!quietMode)
{
ColorTable.PrintTable();
}
}
}
}
28 changes: 28 additions & 0 deletions tools/ColorTool/ColorTool/ConsoleTargets/DefaultConsoleTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

using Microsoft.Win32;
using System;

namespace ColorTool.ConsoleTargets
{
/// <summary>
/// A console target that writes to the Windows registry to modify system defaults
/// </summary>
class DefaultConsoleTarget : IConsoleTarget
{
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
{
//TODO
RegistryKey consoleKey = Registry.CurrentUser.OpenSubKey("Console", true);
for (int i = 0; i < colorScheme.colorTable.Length; i++)
{
string valueName = "ColorTable" + (i < 10 ? "0" : "") + i;
consoleKey.SetValue(valueName, colorScheme.colorTable[i], RegistryValueKind.DWord);
}
Console.WriteLine(Resources.WroteToDefaults);
}
}
}
15 changes: 15 additions & 0 deletions tools/ColorTool/ColorTool/ConsoleTargets/IConsoleTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

namespace ColorTool.ConsoleTargets
{
/// <summary>
/// A console that can have a color scheme applied to it.
/// </summary>
interface IConsoleTarget
{
void ApplyColorScheme(ColorScheme colorScheme, bool quietMode);
}
}
Loading

0 comments on commit 7daea0a

Please sign in to comment.