-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandsReader.cs
167 lines (130 loc) · 4.51 KB
/
CommandsReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using mroot_lib;
namespace Wox.Plugin.Symbols
{
public class SymbolSettings
{
public string CharacterSet { get; set; }
public bool Enabled { get; set; }
}
public class CommandsReader
{
#region api
public List<SymbolDescriptor> ReadSymbolsFromConfig(string configFolder)
{
List<SymbolDescriptor> result = new List<SymbolDescriptor>();
DirectoryInfo directory = new DirectoryInfo(configFolder);
FileInfo[] Files = directory.GetFiles("*.ini");
foreach (FileInfo file in Files)
{
List<string> lines = File.ReadAllLines(file.FullName).ToList();
List<string> trimmedLines = FilterRegularLines(lines);
result.AddRange(GetSymbolsFromLines(trimmedLines));
}
return result;
}
#endregion
#region private methods
private List<string> FilterRegularLines(List<string> lines)
{
List<string> result = new List<string>();
foreach (string line in lines)
{
var trimmedLine = line.Trim();
if (trimmedLine.IsNullOrWhitespace())
{
continue;
}
if (trimmedLine.StartsWith("#") || trimmedLine.StartsWith(";") || trimmedLine.StartsWith("//"))
{
continue;
}
result.Add(trimmedLine);
}
return result;
}
private List<string> GetSection(string sectionName, List<string> lines)
{
List<string> result = new List<string>();
bool inSettingsSection = false;
foreach (string line in lines)
{
if (line.StartsWith($"[{sectionName}]"))
{
inSettingsSection = true;
continue;
}
if (line.StartsWith("["))
{
inSettingsSection = false;
continue;
}
if (inSettingsSection)
{
result.Add(line);
}
}
return result;
}
private List<SymbolDescriptor> GetSymbolsFromLines(List<string> lines)
{
Settings = GetSymbolSettings(GetSection("settings", lines));
return GetSymbolsActions(GetSection("symbols", lines));
}
private List<string> GetSymbols(string text)
{
string[] separtator = { "," };
return text.Split(separtator, StringSplitOptions.RemoveEmptyEntries).ToList();
}
private List<SymbolDescriptor> GetSymbolsActions(List<string> lines)
{
List<SymbolDescriptor> results = new List<SymbolDescriptor>();
foreach (string line in lines)
{
string[] parts = line.Split(new[] { '=' }, 2);
if (parts.Length != 2)
{
continue;
}
string keyword = parts[0].Trim();
string symbolsText = parts[1].Trim();
SymbolDescriptor action = new SymbolDescriptor
{
Keyword = keyword,
Symbols = GetSymbols(symbolsText),
CharacterSet = Settings.CharacterSet,
Enabled = Settings.Enabled
};
results.Add(action);
}
return results;
}
private SymbolSettings GetSymbolSettings(List<string> lines)
{
SymbolSettings result = new SymbolSettings();
foreach (var line in lines)
{
var splitIndex = line.IndexOf('=');
var key = line.Substring(0, splitIndex);
var value = line.Substring(splitIndex + 1, line.Length - splitIndex - 1);
if (key.Equals("enabled"))
{
result.Enabled = Boolean.Parse(value.Trim());
}
else if (key.Equals("character_set"))
{
result.CharacterSet = value.Trim();
}
}
return result;
}
#endregion
#region members
private SymbolSettings Settings { get; set; }
#endregion
}
}