Dynamically generate list of parsers

This commit is contained in:
Taylor Southwick 2017-08-28 10:06:45 -07:00
parent cf4780a4a2
commit 9bd6053664
6 changed files with 30 additions and 12 deletions

View file

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

View file

@ -16,7 +16,7 @@ namespace ColorTool
[DllImport("kernel32")]
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.
static string[] COLOR_NAMES = {
"DARK_BLACK",
"DARK_BLUE",
@ -36,6 +36,8 @@ namespace ColorTool
"BRIGHT_WHITE"
};
public string Name => "INI File Parser";
static uint ParseHex(string arg)
{
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
@ -44,11 +46,12 @@ namespace ColorTool
static uint ParseRgb(string arg)
{
int[] components = { 0, 0, 0};
int[] components = { 0, 0, 0 };
string[] args = arg.Split(',');
if (args.Length != components.Length) 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]);
}

View file

@ -3,10 +3,12 @@
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static ColorTool.ConsoleAPI;
namespace ColorTool
{
@ -86,7 +88,8 @@ namespace ColorTool
static void Usage()
{
Console.WriteLine(Resources.Usage);
Console.WriteLine(Resources.Usage,
string.Join($"{Environment.NewLine} ", GetParsers().Select(p => p.Name)));
}
static void Version()
@ -136,7 +139,7 @@ namespace ColorTool
"46m",
"47m"
};
Console.Write("\t");
for (int bg = 0; bg < BGs.Length; bg++)
{
@ -259,8 +262,7 @@ namespace ColorTool
string schemeName = args[args.Length - 1];
uint[] colorTable = null;
ISchemeParser[] parsers = { new XmlSchemeParser(), new IniSchemeParser() };
foreach (var parser in parsers)
foreach (var parser in GetParsers())
{
uint[] table = parser.ParseScheme(schemeName);
if (table != null)
@ -286,5 +288,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.
// To add or remove a member, edit your .ResX file then rerun ResGen
// 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.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Resources {
@ -114,7 +114,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 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>
public static string Usage {
get {

View file

@ -151,7 +151,10 @@ 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.
-v, --version : Display the version number</value>
-v, --version : Display the version number
Available importers:
{0}</value>
</data>
<data name="WroteToDefaults" xml:space="preserve">
<value>Wrote selected scheme to the defaults.</value>

View file

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