Skip to content

Commit c8c04d9

Browse files
committed
Rdx work
1 parent b5d6b2b commit c8c04d9

File tree

3 files changed

+132
-102
lines changed

3 files changed

+132
-102
lines changed

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ public abstract class BaseSearchEngine : IDisposable
2929
/// </summary>
3030
public virtual string Name => EngineOption.ToString();
3131

32-
public virtual Url BaseUrl { get; }
33-
34-
public bool IsAdvanced { get; protected set; }
32+
public virtual Url BaseUrl { get; }
33+
34+
public bool IsAdvanced { get; protected set; }
35+
36+
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(3);
3537

36-
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(3);
38+
public TimeSpan Duration { get; protected set; }
3739

38-
protected long MaxSize { get; set; } = NA_SIZE;
40+
protected long MaxSize { get; set; } = NA_SIZE;
3941

4042
protected virtual string[] ErrorBodyMessages { get; } = Array.Empty<string>();
4143

@@ -104,7 +106,7 @@ protected virtual SearchResultStatus Verify(SearchQuery q)
104106

105107
return !b ? SearchResultStatus.IllegalInput : SearchResultStatus.None;
106108
}
107-
109+
108110
public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
109111
{
110112

SmartImage.Lib 3/SearchClient.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ public SearchClient(SearchConfig cfg)
4949
LoadEngines();
5050
}
5151

52-
static SearchClient()
53-
{
54-
55-
}
52+
static SearchClient() { }
5653

5754
[ModuleInitializer]
5855
public static void Init()
@@ -127,7 +124,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
127124

128125
var result = await task;
129126

130-
ProcessResult(result);
127+
// ProcessResult(result);
131128

132129
results[i] = result;
133130
i++;
@@ -225,7 +222,13 @@ public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, CancellationTo
225222
var tasks = Engines.Select(e =>
226223
{
227224
Debug.WriteLine($"Starting {e} for {query}");
228-
var res = e.GetResultAsync(query, token);
225+
226+
var res = e.GetResultAsync(query, token)
227+
.ContinueWith( (r) =>
228+
{
229+
ProcessResult(r.Result);
230+
return r.Result;
231+
}, token);
229232

230233
return res;
231234
}).ToList();

SmartImage.Rdx/Cli/SearchCommand.cs

Lines changed: 115 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ namespace SmartImage.Rdx.Cli;
2525
internal sealed class SearchCommand : AsyncCommand<SearchCommandSettings>, IDisposable
2626
{
2727

28-
public override ValidationResult Validate(CommandContext context, SearchCommandSettings settings)
29-
{
30-
31-
var b = SearchQuery.IsValidSourceType(settings.Query);
32-
33-
return b ? ValidationResult.Success() : ValidationResult.Error();
34-
// var v= base.Validate(context, settings);
35-
// return v;
36-
}
37-
3828
public SearchClient Client { get; }
3929

4030
public SearchQuery Query { get; private set; }
@@ -45,7 +35,7 @@ public override ValidationResult Validate(CommandContext context, SearchCommandS
4535

4636
private readonly ConcurrentBag<ResultModel> m_results;
4737

48-
private readonly STable m_resTable;
38+
// private readonly STable m_resTable;
4939

5040
private const int COMPLETE = 100;
5141

@@ -54,11 +44,11 @@ public SearchCommand()
5444
Config = new SearchConfig();
5545
Client = new SearchClient(Config);
5646
Client.OnComplete += OnComplete;
57-
Client.OnResult += OnResult;
58-
m_cts = new CancellationTokenSource();
59-
m_results = new ConcurrentBag<ResultModel>();
47+
// Client.OnResult += OnResult;
48+
m_cts = new CancellationTokenSource();
49+
m_results = new ConcurrentBag<ResultModel>();
6050

61-
m_resTable = new Table()
51+
/*m_resTable = new Table()
6252
{
6353
Border = TableBorder.Heavy,
6454
Title = new($"Results"),
@@ -69,24 +59,9 @@ public SearchCommand()
6959
m_resTable.AddColumns(new TableColumn("#"),
7060
new TableColumn("Name"),
7161
new TableColumn("Count")
72-
);
73-
Query = SearchQuery.Null;
74-
}
75-
76-
private void OnResult(object sender, SearchResult sr)
77-
{
78-
// Interlocked.Increment(ref ResultModel.cnt);
79-
var rm = new ResultModel(sr) { };
80-
m_results.Add(rm);
81-
82-
m_resTable.Rows.Add(new IRenderable[]
83-
{
84-
new Text($"{rm.Id}"),
85-
Markup.FromInterpolated($"[bold]{sr.Engine.Name}[/]"),
86-
new Text($"{sr.Results.Count}")
87-
});
62+
);*/
8863

89-
// AnsiConsole.Write(t);
64+
Query = SearchQuery.Null;
9065
}
9166

9267
private void OnComplete(object sender, SearchResult[] searchResults)
@@ -98,81 +73,62 @@ public async Task Interactive()
9873
{
9974
ConsoleKeyInfo cki;
10075

101-
do {
76+
// var i = AC.Ask<int>("?");
10277

103-
// var i = AC.Ask<int>("?");
78+
/*
79+
if (!char.IsNumber(cki.KeyChar)) {
80+
continue;
81+
}
82+
*/
10483

105-
/*
106-
if (!char.IsNumber(cki.KeyChar)) {
107-
continue;
108-
}
109-
*/
84+
AC.Clear();
11085

111-
AC.Clear();
86+
//todo
11287

113-
//todo
114-
var prompt = AC.Prompt(new SelectionPrompt<string>()
115-
.Title("Engine"));
88+
var select = m_results
89+
.ToDictionary((x) =>
90+
{
91+
return x.Result.Engine.Name;
92+
});
11693

117-
AC.Write(m_resTable);
94+
var choices = new SelectionPrompt<string>()
95+
.Title("Engine")
96+
.AddChoices(select.Keys);
11897

119-
var i = AC.Ask<int>("?", 0);
98+
string prompt = null;
99+
// AC.Write(m_resTable);
120100

121-
if (i == 0) {
122-
break;
123-
}
101+
while (prompt != "") {
102+
prompt = AC.Prompt(choices);
124103

125-
// var i = (int) char.GetNumericValue(cki.KeyChar);
104+
if (select.TryGetValue(prompt, out var v)) {
126105

127-
var rows = m_resTable.Rows;
106+
AC.Clear();
107+
AC.Write(v.Table);
128108

129-
if (rows.Count == 0 || (i < 0 || i > m_results.Count)) {
130-
continue;
131109
}
110+
}
132111

133-
var rr = m_results.FirstOrDefault(x => x.Id == i);
112+
/*var fn = rows.GetType()
113+
.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
114+
.FirstOrDefault(x => x.Name.Contains("get_Item"));
134115
135-
if (rr == null) {
136-
continue;
137-
}
116+
var res = (TableRow) fn.Invoke(rows, new Object[] { i-1 });
117+
var en = res.GetEnumerator();
118+
119+
while (en.MoveNext()) {
120+
var it = en.Current;
138121
139122
AConsole.AlternateScreen(() =>
140123
{
141-
AC.Write(rr.Table);
142-
// Console.ReadKey();
143-
var n = AC.Ask<int>("?");
144-
145-
if (n == 0 || (n < 0 || n > rr.Result.Results.Count)) {
146-
return;
147-
}
148-
149-
var res = rr.Result.Results[n];
150-
HttpUtilities.TryOpenUrl(res.Url);
124+
AC.Clear();
125+
AC.Write(t);
126+
AC.Confirm("");
151127
});
152128
153-
/*var fn = rows.GetType()
154-
.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
155-
.FirstOrDefault(x => x.Name.Contains("get_Item"));
156-
157-
var res = (TableRow) fn.Invoke(rows, new Object[] { i-1 });
158-
var en = res.GetEnumerator();
159-
160-
while (en.MoveNext()) {
161-
var it = en.Current;
162-
163-
AConsole.AlternateScreen(() =>
164-
{
165-
AC.Clear();
166-
AC.Write(t);
167-
AC.Confirm("");
168-
});
169-
170-
if (it is Table t) { }
171-
}*/
172-
173-
switch (i) { }
129+
if (it is Table t) { }
130+
}*/
174131

175-
} while (true);
176132
}
177133

178134
public override async Task<int> ExecuteAsync(CommandContext context, SearchCommandSettings settings)
@@ -227,15 +183,84 @@ public override async Task<int> ExecuteAsync(CommandContext context, SearchComma
227183

228184
// pt1.MaxValue = m_client.Engines.Length;
229185

230-
var run = await Client.RunSearchAsync(Query, token: m_cts.Token);
231-
186+
var grid = new Grid();
187+
188+
grid.AddColumns(
189+
new GridColumn() { Alignment = Justify.Left },
190+
new GridColumn() { Alignment = Justify.Center },
191+
new GridColumn() { Alignment = Justify.Right }
192+
);
193+
194+
grid.AddRow([
195+
new Text("Engine", new Style(Color.Red, decoration: Decoration.Bold | Decoration.Underline)),
196+
new Text("Count", new Style(Color.Green, decoration: Decoration.Bold | Decoration.Underline)),
197+
new Text("Status", new Style(Color.Blue, decoration: Decoration.Bold | Decoration.Underline))
198+
]);
199+
200+
var live = AC.Live(grid)
201+
.StartAsync(async (l) =>
202+
{
203+
204+
Client.OnResult += OnResultComplete;
205+
206+
var run = Client.RunSearchAsync(Query, token: m_cts.Token);
207+
208+
await run;
209+
210+
Client.OnResult -= OnResultComplete;
211+
return;
212+
213+
void OnResultComplete(object sender, SearchResult sr)
214+
{
215+
var rm = new ResultModel(sr) { };
216+
m_results.Add(rm);
217+
var i=(int) sr.Engine.EngineOption;
218+
219+
grid.AddRow([
220+
new Text(sr.Engine.Name,
221+
new Style(Color.FromInt32(Math.Clamp(i%(int)byte.MaxValue,byte.MinValue,byte.MaxValue)),
222+
decoration: Decoration.Italic)),
223+
224+
new Text($"{sr.Results.Count}",
225+
new Style(Color.Wheat1,
226+
decoration: Decoration.None)),
227+
228+
new Text($"{sr.Status}",
229+
new Style(Color.Cyan1,
230+
decoration: Decoration.None))
231+
]);
232+
233+
/*m_resTable.Rows.Add(new IRenderable[]
234+
{
235+
new Text($"{rm.Id}"),
236+
Markup.FromInterpolated($"[bold]{sr.Engine.Name}[/]"),
237+
new Text($"{sr.Results.Count}")
238+
});*/
239+
240+
l.Refresh();
241+
242+
}
243+
});
244+
245+
await live;
246+
232247
if (settings.Interactive) {
233248
await Interactive();
234249
}
235250

236251
return 0;
237252
}
238253

254+
public override ValidationResult Validate(CommandContext context, SearchCommandSettings settings)
255+
{
256+
257+
var b = SearchQuery.IsValidSourceType(settings.Query);
258+
259+
return b ? ValidationResult.Success() : ValidationResult.Error();
260+
// var v= base.Validate(context, settings);
261+
// return v;
262+
}
263+
239264
public void Dispose()
240265
{
241266
foreach (var sr in m_results) {

0 commit comments

Comments
 (0)