Skip to content

Commit 10170f1

Browse files
committed
Result filtering
1 parent 50212ad commit 10170f1

File tree

16 files changed

+220
-133
lines changed

16 files changed

+220
-133
lines changed

SmartImage/Core/ConfigComponents.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ internal static class ConfigComponents
1919
/*
2020
* Handles config components (properties or fields).
2121
*
22-
* A config component is a setting (i.e. field, parameter, etc) within the config.
22+
*
23+
* A config component is a setting/option (i.e. field, parameter, etc).
24+
* - Its value can be stored and later retrieved from a config file.
25+
* - Its value can also be specified through the command line.
26+
*
2327
*
2428
* mname - Member name
2529
* id - Map name (Id)

SmartImage/Core/Info.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public static string AppFolder
9191

9292
public static bool IsAppFolderInPath => OS.IsFolderInPath(AppFolder);
9393

94-
94+
/// <summary>
95+
/// Setup
96+
/// </summary>
9597
public static void Setup()
9698
{
9799
if (!OperatingSystem.IsWindows()) {

SmartImage/Core/Integration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
4646
}
4747
catch (Exception ex) {
4848
NConsole.WriteError("{0}", ex.Message);
49-
NConsoleIO.WaitForInput();
49+
NConsole.WaitForInput();
5050
return false;
5151
}
5252
finally {
@@ -74,7 +74,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
7474
}
7575
catch (Exception ex) {
7676
NConsole.WriteError("{0}", ex.Message);
77-
NConsoleIO.WaitForInput();
77+
NConsole.WaitForInput();
7878
return false;
7979
}
8080

SmartImage/Core/Interface.cs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static NConsoleOption[] AllOptions
5656
/// <remarks>
5757
/// User-friendly menu
5858
/// </remarks>
59-
internal static void Run() => NConsoleIO.ReadOptions(MainMenu);
59+
internal static void Run() => NConsole.ReadOptions(MainMenu);
6060

6161

6262
/// <summary>
@@ -96,6 +96,9 @@ private static NConsoleOption[] AllOptions
9696
/// </summary>
9797
internal const int ConsoleWindowHeight = 50;
9898

99+
/// <summary>
100+
/// Main option
101+
/// </summary>
99102
private static readonly NConsoleOption RunSelectImage = new()
100103
{
101104
Name = ">>> Select image <<<",
@@ -104,11 +107,11 @@ private static NConsoleOption[] AllOptions
104107
{
105108
Console.WriteLine("Drag and drop the image here.");
106109

107-
string? img = NConsoleIO.ReadInput("Image");
110+
string? img = NConsole.ReadInput("Image");
108111

109112
if (String.IsNullOrWhiteSpace(img)) {
110113
NConsole.WriteError("Invalid image");
111-
NConsoleIO.WaitForInput();
114+
NConsole.WaitForInput();
112115
return null;
113116
}
114117

@@ -128,15 +131,15 @@ private static NConsoleOption[] AllOptions
128131
Function = () =>
129132
{
130133
var rgEnum = NConsoleOption.FromEnum<SearchEngineOptions>();
131-
var values = NConsoleIO.ReadOptions(rgEnum, true);
134+
var values = NConsole.ReadOptions(rgEnum, true);
132135

133136
var newValues = Enums.ReadFromSet<SearchEngineOptions>(values);
134137

135138
NConsole.WriteSuccess(newValues);
136139

137140
SearchConfig.Config.SearchEngines = newValues;
138141

139-
NConsoleIO.WaitForSecond();
142+
NConsole.WaitForSecond();
140143
return null;
141144
}
142145
};
@@ -149,15 +152,15 @@ private static NConsoleOption[] AllOptions
149152
Function = () =>
150153
{
151154
var rgEnum = NConsoleOption.FromEnum<SearchEngineOptions>();
152-
var values = NConsoleIO.ReadOptions(rgEnum, true);
155+
var values = NConsole.ReadOptions(rgEnum, true);
153156

154157
var newValues = Enums.ReadFromSet<SearchEngineOptions>(values);
155158

156159
NConsole.WriteSuccess(newValues);
157160

158161
SearchConfig.Config.PriorityEngines = newValues;
159162

160-
NConsoleIO.WaitForSecond();
163+
NConsole.WaitForSecond();
161164
return null;
162165
}
163166
};
@@ -169,9 +172,9 @@ private static NConsoleOption[] AllOptions
169172
Color = ColorConfig,
170173
Function = () =>
171174
{
172-
SearchConfig.Config.SauceNaoAuth = NConsoleIO.ReadInput("API key");
175+
SearchConfig.Config.SauceNaoAuth = NConsole.ReadInput("API key");
173176

174-
NConsoleIO.WaitForSecond();
177+
NConsole.WaitForSecond();
175178
return null;
176179
}
177180
};
@@ -183,13 +186,36 @@ private static NConsoleOption[] AllOptions
183186
Function = () =>
184187
{
185188

186-
SearchConfig.Config.ImgurAuth = NConsoleIO.ReadInput("API key");
189+
SearchConfig.Config.ImgurAuth = NConsole.ReadInput("API key");
187190

188-
NConsoleIO.WaitForSecond();
191+
NConsole.WaitForSecond();
189192
return null;
190193
}
191194
};
192195

196+
private static readonly NConsoleOption ConfigAutoFilter = new()
197+
{
198+
Name = GetAutoFilterString(),
199+
Color = ColorConfig,
200+
Function = () =>
201+
{
202+
203+
SearchConfig.Config.FilterResults = !SearchConfig.Config.FilterResults;
204+
ConfigAutoFilter.Name= GetAutoFilterString();
205+
return null;
206+
}
207+
};
208+
209+
private static string GetAutoFilterString()
210+
{
211+
//var x = SearchConfig.Config.FilterResults
212+
// ? Formatting.CHECK_MARK.ToString()
213+
// : Formatting.BALLOT_X.ToString();
214+
215+
var x = SearchConfig.Config.FilterResults;
216+
return $"Filter results: {x}";
217+
}
218+
193219
private static readonly NConsoleOption ConfigUpdateOption = new()
194220
{
195221
Name = "Update configuration file",
@@ -198,20 +224,20 @@ private static NConsoleOption[] AllOptions
198224
{
199225
SearchConfig.Config.SaveFile();
200226

201-
NConsoleIO.WaitForSecond();
227+
NConsole.WaitForSecond();
202228
return null;
203229
}
204230
};
205231

206232
private static readonly NConsoleOption ShowInfoOption = new()
207233
{
208-
Name = "Show info",
234+
Name = "Show info and config",
209235
Color = ColorMisc,
210236
Function = () =>
211237
{
212238
Info.ShowInfo();
213239

214-
NConsoleIO.WaitForInput();
240+
NConsole.WaitForInput();
215241
return null;
216242
}
217243
};
@@ -224,22 +250,17 @@ private static NConsoleOption[] AllOptions
224250
Function = () =>
225251
{
226252
bool ctx = Integration.IsContextMenuAdded;
227-
bool added;
228253

229-
if (!ctx) {
230-
Integration.HandleContextMenu(IntegrationOption.Add);
231-
NConsole.WriteSuccess("Added to context menu");
232-
added = true;
233-
}
234-
else {
235-
Integration.HandleContextMenu(IntegrationOption.Remove);
236-
NConsole.WriteSuccess("Removed from context menu");
237-
added = false;
238-
}
254+
var io = !ctx ? IntegrationOption.Add : IntegrationOption.Remove;
255+
256+
Integration.HandleContextMenu(io);
257+
bool added = io == IntegrationOption.Add;
258+
259+
NConsole.WriteInfo($"Context menu integrated: {added}");
239260

240261
ContextMenuOption.Name = GetContextMenuString(added);
241262

242-
NConsoleIO.WaitForSecond();
263+
NConsole.WaitForSecond();
243264
return null;
244265
}
245266
};
@@ -256,7 +277,7 @@ private static string GetContextMenuString(bool added) =>
256277
{
257278
UpdateInfo.AutoUpdate();
258279

259-
NConsoleIO.WaitForSecond();
280+
NConsole.WaitForSecond();
260281
return null;
261282
}
262283
};
@@ -269,7 +290,7 @@ private static string GetContextMenuString(bool added) =>
269290
{
270291
Integration.ResetIntegrations();
271292

272-
NConsoleIO.WaitForSecond();
293+
NConsole.WaitForSecond();
273294
return null;
274295
}
275296
};
@@ -285,7 +306,7 @@ private static string GetContextMenuString(bool added) =>
285306
bool ok = LegacyIntegration.LegacyCleanup();
286307

287308
NConsole.WriteInfo($"Legacy cleanup: {ok}");
288-
NConsoleIO.WaitForInput();
309+
NConsole.WaitForInput();
289310

290311
return null;
291312
}
@@ -335,7 +356,7 @@ private static string GetContextMenuString(bool added) =>
335356

336357
var rgOption = NConsoleOption.FromArray(TestImages, s => s);
337358

338-
var testImg = (string) NConsoleIO.ReadOptions(rgOption).First();
359+
var testImg = (string) NConsole.ReadOptions(rgOption).First();
339360

340361
var img = Path.Combine(cd2, testImg);
341362

SmartImage/Core/LegacyIntegration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
9090
}
9191
catch (Exception e) {
9292
NConsole.WriteError("Context menu error: {0}", e.Message);
93-
NConsoleIO.WaitForSecond();
93+
NConsole.WaitForSecond();
9494
return false;
9595
}
9696
}
@@ -122,7 +122,7 @@ internal static bool LegacyCleanup()
122122
Integration.HandleContextMenu(IntegrationOption.Add);
123123

124124
NConsole.WriteSuccess("Added new context menu");
125-
NConsoleIO.WaitForSecond();
125+
NConsole.WaitForSecond();
126126
}
127127

128128
return true;

SmartImage/Core/SearchConfig.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
namespace SmartImage.Core
2020
{
2121
/// <summary>
22-
/// Search config
22+
/// Search configuration and options
2323
/// </summary>
2424
/// <remarks>
25-
/// Config is read from config file (<see cref="ConfigLocation" />) or from specified arguments.
25+
/// Config is read from config file (<see cref="ConfigLocation" />) first, then from specified command line arguments.
2626
/// </remarks>
2727
/// <seealso cref="ConfigComponents" />
2828
public sealed class SearchConfig
@@ -42,7 +42,6 @@ private SearchConfig()
4242
// Read config from command line arguments
4343
ReadFromArguments();
4444

45-
4645
// Setup
4746
EnsureConfig();
4847
}
@@ -81,15 +80,20 @@ private SearchConfig()
8180
[field: ConfigComponent("saucenao_key", "--imgur-auth", Strings.Empty)]
8281
public string SauceNaoAuth { get; set; }
8382

84-
83+
/// <summary>
84+
/// Does not open results from priority engines if the result similarity (if available) is below a certain threshold.
85+
/// </summary>
86+
[field: ConfigComponent("filter_results", "--filter-results", true, true)]
87+
public bool FilterResults { get; set; }
88+
8589
/// <summary>
8690
/// Whether to save passed in arguments (via CLI) to the config file upon exit
8791
/// </summary>
8892
public bool UpdateConfig { get; set; }
8993

9094

9195
/// <summary>
92-
/// The image we are searching for
96+
/// Image
9397
/// </summary>
9498
public string Image { get; set; }
9599

@@ -98,6 +102,9 @@ private SearchConfig()
98102
/// </summary>
99103
public static string ConfigLocation => Path.Combine(Info.AppFolder, Info.NAME_CFG);
100104

105+
/// <summary>
106+
/// Read configuration from file (<see cref="ConfigLocation"/>)
107+
/// </summary>
101108
private void ReadFromFile()
102109
{
103110
bool newCfg = false;
@@ -123,7 +130,9 @@ private void ReadFromFile()
123130
Image = String.Empty;
124131
}
125132

126-
133+
/// <summary>
134+
/// Reset configuration to defaults
135+
/// </summary>
127136
public void Reset() => ConfigComponents.ResetComponents(this);
128137

129138

@@ -149,7 +158,7 @@ public void EnsureConfig()
149158
if (illegalOptions != 0) {
150159
NConsole.WriteError($"Search engine option {illegalOptions} cannot be used for search engine options");
151160

152-
NConsoleIO.WaitForSecond();
161+
NConsole.WaitForSecond();
153162

154163
// Clear illegal options
155164
SearchEngines &= ~illegalOptions;
@@ -158,7 +167,7 @@ public void EnsureConfig()
158167
// Special case
159168
if (SearchEngines == SearchEngineOptions.None) {
160169
NConsole.WriteInfo("Reverting search engine options to default");
161-
NConsoleIO.WaitForSecond();
170+
NConsole.WaitForSecond();
162171
ConfigComponents.ResetComponent(this, nameof(SearchEngines));
163172
}
164173

@@ -197,6 +206,7 @@ public override string ToString()
197206
sb.AppendFormat("Imgur authentication: {0}\n", imgurAuth);
198207
}
199208

209+
sb.Append($"Auto filtering: {FilterResults}\n");
200210

201211
sb.AppendFormat("Image upload service: {0}\n",
202212
imgurNull ? "ImgOps" : "Imgur");
@@ -209,16 +219,13 @@ public override string ToString()
209219

210220

211221
/// <summary>
212-
/// Parse config arguments and options
222+
/// Read config from command line arguments
213223
/// </summary>
214224
private void ReadFromArguments()
215225
{
216226
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
217227

218-
219-
bool noArgs = args.Length == 0;
220-
221-
if (noArgs) {
228+
if (!args.Any()) {
222229
NoArguments = true;
223230
return;
224231
}

0 commit comments

Comments
 (0)