Merge branch 'twsouthwick-dynamic-parser-list'

This commit is contained in:
Michael Niksa 2018-04-25 14:23:34 -07:00
commit cad9d55e41
6 changed files with 29 additions and 12 deletions

View file

@ -7,6 +7,8 @@ namespace ColorTool
{ {
interface ISchemeParser interface ISchemeParser
{ {
string Name { get; }
uint[] ParseScheme(string schemeName); uint[] ParseScheme(string schemeName);
} }
} }

View file

@ -16,7 +16,7 @@ namespace ColorTool
[DllImport("kernel32")] [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
// These are in Windows Color table order - BRG, not RGB. // These are in Windows Color table order - BRG, not RGB.
public static string[] COLOR_NAMES = { public static string[] COLOR_NAMES = {
"DARK_BLACK", "DARK_BLACK",
"DARK_BLUE", "DARK_BLUE",
@ -36,6 +36,8 @@ namespace ColorTool
"BRIGHT_WHITE" "BRIGHT_WHITE"
}; };
public string Name => "INI File Parser";
static uint ParseHex(string arg) static uint ParseHex(string arg)
{ {
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg); System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
@ -44,11 +46,12 @@ namespace ColorTool
static uint ParseRgb(string arg) static uint ParseRgb(string arg)
{ {
int[] components = { 0, 0, 0}; int[] components = { 0, 0, 0 };
string[] args = arg.Split(','); string[] args = arg.Split(',');
if (args.Length != components.Length) throw new Exception("Invalid color format \"" + arg + "\""); if (args.Length != components.Length) throw new Exception("Invalid color format \"" + arg + "\"");
if (args.Length != 3) throw new Exception("Invalid color format \"" + arg + "\""); if (args.Length != 3) throw new Exception("Invalid color format \"" + arg + "\"");
for (int i = 0; i < args.Length; i++){ for (int i = 0; i < args.Length; i++)
{
components[i] = Int32.Parse(args[i]); components[i] = Int32.Parse(args[i]);
} }

View file

@ -3,10 +3,12 @@
// Licensed under the terms described in the LICENSE file in the root of this project. // Licensed under the terms described in the LICENSE file in the root of this project.
// //
using System;
using static ColorTool.ConsoleAPI;
using Microsoft.Win32; using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using static ColorTool.ConsoleAPI;
namespace ColorTool namespace ColorTool
{ {
@ -86,7 +88,8 @@ namespace ColorTool
static void Usage() static void Usage()
{ {
Console.WriteLine(Resources.Usage); Console.WriteLine(Resources.Usage,
string.Join($"{Environment.NewLine} ", GetParsers().Select(p => p.Name)));
} }
static void OutputUsage() static void OutputUsage()
@ -141,7 +144,7 @@ namespace ColorTool
"46m", "46m",
"47m" "47m"
}; };
Console.Write("\t"); Console.Write("\t");
for (int bg = 0; bg < BGs.Length; bg++) for (int bg = 0; bg < BGs.Length; bg++)
{ {
@ -307,8 +310,7 @@ namespace ColorTool
string schemeName = args[args.Length - 1]; string schemeName = args[args.Length - 1];
uint[] colorTable = null; uint[] colorTable = null;
ISchemeParser[] parsers = { new XmlSchemeParser(), new IniSchemeParser() }; foreach (var parser in GetParsers())
foreach (var parser in parsers)
{ {
uint[] table = parser.ParseScheme(schemeName); uint[] table = parser.ParseScheme(schemeName);
if (table != null) if (table != null)
@ -334,5 +336,11 @@ namespace ColorTool
} }
} }
private static IEnumerable<ISchemeParser> GetParsers()
{
return typeof(Program).Assembly.GetTypes()
.Where(t => !t.IsAbstract && typeof(ISchemeParser).IsAssignableFrom(t))
.Select(t => (ISchemeParser)Activator.CreateInstance(t));
}
} }
} }

View file

@ -19,7 +19,7 @@ namespace ColorTool {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Resources { public class Resources {
@ -123,7 +123,7 @@ namespace ColorTool {
///By default, applies the colors in the specified .itermcolors or .ini file to the current console window. ///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;. ///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. ///Included should be a `schemes/` directory with a selection of schemes of both formats for examples.
///Feel free to add your own prefered scheme to that direc [rest of string was truncated]&quot;;. ///Feel free to add your own preferred scheme to that dire [rest of string was truncated]&quot;;.
/// </summary> /// </summary>
public static string Usage { public static string Usage {
get { get {

View file

@ -156,7 +156,9 @@ Options:
-b, --both : Apply the scheme to both the current console and the defaults. -b, --both : Apply the scheme to both the current console and the defaults.
-v, --version : Display the version number -v, --version : Display the version number
-o, --output &lt;filename&gt; : output the current color table to an file (in .ini format) -o, --output &lt;filename&gt; : output the current color table to an file (in .ini format)
</value>
Available importers:
{0}</value>
</data> </data>
<data name="WroteToDefaults" xml:space="preserve"> <data name="WroteToDefaults" xml:space="preserve">
<value>Wrote selected scheme to the defaults.</value> <value>Wrote selected scheme to the defaults.</value>

View file

@ -34,6 +34,8 @@ namespace ColorTool
static string GREEN_KEY = "Green Component"; static string GREEN_KEY = "Green Component";
static string BLUE_KEY = "Blue Component"; static string BLUE_KEY = "Blue Component";
public string Name => "iTerm Parser";
static bool parseRgbFromXml(XmlNode components, ref uint rgb) static bool parseRgbFromXml(XmlNode components, ref uint rgb)
{ {
int r = -1; int r = -1;