Merge branch 'hugo-vrijswijk-master'

This commit is contained in:
Michael Niksa 2018-04-25 15:45:38 -07:00
commit 3f76c471d0
9 changed files with 234 additions and 151 deletions

View file

@ -56,6 +56,12 @@ namespace ColorTool
[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)

View file

@ -9,6 +9,6 @@ namespace ColorTool
{
string Name { get; }
uint[] ParseScheme(string schemeName);
uint[] ParseScheme(string schemeName, bool reportErrors = true);
}
}

View file

@ -107,7 +107,7 @@ namespace ColorTool
return null;
}
public uint[] ParseScheme(string schemeName)
public uint[] ParseScheme(string schemeName, bool reportErrors = true)
{
bool success = true;
@ -127,7 +127,10 @@ namespace ColorTool
if (tableStrings[i].Length <= 0)
{
success = false;
if (reportErrors)
{
Console.WriteLine(string.Format(Resources.IniParseError, filename, name, tableStrings[i]));
}
break;
}
}
@ -143,8 +146,11 @@ namespace ColorTool
}
}
catch (Exception /*e*/)
{
if (reportErrors)
{
Console.WriteLine(string.Format(Resources.IniLoadError, filename));
}
colorTable = null;
}

View file

@ -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
@ -184,6 +187,54 @@ namespace ColorTool
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();
@ -302,6 +353,10 @@ namespace ColorTool
OutputUsage();
}
return;
case "-s":
case "--schemes":
PrintSchemes();
return;
default:
break;
}
@ -309,16 +364,7 @@ namespace ColorTool
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)
{
@ -342,5 +388,18 @@ namespace ColorTool
.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;
}
}
}

View file

@ -0,0 +1,7 @@
{
"profiles": {
"ColorTool": {
"commandName": "Project"
}
}
}

View file

@ -123,7 +123,7 @@ namespace ColorTool {
///By default, applies the colors in the specified .itermcolors or .ini file to the current console window.
///This does NOT save the properties automatically. For that, you&apos;ll need to open the properties sheet and hit &quot;Ok&quot;.
///Included should be a `schemes/` directory with a selection of schemes of both formats for examples.
///Feel free to add your own preferred scheme to that dire [rest of string was truncated]&quot;;.
///Feel free to add your own preferred scheme to that directory. [rest of string was truncated]&quot;;.
/// </summary>
public static string Usage {
get {

View file

@ -154,6 +154,7 @@ Options:
-q, --quiet : Don't print the color table after applying
-d, --defaults : Apply the scheme to only the defaults in the registry
-b, --both : Apply the scheme to both the current console and the defaults.
-s, --schemes : Displays all available schemes
-v, --version : Display the version number
-o, --output &lt;filename&gt; : output the current color table to an file (in .ini format)

View file

@ -123,7 +123,7 @@ namespace ColorTool
}
public uint[] ParseScheme(string schemeName)
public uint[] ParseScheme(string schemeName, bool reportErrors = true)
{
XmlDocument xmlDoc = loadXmlScheme(schemeName); // Create an XML document object
if (xmlDoc == null) return null;
@ -166,8 +166,11 @@ namespace ColorTool
}
if (colorsFound < COLOR_TABLE_SIZE)
{
if (reportErrors)
{
Console.WriteLine(Resources.InvalidNumberOfColors);
}
success = false;
}
if (!success)

View file

@ -17,6 +17,7 @@ Options:
-q, --quiet : Don't print the color table after applying
-d, --defaults : Apply the scheme to only the defaults in the registry
-b, --both : Apply the scheme to both the current console and the defaults.
-s, --schemes : Display all available schemes
-v, --version : Display the version number
```