Skip to content

Commit

Permalink
Merge branch 'hugo-vrijswijk-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
miniksa committed Apr 25, 2018
2 parents 1465871 + d6e77ed commit 3f76c47
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 151 deletions.
6 changes: 6 additions & 0 deletions tools/ColorTool/ColorTool/ConsoleAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ 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 SetConsoleMode(IntPtr hConsoleHandle, int mode);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr handle, out int mode);
////////////////////////////////////////////////////////////////////////

public static uint RGB(int r, int g, int b)
Expand Down
2 changes: 1 addition & 1 deletion tools/ColorTool/ColorTool/ISchemeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ interface ISchemeParser
{
string Name { get; }

uint[] ParseScheme(string schemeName);
uint[] ParseScheme(string schemeName, bool reportErrors = true);
}
}
12 changes: 9 additions & 3 deletions tools/ColorTool/ColorTool/IniSchemeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static string FindIniScheme(string schemeName)
return null;
}

public uint[] ParseScheme(string schemeName)
public uint[] ParseScheme(string schemeName, bool reportErrors = true)
{
bool success = true;

Expand All @@ -127,7 +127,10 @@ public uint[] ParseScheme(string schemeName)
if (tableStrings[i].Length <= 0)
{
success = false;
Console.WriteLine(string.Format(Resources.IniParseError, filename, name, tableStrings[i]));
if (reportErrors)
{
Console.WriteLine(string.Format(Resources.IniParseError, filename, name, tableStrings[i]));
}
break;
}
}
Expand All @@ -144,7 +147,10 @@ public uint[] ParseScheme(string schemeName)
}
catch (Exception /*e*/)
{
Console.WriteLine(string.Format(Resources.IniLoadError, filename));
if (reportErrors)
{
Console.WriteLine(string.Format(Resources.IniLoadError, filename));
}

colorTable = null;
}
Expand Down
79 changes: 69 additions & 10 deletions tools/ColorTool/ColorTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using static ColorTool.ConsoleAPI;

namespace ColorTool
Expand Down Expand Up @@ -184,6 +187,54 @@ static void PrintTable()
Console.BackgroundColor = currentBackground;
}

static void PrintSchemes()
{
if (Directory.Exists("./schemes"))
{
IntPtr handle = GetStdHandle(-11);
GetConsoleMode(handle, out var mode);
SetConsoleMode(handle, mode | 0x4);

int consoleWidth = Console.WindowWidth;
string fgText = " gYw ";
foreach (string schemeName in Directory.GetFiles("./schemes/").Select(Path.GetFileName))
{
uint[] colorTable = GetSchemeUints(schemeName, false);
if (colorTable != null)
{
string colors = string.Empty;
for (var index = 0; index < 8; index++)
{
uint t = colorTable[index];
var color = UIntToColor(t);
// Set the background color to the color in the scheme, plus some text to show how it looks
colors += $"\x1b[48;2;{color.R};{color.G};{color.B}m{fgText}";
}
// Align scheme colors right, or on newline if it doesn't fit
int schemeTextLength = fgText.Length * 8;
int bufferLength = consoleWidth - (schemeName.Length + schemeTextLength);

string bufferString = bufferLength >= 0
? new string(' ', bufferLength)
: "\n" + new string(' ', consoleWidth - schemeTextLength);

string outputString = schemeName + bufferString + colors;
Console.WriteLine(outputString);
Console.ResetColor();
}
}
}
}

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

static bool SetProperties(uint[] colorTable)
{
CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
Expand Down Expand Up @@ -302,23 +353,18 @@ static void Main(string[] args)
OutputUsage();
}
return;
case "-s":
case "--schemes":
PrintSchemes();
return;
default:
break;
}
}

string schemeName = args[args.Length - 1];

uint[] colorTable = null;
foreach (var parser in GetParsers())
{
uint[] table = parser.ParseScheme(schemeName);
if (table != null)
{
colorTable = table;
break;
}
}
uint[] colorTable = GetSchemeUints(schemeName);

if (colorTable == null)
{
Expand All @@ -342,5 +388,18 @@ private static IEnumerable<ISchemeParser> GetParsers()
.Where(t => !t.IsAbstract && typeof(ISchemeParser).IsAssignableFrom(t))
.Select(t => (ISchemeParser)Activator.CreateInstance(t));
}

private static uint[] GetSchemeUints(string schemeName, bool reportErrors = true)
{
foreach (var parser in GetParsers())
{
uint[] table = parser.ParseScheme(schemeName, reportErrors);
if (table != null)
{
return table;
}
}
return null;
}
}
}
7 changes: 7 additions & 0 deletions tools/ColorTool/ColorTool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"ColorTool": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit 3f76c47

Please sign in to comment.