Skip to content

Commit 947e039

Browse files
committed
Filtering
1 parent d500f26 commit 947e039

File tree

6 files changed

+143
-50
lines changed

6 files changed

+143
-50
lines changed

SmartImage 3/Mode/CliMode.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using JetBrains.Annotations;
1010
using SmartImage.Lib;
1111
using SmartImage.Lib.Results;
12+
using SmartImage.Lib.Utilities;
1213
using SmartImage.Utilities;
1314
using Spectre.Console;
1415
using Spectre.Console.Rendering;
@@ -30,8 +31,6 @@ public sealed class CliMode : IDisposable, IMode, IProgress<int>
3031

3132
private SearchQuery m_query;
3233

33-
private SearchResult[] m_results2;
34-
3534
public SearchConfig Config { get; }
3635
private const int COMPLETE = 100;
3736

@@ -133,15 +132,14 @@ void OnResult(object sender, SearchResult sr)
133132
public void Dispose()
134133
{
135134
m_results.Clear();
136-
Array.Clear(m_results2);
137135
m_cts.Dispose();
138136
m_query.Dispose();
139137
m_client.Dispose();
140138
}
141139

142140
public async Task<object?> RunAsync(object? c)
143141
{
144-
var cstr = (string) c;
142+
var cstr = (string?) c;
145143
Debug.WriteLine($"Input: {cstr}");
146144

147145
await AConsole.Progress().AutoRefresh(true).StartAsync(async ctx =>
@@ -161,6 +159,10 @@ await AConsole.Progress().AutoRefresh(true).StartAsync(async ctx =>
161159
p.IsIndeterminate = true;
162160
var url = await m_query.UploadAsync();
163161

162+
if (url == null) {
163+
throw new SmartImageException();//todo
164+
}
165+
164166
p.Increment(COMPLETE);
165167
ctx.Refresh();
166168
});

SmartImage 3/Mode/Shell/ShellMode.Handlers.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22
// 2023-02-14 @ 12:13 AM
33

44
using Color = Terminal.Gui.Color;
5-
using System.Collections;
65
using System.Collections.Concurrent;
7-
using System.Data;
86
using System.Diagnostics;
9-
using System.Reflection;
10-
using AngleSharp.Dom;
117
using Kantan.Net.Utilities;
128
using Kantan.Text;
13-
using Microsoft.Extensions.Logging;
149
using Microsoft.VisualBasic.FileIO;
15-
using Novus.OS;
1610
using NStack;
1711
using SmartImage.App;
1812
using SmartImage.Lib;
1913
using SmartImage.Mode.Shell.Assets;
2014
using Terminal.Gui;
2115
using Clipboard = Novus.Win32.Clipboard;
22-
using Microsoft.VisualBasic.FileIO;
2316
using Novus.FileTypes;
2417
using Novus.Win32.Structures.User32;
2518
using SmartImage.Lib.Engines;
2619
using SmartImage.Lib.Results;
27-
using SmartImage.Lib.Utilities;
2820
using Attribute = Terminal.Gui.Attribute;
29-
using Command = Terminal.Gui.Command;
3021
using FileSystem = Novus.OS.FileSystem;
3122

3223
namespace SmartImage.Mode.Shell;
@@ -177,6 +168,66 @@ private async void Run_Clicked()
177168
await RunMainAsync();
178169
}
179170

171+
private static int filterOrder = 0;
172+
private const int filterMax = 3;
173+
174+
private static Func<SearchResultItem, bool>[] funcs = new[]
175+
{
176+
(SearchResultItem x) =>
177+
{
178+
return x.Score >= 3;
179+
},
180+
(SearchResultItem x) =>
181+
{
182+
return SearchQuery.IsValidSourceType(x.Url);
183+
},
184+
185+
};
186+
187+
private async void Filter_Clicked()
188+
{
189+
190+
var res = m_results
191+
.Where(r => r.Status == SearchResultStatus.Success || r.Status == SearchResultStatus.None)
192+
.SelectMany(r => r.Results);
193+
194+
filterOrder = Math.Clamp(++filterOrder, 0, filterMax);
195+
Btn_Filter.Text = $"Filter {filterOrder}";
196+
197+
for (int j = 0; j < filterOrder - (funcs.Length - 1); j++) {
198+
res = res.Where(funcs[j]);
199+
}
200+
201+
if (filterOrder == filterMax) {
202+
var res3 = res as List<SearchResultItem> ?? res.ToList();
203+
var res2 = new ConcurrentBag<SearchResultItem>();
204+
205+
await Parallel.ForEachAsync(res3, async (item, token) =>
206+
{
207+
var us = await item.GetUniAsync().ConfigureAwait(false);
208+
209+
if (us) {
210+
res2.Add(item);
211+
212+
}
213+
});
214+
res = res2;
215+
216+
filterOrder = 0;
217+
}
218+
219+
IndexColors.Clear();
220+
Dt_Results.Clear();
221+
222+
int i = 0;
223+
224+
foreach (var sri in res) {
225+
add(sri, i);
226+
i++;
227+
Tv_Results.Update();
228+
}
229+
}
230+
180231
private void Queue_Checked(bool b)
181232
{
182233
QueueMode = !b;

SmartImage 3/Mode/Shell/ShellMode.cs

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Attribute = Terminal.Gui.Attribute;
2727
using Clipboard = Novus.Win32.Clipboard;
2828
using Window = Terminal.Gui.Window;
29+
using Kantan.Monad;
2930

3031
// ReSharper disable IdentifierTypo
3132

@@ -237,6 +238,16 @@ static ShellMode() { }
237238

238239
};
239240

241+
private static readonly Button Btn_Filter = new("Filter")
242+
{
243+
X = Pos.Right(Btn_Queue),
244+
Y = Pos.Y(Btn_Queue),
245+
246+
Height = Dim.Height(Btn_Run),
247+
ColorScheme = UI.Cs_Btn1
248+
249+
};
250+
240251
private static readonly Button Btn_Delete = new("Delete")
241252
{
242253
X = Pos.X(Btn_Cancel),
@@ -304,15 +315,15 @@ public bool UseClipboard
304315
}
305316
else {
306317
m_cbCallbackTok = Application.MainLoop.RemoveTimeout(m_cbCallbackTok);
307-
m_clipboard?.Clear();
318+
m_clipboard.Clear();
308319
}
309320

310321
}
311322
}
312323

313324
public bool QueueMode { get; private set; }
314325

315-
public readonly ConcurrentQueue<string> Queue;
326+
internal readonly ConcurrentQueue<string> Queue;
316327

317328
public SearchQuery Query { get; internal set; }
318329

@@ -505,10 +516,12 @@ public ShellMode(string[] args)
505516
Cb_Queue.Checked = QueueMode;
506517
Btn_Next.Enabled = QueueMode;
507518

519+
Btn_Filter.Clicked += Filter_Clicked;
520+
508521
Win.Add(Lbl_Input, Tf_Input, Btn_Run, Lbl_InputOk,
509522
Btn_Clear, Tv_Results, Pbr_Status, Lbl_InputInfo, Lbl_QueryUpload,
510523
Btn_Restart, Btn_Config, Lbl_InputInfo2, Btn_Cancel, Lbl_Status, Btn_Browse,
511-
Lbl_Status2, Btn_Delete, Btn_Queue, Cb_Queue, Btn_Next
524+
Lbl_Status2, Btn_Delete, Btn_Queue, Cb_Queue, Btn_Next, Btn_Filter
512525
);
513526

514527
Top.Add(Win);
@@ -573,52 +586,67 @@ private void OnResult(object o, SearchResult result)
573586

574587
Application.MainLoop.Invoke(() =>
575588
{
576-
int st = Dt_Results.Rows.Count;
589+
AddResultToTable(result);
590+
});
577591

578-
IndexColors[st] = GetColor(result.Engine);
592+
}
579593

580-
Dt_Results.Rows.Add($"{result.Engine.Name} (Raw)", string.Empty,
581-
result.RawUrl, 0, null, null, $"{result.Status}",
582-
null, null, null, null, null, null);
583-
// Message[result.RawUrl] = "?";
594+
private void AddResultToTable(SearchResult result)
595+
{
596+
int st = Dt_Results.Rows.Count;
584597

585-
for (int i = 0; i < result.Results.Count; i++) {
586-
SearchResultItem sri = result.Results[i];
598+
var cs = GetColor(result.Engine);
587599

588-
object? meta = sri.Metadata switch
589-
{
590-
string[] rg => rg.QuickJoin(),
591-
Array rg => rg.QuickJoin(),
592-
ICollection c => c.QuickJoin(),
593-
string s => s,
594-
ExpandoObject eo => eo.QuickJoin(),
595-
_ => null,
600+
IndexColors[st] = cs;
596601

597-
};
602+
Dt_Results.Rows.Add($"{result.Engine.Name} (Raw)", string.Empty,
603+
result.RawUrl, 0, null, null, $"{result.Status}",
604+
null, null, null, null, null, null);
605+
// Message[result.RawUrl] = "?";
606+
// var rawSri = new SearchResultItem(result) { Url = result.RawUrl, Similarity = null };
598607

599-
// object? meta = sri.Metadata;
608+
for (int i = 0; i < result.Results.Count; i++) {
609+
SearchResultItem sri = result.Results[i];
610+
add(sri, i);
600611

601-
if (sri.Similarity is { } and 0) {
602-
sri.Similarity = null;
603-
}
612+
}
604613

605-
st = Dt_Results.Rows.Count;
614+
// Interlocked.Increment(ref ResultCount);
615+
Pbr_Status.Fraction = (float) m_results.Count / (Client.Engines.Length);
606616

607-
IndexColors[st] = GetColor(result.Engine);
617+
// Pbr_Status.Fraction = (float) ++ResultCount / (Client.Engines.Length);
618+
Tv_Results.SetNeedsDisplay();
619+
Pbr_Status.SetNeedsDisplay();
608620

609-
Dt_Results.Rows.Add($"{result.Engine.Name} #{i + 1}", "",
610-
sri.Url, sri.Score, sri.Similarity, sri.Artist, sri.Description, sri.Source,
611-
sri.Title, sri.Site, sri.Width, sri.Height, meta);
612-
}
621+
}
613622

614-
// Interlocked.Increment(ref ResultCount);
615-
Pbr_Status.Fraction = (float) m_results.Count / (Client.Engines.Length);
623+
void add(SearchResultItem sri, int i)
624+
{
625+
object? meta = sri.Metadata switch
626+
{
627+
string[] rg => rg.QuickJoin(),
628+
Array rg => rg.QuickJoin(),
629+
ICollection c => c.QuickJoin(),
630+
string s => s,
631+
ExpandoObject eo => eo.QuickJoin(),
632+
_ => null,
616633

617-
// Pbr_Status.Fraction = (float) ++ResultCount / (Client.Engines.Length);
618-
Tv_Results.SetNeedsDisplay();
619-
Pbr_Status.SetNeedsDisplay();
620-
});
634+
};
635+
636+
// object? meta = sri.Metadata;
637+
638+
if (sri.Similarity is { } and 0) {
639+
sri.Similarity = null;
640+
}
641+
642+
var cs = GetColor(sri.Root.Engine);
643+
var st = Dt_Results.Rows.Count;
644+
645+
IndexColors[st] = cs;
621646

647+
Dt_Results.Rows.Add($"{sri.Root.Engine.Name} #{i + 1}", "",
648+
sri.Url, sri.Score, sri.Similarity, sri.Artist, sri.Description, sri.Source,
649+
sri.Title, sri.Site, sri.Width, sri.Height, meta);
622650
}
623651

624652
private void OnComplete(object sender, SearchResult[] results)
@@ -694,7 +722,7 @@ private void ProcessArgs()
694722

695723
else if (s == R2.Arg_Queue) {
696724
while (e.MoveNext()) {
697-
Queue.Enqueue(e.Current?.ToString());
725+
Queue.Enqueue(e.Current.ToString());
698726
}
699727

700728
QueueMode = true;

SmartImage 3/SmartImage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<PrivateAssets>all</PrivateAssets>
7979
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
8080
</PackageReference>
81+
<PackageReference Include="Spectre.Console.Cli" Version="0.47.0" />
8182
<PackageReference Include="Spectre.Console.Extensions.Progress" Version="1.0.0" />
8283
<PackageReference Include="Spectre.Console.Extensions.Table" Version="1.0.0" />
8384
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />

SmartImage.Lib 3/Engines/Impl/Upload/BaseUploadEngine.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Net;
23
using System.Net.NetworkInformation;
34
using System.Reflection.PortableExecutable;
45
using Flurl.Http;
@@ -96,6 +97,14 @@ protected override async Task<BaseUploadResponse> VerifyResultAsync(
9697
}
9798

9899
var responseMessage = response.ResponseMessage;
100+
101+
switch (responseMessage.StatusCode) {
102+
case HttpStatusCode.GatewayTimeout:
103+
url = null;
104+
ok = false;
105+
goto ret;
106+
}
107+
99108
url = await responseMessage.Content.ReadAsStringAsync(ct);
100109

101110
ok = true;

SmartImage.Lib 3/Utilities/ImageHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Diagnostics;
23
using AngleSharp.Dom;
34
using AngleSharp.Html.Parser;
45
using Flurl.Http;
@@ -21,6 +22,7 @@ public static async Task<UniSource[]> ScanAsync(Url u, CancellationToken ct = de
2122
}
2223
catch (Exception e)
2324
{
25+
Debug.WriteLine($"{e.Message}");
2426
return Array.Empty<UniSource>();
2527
}
2628

0 commit comments

Comments
 (0)