allow scheme parsers to opt out of attempting to parse a file
This fixes the issue where the INI file parser can throw errors because it's attempting to parse an `.itermcolors` (xml) file.
This commit is contained in:
parent
05f518db5b
commit
b61cb830c3
|
@ -91,15 +91,10 @@ namespace ColorTool
|
|||
|
||||
public static ColorScheme GetScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
foreach (var parser in GetParsers())
|
||||
{
|
||||
ColorScheme scheme = parser.ParseScheme(schemeName, reportErrors);
|
||||
if (scheme != null)
|
||||
{
|
||||
return scheme;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return GetParsers()
|
||||
.Where(parser => parser.CanParse(schemeName))
|
||||
.Select(parser => parser.ParseScheme(schemeName, reportErrors))
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static IEnumerable<ISchemeParser> GetParsers()
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace ColorTool.SchemeParsers
|
|||
interface ISchemeParser
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
bool CanParse(string schemeName);
|
||||
ColorScheme ParseScheme(string schemeName, bool reportErrors = false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ namespace ColorTool.SchemeParsers
|
|||
[DllImport("kernel32")]
|
||||
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
|
||||
|
||||
private const string FileExtension = ".ini";
|
||||
|
||||
// These are in Windows Color table order - BRG, not RGB.
|
||||
internal static readonly IReadOnlyList<string> COLOR_NAMES = new[]
|
||||
{
|
||||
|
@ -41,6 +43,9 @@ namespace ColorTool.SchemeParsers
|
|||
|
||||
public string Name { get; } = "INI File Parser";
|
||||
|
||||
public bool CanParse(string schemeName) =>
|
||||
string.Equals(Path.GetExtension(schemeName), FileExtension, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
bool success = true;
|
||||
|
@ -182,7 +187,7 @@ namespace ColorTool.SchemeParsers
|
|||
|
||||
private static string FindIniScheme(string schemeName)
|
||||
{
|
||||
return SchemeManager.GetSearchPaths(schemeName, ".ini").FirstOrDefault(File.Exists);
|
||||
return SchemeManager.GetSearchPaths(schemeName, FileExtension).FirstOrDefault(File.Exists);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace ColorTool.SchemeParsers
|
|||
{
|
||||
class JsonParser : ISchemeParser
|
||||
{
|
||||
private const string FileExtension = ".json";
|
||||
private static IReadOnlyList<string> CONCFG_COLOR_NAMES = new[]
|
||||
{
|
||||
"black", // DARK_BLACK
|
||||
|
@ -37,9 +38,12 @@ namespace ColorTool.SchemeParsers
|
|||
|
||||
public string Name { get; } = "concfg Parser";
|
||||
|
||||
public bool CanParse(string schemeName) =>
|
||||
string.Equals(Path.GetExtension(schemeName), FileExtension, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
XmlDocument xmlDoc = loadJsonFile(schemeName);
|
||||
XmlDocument xmlDoc = LoadJsonFile(schemeName);
|
||||
if (xmlDoc == null) return null;
|
||||
|
||||
try
|
||||
|
@ -113,10 +117,10 @@ namespace ColorTool.SchemeParsers
|
|||
return RGB(col.R, col.G, col.B);
|
||||
}
|
||||
|
||||
private static XmlDocument loadJsonFile(string schemeName)
|
||||
private static XmlDocument LoadJsonFile(string schemeName)
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, ".json")
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, FileExtension)
|
||||
.Where(File.Exists))
|
||||
{
|
||||
try
|
||||
|
|
|
@ -40,9 +40,13 @@ namespace ColorTool.SchemeParsers
|
|||
private const string RED_KEY = "Red Component";
|
||||
private const string GREEN_KEY = "Green Component";
|
||||
private const string BLUE_KEY = "Blue Component";
|
||||
private const string FileExtension = ".itermcolors";
|
||||
|
||||
public string Name { get; } = "iTerm Parser";
|
||||
|
||||
public bool CanParse(string schemeName) =>
|
||||
string.Equals(Path.GetExtension(schemeName), FileExtension, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
XmlDocument xmlDoc = LoadXmlScheme(schemeName); // Create an XML document object
|
||||
|
@ -123,7 +127,7 @@ namespace ColorTool.SchemeParsers
|
|||
private static XmlDocument LoadXmlScheme(string schemeName)
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, ".itermcolors")
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, FileExtension)
|
||||
.Where(File.Exists))
|
||||
{
|
||||
try
|
||||
|
|
Loading…
Reference in a new issue