Skip to content

Commit

Permalink
Merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
miniksa committed Apr 25, 2018
2 parents 4c3065f + fe7a2d0 commit 550e197
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 145 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
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);
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)
{
foreach (var parser in GetParsers())
{
uint[] table = parser.ParseScheme(schemeName);
if (table != null)
{
return table;
}
}
return null;
}
}
}
Loading

0 comments on commit 550e197

Please sign in to comment.