From 9bd6053664fda6a96cc7047b82274315f7601287 Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Mon, 28 Aug 2017 10:06:45 -0700 Subject: [PATCH] Dynamically generate list of parsers --- tools/ColorTool/ColorTool/ISchemeParser.cs | 2 ++ tools/ColorTool/ColorTool/IniSchemeParser.cs | 9 ++++++--- tools/ColorTool/ColorTool/Program.cs | 20 +++++++++++++------ .../ColorTool/ColorTool/Resources.Designer.cs | 4 ++-- tools/ColorTool/ColorTool/Resources.resx | 5 ++++- tools/ColorTool/ColorTool/XmlSchemeParser.cs | 2 ++ 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tools/ColorTool/ColorTool/ISchemeParser.cs b/tools/ColorTool/ColorTool/ISchemeParser.cs index df8cfcec2..150ca35cc 100644 --- a/tools/ColorTool/ColorTool/ISchemeParser.cs +++ b/tools/ColorTool/ColorTool/ISchemeParser.cs @@ -7,6 +7,8 @@ namespace ColorTool { interface ISchemeParser { + string Name { get; } + uint[] ParseScheme(string schemeName); } } diff --git a/tools/ColorTool/ColorTool/IniSchemeParser.cs b/tools/ColorTool/ColorTool/IniSchemeParser.cs index 3b1789f57..80ea47f13 100644 --- a/tools/ColorTool/ColorTool/IniSchemeParser.cs +++ b/tools/ColorTool/ColorTool/IniSchemeParser.cs @@ -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]); } diff --git a/tools/ColorTool/ColorTool/Program.cs b/tools/ColorTool/ColorTool/Program.cs index d65507d51..00c0a5d08 100644 --- a/tools/ColorTool/ColorTool/Program.cs +++ b/tools/ColorTool/ColorTool/Program.cs @@ -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 GetParsers() + { + return typeof(Program).Assembly.GetTypes() + .Where(t => !t.IsAbstract && typeof(ISchemeParser).IsAssignableFrom(t)) + .Select(t => (ISchemeParser)Activator.CreateInstance(t)); + } } } diff --git a/tools/ColorTool/ColorTool/Resources.Designer.cs b/tools/ColorTool/ColorTool/Resources.Designer.cs index e053d6d11..a609f9af4 100644 --- a/tools/ColorTool/ColorTool/Resources.Designer.cs +++ b/tools/ColorTool/ColorTool/Resources.Designer.cs @@ -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'll need to open the properties sheet and hit "Ok". ///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]";. + ///Feel free to add your own preferred scheme to that dire [rest of string was truncated]";. /// public static string Usage { get { diff --git a/tools/ColorTool/ColorTool/Resources.resx b/tools/ColorTool/ColorTool/Resources.resx index 503cdd2f8..dc5bee9ed 100644 --- a/tools/ColorTool/ColorTool/Resources.resx +++ b/tools/ColorTool/ColorTool/Resources.resx @@ -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 + -v, --version : Display the version number + +Available importers: + {0} Wrote selected scheme to the defaults. diff --git a/tools/ColorTool/ColorTool/XmlSchemeParser.cs b/tools/ColorTool/ColorTool/XmlSchemeParser.cs index a28aea54e..d1b1424b1 100644 --- a/tools/ColorTool/ColorTool/XmlSchemeParser.cs +++ b/tools/ColorTool/ColorTool/XmlSchemeParser.cs @@ -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;