Skip to content

Commit

Permalink
Prototype support for WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Oct 13, 2017
1 parent dc9cab0 commit 5b47a58
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tools/ColorTool/ColorTool/ConsoleAPI.cs
Expand Up @@ -56,12 +56,33 @@ public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleScreenBufferInfoEx(IntPtr ConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsole(
IntPtr hConsoleOutput,
string lpBuffer,
uint nNumberOfCharsToWrite,
out uint lpNumberOfCharsWritten,
IntPtr lpReserved
);
////////////////////////////////////////////////////////////////////////

public static uint RGB(int r, int g, int b)
{
return (uint)r + (((uint)g) << 8) + (((uint)b) << 16);
}
public static uint Rvalue(uint rgb)
{
return rgb & 0x000000ff;
}
public static uint Gvalue(uint rgb)
{
return rgb & 0x0000ff00 >> 8;
}
public static uint Bvalue(uint rgb)
{
return (rgb & 0x00ff0000) >> 16;
}

public const int COLOR_TABLE_SIZE = 16;
}
Expand Down
57 changes: 56 additions & 1 deletion tools/ColorTool/ColorTool/Program.cs
Expand Up @@ -83,6 +83,7 @@ class Program
static bool quietMode = false;
static bool setDefaults = false;
static bool setProperties = true;
static bool setUnixStyle = false;

static void Usage()
{
Expand Down Expand Up @@ -200,6 +201,47 @@ static bool SetProperties(uint[] colorTable)
}
return success;
}

static int[] VT_INDICIES = {
0, // DARK_BLACK
4, // DARK_BLUE
2, // DARK_GREEN
6, // DARK_CYAN
1, // DARK_RED
5, // DARK_MAGENTA
3, // DARK_YELLOW
7, // DARK_WHITE
8+0, // BRIGHT_BLACK
8+4, // BRIGHT_BLUE
8+2, // BRIGHT_GREEN
8+6, // BRIGHT_CYAN
8+1, // BRIGHT_RED
8+5, // BRIGHT_MAGENTA
8+3, // BRIGHT_YELLOW
8+7,// BRIGHT_WHITE
};

static bool SetPropertiesWithVt(uint[] colorTable)
{
Console.WriteLine("Setting colors using VT Sequences");
int STD_OUTPUT_HANDLE = -11;
IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
IntPtr whatever = hOut;
uint dwWritten = 0;
for(int i = 0; i < 16; i++)
{
int vtIndex = VT_INDICIES[i];
uint rgb = colorTable[i];
string s = "\x1b]4;" + vtIndex + ";rgb:" + Rvalue(rgb) + "/"+ Gvalue(rgb) + "/"+ Bvalue(rgb) + "\x7";
string printable = "\\x1b]4;" + vtIndex + ";rgb:" + Rvalue(rgb).ToString("X") + "/"+ Gvalue(rgb).ToString("X") + "/"+ Bvalue(rgb).ToString("X") + "\x7";
Console.WriteLine(s);
Console.WriteLine(printable);
// WriteConsole(hOut, s, (uint)s.Length, out dwWritten, whatever);
// WriteConsole(hOut, printable, (uint)printable.Length, out dwWritten, whatever);
}
return true;
}

static bool SetDefaults(uint[] colorTable)
{
RegistryKey consoleKey = Registry.CurrentUser.OpenSubKey("Console", true);
Expand Down Expand Up @@ -251,6 +293,11 @@ static void Main(string[] args)
case "--version":
Version();
return;
case "-x":
case "--xterm":
setUnixStyle = true;
setProperties = true;
break;
default:
break;
}
Expand Down Expand Up @@ -282,7 +329,15 @@ static void Main(string[] args)
}
if (setProperties)
{
SetProperties(colorTable);
if (setUnixStyle)
{
SetPropertiesWithVt(colorTable);
}
else
{
SetProperties(colorTable);

}
}
}

Expand Down

0 comments on commit 5b47a58

Please sign in to comment.