Skip to content

Commit 188885d

Browse files
committed
Big fixes, improvements, updates
1 parent f1733dd commit 188885d

File tree

3 files changed

+178
-185
lines changed

3 files changed

+178
-185
lines changed

SmartImage/Program.cs

Lines changed: 56 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ public static partial class Program
6060
/// </summary>
6161
internal static SearchClient Client { get; private set; } = new(Config);
6262

63+
private static Dictionary<string, string> desc = new()
64+
{
65+
["Ctrl"] = "Load direct",
66+
["Alt"] = "Show other",
67+
["Shift"] = "Open raw",
68+
["Alt+Ctrl"] = "Download",
69+
70+
};
71+
72+
private static Dictionary<string, string> desc2 = new()
73+
{
74+
75+
["F1"] = "Show filtered results",
76+
["F2"] = "Refine",
77+
["F5"] = "Refresh"
78+
};
6379

6480
/// <summary>
6581
/// Console UI for search results
@@ -68,14 +84,9 @@ public static partial class Program
6884
{
6985
Options = new List<ConsoleOption>(),
7086

71-
Description = "Press the result number to open in browser\n".AddColor(Elements.ColorOther) +
72-
$"{"Ctrl:".AddColor(Elements.ColorKey)} Load direct | " +
73-
$"{"Alt:".AddColor(Elements.ColorKey)} Show other | " +
74-
$"{"Shift:".AddColor(Elements.ColorKey)} Open raw | " +
75-
$"{"Alt+Ctrl:".AddColor(Elements.ColorKey)} Download\n" +
76-
$"{"F1:".AddColor(Elements.ColorKey)} Show filtered results | " +
77-
$"{"F2:".AddColor(Elements.ColorKey)} Refine | " +
78-
$"{"F5:".AddColor(Elements.ColorKey)} Refresh",
87+
Description = "Press the result number to open in browser\n".AddColor(Elements.ColorOther)
88+
+ Elements.GetMapString(desc, Elements.ColorKey) + '\n'
89+
+ Elements.GetMapString(desc2, Elements.ColorKey),
7990

8091
Functions = new()
8192
{
@@ -116,7 +127,12 @@ public static partial class Program
116127
await Client.RefineSearchAsync();
117128
}
118129
catch (Exception e) {
119-
qmsg($"Error: {e.Message.AddColor(Elements.ColorError)}");
130+
string s = $"Error: {e.Message.AddColor(Elements.ColorError)}";
131+
132+
Console.WriteLine(
133+
$"\n{Strings.Constants.CHEVRON} {s}");
134+
135+
ConsoleManager.WaitForTimeSpan(TimeSpan.FromSeconds(2));
120136

121137
ResultDialog.Options.Clear();
122138

@@ -131,14 +147,6 @@ public static partial class Program
131147
}
132148
};
133149

134-
private static void qmsg(string s)
135-
{
136-
Console.WriteLine(
137-
$"\n{Strings.Constants.CHEVRON} {s}");
138-
139-
ConsoleManager.WaitForTimeSpan(TimeSpan.FromSeconds(2));
140-
}
141-
142150
#endregion
143151

144152

@@ -182,9 +190,14 @@ private static async Task Main(string[] args)
182190
if (!await Cli.HandleArguments())
183191
return;
184192

185-
ResultDialog.Subtitle = $"SE: {Config.SearchEngines} " +
186-
$"| PE: {Config.PriorityEngines} " +
187-
$"| Filtering: {Elements.ToToggleString(Config.Filtering)}";
193+
var map = new Dictionary<string, string>()
194+
{
195+
["SE"] = Config.SearchEngines.ToString(),
196+
["PE"] = Config.PriorityEngines.ToString(),
197+
["Filtering"] = Elements.GetToggleString(Config.Filtering)
198+
};
199+
200+
ResultDialog.Subtitle = Elements.GetMapString(map);
188201

189202

190203
_ctsSearch = new();
@@ -202,7 +215,6 @@ private static async Task Main(string[] args)
202215

203216
ConsoleManager.UI.ProgressIndicator.Instance.Start(_ctsProgress);
204217

205-
206218
// Show results
207219
_searchTask = Client.RunSearchAsync(_ctsSearch.Token);
208220
_continueTask = Client.RunContinueAsync(_ctsContinue.Token);
@@ -213,9 +225,7 @@ private static async Task Main(string[] args)
213225
ResultDialog.Options.Add(_originalResult);
214226

215227
await ResultDialog.ReadInputAsync(_ctsReadInput.Token);
216-
217228

218-
// if (!Config.OutputOnly) { }
219229

220230
await _searchTask;
221231

@@ -226,75 +236,23 @@ private static async Task Main(string[] args)
226236
//ignored
227237
}
228238

229-
230239
/*Client.Dispose();
231240
Client.Reset();
232241
*/
233242

234-
//todo
235-
Debug.WriteLine("done");
236-
237-
// ResultDialog.Display(false);
238-
239-
// if (Config.OutputOnly) { }
240-
241-
242243
}
243244

244245

245246
#region Event handlers
246247

247-
private static void OnCancel(object o, ConsoleCancelEventArgs eventArgs)
248+
private static void OnCancel(object sender, ConsoleCancelEventArgs eventArgs)
248249
{
249-
/*ResultDialog.Options.Clear();
250-
ResultDialog.Options.Add(_originalResult);*/
251-
252-
253-
// ResultDialog.Options.Clear();
254-
// ResultDialog.Refresh();
255250
_ctsSearch.Cancel();
256251
_ctsContinue.Cancel();
257252
// _ctsReadInput.Cancel();
258253

259254
eventArgs.Cancel = true;
260-
261-
// _cancellationTokenSource = new();
262-
263-
// ResultDialog.Refresh();
264-
// await ResultDialog.ReadInputAsync();
265-
266255
SystemSounds.Hand.Play();
267-
268-
// var x = cd.ReadInputAsync();
269-
// x.Wait();
270-
// Debug.WriteLine($"{ResultDialog.Options.Count}|{Client.AllResults.Count}");
271-
// ResultDialog = cd;
272-
273-
var results = Client.Results;
274-
275-
var options = results.Select(x =>
276-
{
277-
return x.GetConsoleOption();
278-
}).ToArray();
279-
280-
var cd = new ConsoleDialog()
281-
{
282-
Description = ResultDialog.Description,
283-
Header = ResultDialog.Header,
284-
Options = options,
285-
Subtitle = ResultDialog.Subtitle,
286-
};
287-
// cd.ReadInput();
288-
289-
/*ResultDialog.Options.Clear();
290-
291-
foreach (ConsoleOption t in options) {
292-
ResultDialog.Options.Add(t);
293-
}*/
294-
// await cd.ReadInputAsync();
295-
// ResultDialog.Display(true);
296-
qmsg("Cancelled");
297-
298256
}
299257

300258
private static void OnResultUpdated(object sender, EventArgs result)
@@ -345,28 +303,41 @@ private static void OnResultCompleted(object sender, ResultCompletedEventArgs ev
345303
option.Function();
346304
}
347305

348-
var status = $"Results: {Client.Results.Count}";
306+
var map = new Dictionary<string, string>()
307+
{
308+
["Results"] = Client.Results.Count.ToString(),
309+
};
349310

350311
if (Config.Filtering) {
351-
status += $" | Filtered: {Client.FilteredResults.Count}";
312+
map.Add("Filtered", Client.FilteredResults.Count.ToString());
352313
}
353314

354-
status += $" | Pending: {Client.PendingCount}";
315+
map.Add("Pending", Client.PendingCount.ToString());
355316

356-
ResultDialog.Status = status;
317+
string s;
318+
319+
if (_ctsSearch.IsCancellationRequested) {
320+
s = "Cancelled";
321+
}
322+
else if (Client.IsComplete) {
323+
s = "Complete";
324+
}
325+
else {
326+
s = "Searching";
327+
}
357328

358-
/*GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
359-
GC.Collect();*/
329+
map.Add("Status", s);
360330

331+
var status = Elements.GetMapString(map);
332+
333+
ResultDialog.Status = status;
361334
}
362335

363336
#endregion
364-
private static CancellationTokenSource _ctsProgress;
365337

338+
private static CancellationTokenSource _ctsProgress;
366339
private static CancellationTokenSource _ctsReadInput;
367-
368340
private static CancellationTokenSource _ctsContinue;
369-
370341
private static CancellationTokenSource _ctsSearch;
371342

372343
private static bool _isFilteredShown;
Lines changed: 15 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
1+
using System.Diagnostics;
42
using System.Drawing;
53
using System.Reflection;
64
using System.Text;
@@ -9,8 +7,8 @@
97
using Kantan.Cli.Controls;
108
using Kantan.Text;
119
using Kantan.Utilities;
12-
using Novus.Utilities;
1310
using Novus.OS.Win32;
11+
using Novus.Utilities;
1412
using SmartImage.Core;
1513
using SmartImage.Lib.Engines;
1614

@@ -55,7 +53,8 @@ internal static class Elements
5553

5654
#region Constants
5755

58-
private static readonly Encoding CodePage437 = CodePagesEncodingProvider.Instance.GetEncoding((int) Native.CodePages.CP_IBM437);
56+
private static readonly Encoding CodePage437 =
57+
CodePagesEncodingProvider.Instance.GetEncoding((int) Native.CodePages.CP_IBM437);
5958

6059
private static readonly string CheckMark =
6160
Strings.EncodingConvert(Encoding.Unicode, CodePage437, Strings.Constants.CHECK_MARK.ToString());
@@ -64,101 +63,27 @@ internal static class Elements
6463

6564
private static readonly string Disabled = Strings.Constants.MUL_SIGN.ToString().AddColor(ColorNo);
6665

67-
internal static string ToToggleString(bool b) => b ? Enabled : Disabled;
68-
69-
internal static string ToVersionString(Version v) => $"{v.Major}.{v.Minor}.{v.Build}";
70-
7166
#endregion
7267

73-
[UsedImplicitly]
74-
internal static string GetName(string s, bool added) => $"{s} ({(ToToggleString(added))})";
75-
76-
internal static ConsoleOption CreateEnumConfigOption<T>(string f, string name, object o) where T : Enum
77-
{
78-
return new()
79-
{
80-
Name = name,
81-
Color = Elements.ColorOther,
82-
Function = () =>
83-
{
84-
var enumOptions = ConsoleOption.FromEnum<T>();
85-
86-
var selected = (new ConsoleDialog
87-
{
88-
Options = enumOptions,
89-
SelectMultiple = true
90-
}).ReadInput();
91-
92-
var enumValue = EnumHelper.ReadFromSet<T>(selected.Output);
93-
var field = o.GetType().GetAnyResolvedField(f);
94-
field.SetValue(o, enumValue);
95-
96-
Console.WriteLine(enumValue);
97-
98-
ConsoleManager.WaitForSecond();
99-
100-
Debug.Assert(((T) field.GetValue(o)).Equals(enumValue));
101-
102-
AppConfig.UpdateConfig();
103-
104-
return null;
105-
}
106-
};
107-
}
108-
109-
internal static ConsoleOption CreateConfigOption(PropertyInfo member, string name, Action<bool> fn, object o)
110-
{
111-
bool initVal = (bool) member.GetValue(o);
112-
113-
var option = new ConsoleOption
114-
{
115-
Name = Elements.GetName(name, initVal)
116-
};
117-
118-
option.Function = () =>
119-
{
120-
var pi = member.DeclaringType.GetProperty(member.Name);
121-
bool curVal = (bool) pi.GetValue(null);
122-
fn(curVal);
123-
bool newVal = (bool) pi.GetValue(null);
124-
option.Name = Elements.GetName(name, newVal);
68+
12569

126-
Debug.Assert((bool) pi.GetValue(null) == newVal);
12770

128-
return null;
129-
};
71+
internal static string GetToggleString(bool b) => b ? Enabled : Disabled;
13072

131-
return option;
132-
}
73+
internal static string GetVersionString(Version v) => $"{v.Major}.{v.Minor}.{v.Build}";
13374

134-
internal static ConsoleOption CreateConfigOption(string field, string name, object t)
75+
internal static string GetMapString(Dictionary<string, string> map, Color? c = null)
13576
{
136-
bool initVal = (bool) (t).GetType().GetAnyResolvedField(field).GetValue(t);
137-
138-
ConsoleOption option = new ConsoleOption
77+
return map.Select(kv =>
13978
{
140-
Name = Elements.GetName(name, initVal)
141-
};
142-
143-
option.Function = () =>
144-
{
145-
var fi = t.GetType().GetAnyResolvedField(field);
146-
object curVal = fi.GetValue(t);
147-
bool newVal = !(bool) curVal;
148-
fi.SetValue(t, newVal);
149-
150-
151-
option.Name = Elements.GetName(name, newVal);
152-
153-
Debug.Assert((bool) fi.GetValue(t) == newVal);
154-
155-
AppConfig.UpdateConfig();
156-
157-
return null;
158-
};
79+
var key = kv.Key;
15980

81+
if (c.HasValue) {
82+
key = key.AddColor(c.Value);
83+
}
16084

161-
return option;
85+
return $"{key}: {kv.Value}";
86+
}).QuickJoin(" | ");
16287
}
16388
}
16489
}

0 commit comments

Comments
 (0)