Skip to content

Commit e07400c

Browse files
committed
Fix indexing again
1 parent 6a3e6db commit e07400c

6 files changed

Lines changed: 188 additions & 173 deletions

File tree

SmartImage.Lib/Images/Uni/UniImageUrl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ internal UniImageUrl(Url url) : base(url?.ToString(), UniImageType.Uri)
3232
// public override string Name => Url?.GetFileName();
3333

3434

35-
public async ValueTask<bool> ScanAsync<TUniImageUrl>(ChannelWriter<TUniImageUrl> cw, Func<string, TUniImageUrl> newItem, CancellationToken ct = default)
36-
where TUniImageUrl : UniImageUrl
35+
public async ValueTask<bool> ScanAsync<TUni>(ChannelWriter<TUni> cw, Func<string, TUni> newItem, CancellationToken ct = default)
36+
where TUni : UniImage
3737
{
3838
var allocImageAsync = await AllocImageAsync(ct);
3939

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#nullable disable
2-
using System.Collections.Concurrent;
3-
using SmartImage.Lib.Engines.Results;
4-
using SmartImage.Lib.Utilities;
5-
using SmartImage.Rdx.Shell;
6-
using Spectre.Console;
7-
82
namespace SmartImage.Rdx.Commands.Search;
93

104
public sealed partial class SearchCommand
@@ -47,123 +41,6 @@ public sealed partial class SearchCommand
4741
4842
}*/
4943

50-
internal static readonly ConcurrentDictionary<SearchResultItem, int> tbl = new();
51-
52-
internal record Selection
53-
{
54-
55-
public SearchResultItem Item { get; }
56-
57-
// public SearchResultItem Scanned { get; }
58-
59-
public int ItemIdx { get; }
60-
61-
public int ScanIdx { get; }
62-
63-
public bool IsScannedItem { get; }
64-
65-
public Selection(SearchResultItem item, int itemIdx, int scanIdx, bool isScanned)
66-
{
67-
Item = item;
68-
ItemIdx = itemIdx;
69-
ScanIdx = scanIdx;
70-
IsScannedItem = isScanned;
71-
}
72-
73-
public int Index()
74-
{
75-
int i = 0, j = 0;
76-
var scnIdx = 0;
77-
int root = 0;
78-
int t = 0;
79-
80-
if (Item.IsChild) {
81-
// scnIdx = Item.Parent.ScannedItems.IndexOf(Item);
82-
root = Item.Parent.Root.Results.IndexOf(Item.Parent);
83-
84-
scnIdx++;
85-
86-
}
87-
else {
88-
root = Item.Root.Results.IndexOf(Item);
89-
90-
}
91-
92-
// t = Item.Parent.Root.Results[..(root + 1)].Sum(rr => rr.ScannedItems.Count);
93-
94-
for (int k = 0; k < root; k++) {
95-
96-
var result = Item.Parent.Root.Results[k];
97-
var scnItm = result.ScannedItems;
98-
var scnIdx2 = scnItm.IndexOf(Item);
99-
100-
if (scnIdx2 == -1) {
101-
t += scnItm.Count;
102-
}
103-
else {
104-
t += scnIdx2;
105-
}
106-
107-
}
108-
109-
return root + scnIdx + t;
110-
}
111-
112-
}
113-
114-
[CBN]
115-
static Selection Parse(string str, SearchResult sr)
116-
{
117-
var spl = str.Split('.');
118-
119-
SearchResultItem sri = null, sri2 = null;
120-
121-
var resIdx = 0;
122-
var scnIdx = -1;
123-
bool isScanned = false;
124-
125-
126-
if (sr.Results.TryParseIndex(spl[0], out resIdx, out sri)) {
127-
if (spl.Length == 2) {
128-
if (sri.ScannedItems.TryParseIndex(spl[1], out scnIdx, out sri2)) {
129-
sri = sri2;
130-
isScanned = true;
131-
}
132-
133-
// if (sr.ScannedResults[sri].TryParseIndex(spl[1], out scnIdx, out sri2)) { }
134-
135-
}
136-
else { }
137-
138-
}
139-
else { }
140-
141-
return new Selection(sri, resIdx, scnIdx, isScanned);
142-
}
143-
144-
private static Selection GetSelectionChoice(SearchResult res)
145-
{
146-
Selection ret;
147-
148-
Elements.Prm_Num2.Validator = str =>
149-
{
150-
ret = Parse(str, res);
151-
152-
if (ret == null) {
153-
return ValidationResult.Error();
154-
}
155-
156-
return ValidationResult.Success();
157-
};
158-
159-
160-
var val = AnsiConsole.Prompt(Elements.Prm_Num2);
161-
var sri = Parse(val, res);
162-
return sri;
163-
164-
165-
}
166-
16744
#endregion
16845

16946
}

SmartImage.Rdx/Commands/Search/SearchCommand.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,12 @@ private async Task RunInteractiveAsync(CancellationToken ct = default)
283283
break;
284284
}
285285

286-
var sel = GetSelectionChoice(sr);
287-
var sri = sel.Item;
286+
var sel = ShellSelection.GetSelectionChoice(sr);
287+
var sri = sel.Item;
288+
var selIdx = sel.Index();
289+
var selIdx2 = sel.Index2();
288290

289-
s_logger.LogDebug("Selected {Item} {Scn} | {Idx1}, {Idx2}", sel.Item, sel.IsScannedItem, sel.ItemIdx, sel.ScanIdx);
291+
s_logger.LogDebug("Selected {Item} {Scn} | {Idx1}, {Idx2}", sel.Item, sel.IsScannedItem, selIdx, selIdx2);
290292

291293
// s_logger.LogTrace("Interactive: {ResItem}", sri);
292294

@@ -316,12 +318,11 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
316318

317319
// var idx = tbl[sel.Item.Parent];
318320

319-
var selIdx = sel.Index();
320321

321322
for (int i = 0; i < sri.ScannedItems.Count; i++) {
322323
SearchResultItem scnItm = sri.ScannedItems[i];
323324
var scnRow = scnItm.GetItemRow(sel.ItemIdx, i);
324-
srTable.InsertRow(selIdx + i + 1, scnRow);
325+
srTable.InsertRow(selIdx2 + i + 1, scnRow);
325326
}
326327

327328

@@ -335,13 +336,13 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
335336
// row = row2.RootIdx + (row2.ScnIdx == -1 ? 0 : (row2.ScnIdx + 1));
336337

337338
if (sri.HasImage) {
338-
srTable.Rows.Update(selIdx, (int) ResultRowIndex.ROW_WH, sri.GetResolution());
339+
srTable.Rows.Update(selIdx2, (int) ResultRowIndex.ROW_WH, sri.GetResolution());
339340

340341
}
341342

342343
if (sri.HasHash && !sri.Similarity.HasValue) {
343344
sri.CalculateSimilarity(Query.Source);
344-
srTable.Rows.Update(selIdx, (int) ResultRowIndex.ROW_SIMILARITY, sri.GetSimilarity());
345+
srTable.Rows.Update(selIdx2, (int) ResultRowIndex.ROW_SIMILARITY, sri.GetSimilarity());
345346
}
346347

347348
f.Refresh();
@@ -356,12 +357,9 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
356357

357358
AnsiConsole.Live(srTable).Start(f =>
358359
{
359-
//todo
360-
var row = sel.ItemIdx;
361-
362360
sri.CalculateSimilarity(Query.Source);
363361

364-
srTable.Rows.Update(row, (int) ResultRowIndex.ROW_SIMILARITY, sri.GetSimilarity());
362+
srTable.Rows.Update(selIdx2, (int) ResultRowIndex.ROW_SIMILARITY, sri.GetSimilarity());
365363
f.Refresh();
366364
});
367365

@@ -614,7 +612,7 @@ public override void Dispose()
614612
sr.Dispose();
615613
}
616614

617-
Elements.Prm_Num2.Validator = null;
615+
Elements.Prm_Selection.Validator = null;
618616

619617
m_resultTables.Clear();
620618
m_cts.Dispose();

SmartImage.Rdx/Shell/Elements.cs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,6 @@ static Elements() { }
7575

7676
internal const double COMPLETE = 100.0d;
7777

78-
#region Engine map table
79-
80-
// TODO: FOR SERVER ONLY, DEPRECATE
81-
82-
internal const int ROW_EMT2_THR = 0;
83-
internal const int ROW_EMT2_NAME = 1;
84-
internal const int ROW_EMT2_RESULTS = 2;
85-
internal const int ROW_EMT2_STATUS = 3;
86-
internal const int ROW_EMT2_TIMEOUT = 4;
87-
88-
89-
public static SpcTable GetEngineMapTableBase()
90-
{
91-
var table = new SpcTable();
92-
93-
var columns = GetColumns("Thread", nameof(BaseSearchEngine.Name), nameof(SearchResult.Results),
94-
nameof(SearchResult.Status), nameof(BaseSearchEngine.Timeout));
95-
96-
table.AddColumns(columns.ToArray());
97-
return table;
98-
}
99-
100-
private static TableColumn GetColumn(string name)
101-
{
102-
return new TableColumn(new Text(name, Sty_Grid1)) { };
103-
}
104-
105-
public static IEnumerable<TableColumn> GetColumns(params string[] names)
106-
{
107-
return names.Select(GetColumn);
108-
}
109-
110-
#endregion
111-
11278

11379
#region Prompts
11480

@@ -123,7 +89,7 @@ public static IEnumerable<TableColumn> GetColumns(params string[] names)
12389
}
12490
};
12591

126-
public static readonly TextPrompt<string> Prm_Num2 = new(Markup.Escape("[#.#]"))
92+
public static readonly TextPrompt<string> Prm_Selection = new(Markup.Escape("[#.#]"))
12793
{
12894
ShowChoices = false,
12995
ShowDefaultValue = false,
@@ -199,6 +165,8 @@ public static SpcTable CreateFullResultTable()
199165

200166
#endregion
201167

168+
#region
169+
202170
internal static Grid CreateConfigGrid(SearchConfig cfg, SearchQuery query)
203171
{
204172
var dt = new Grid();
@@ -307,4 +275,40 @@ internal static Grid MapToGrid<TKey, TValue>(IDictionary<TKey, TValue> dictionar
307275
return grd;
308276
}
309277

278+
#endregion
279+
280+
#region Engine map table
281+
282+
// TODO: FOR SERVER ONLY, DEPRECATE
283+
284+
internal const int ROW_EMT2_THR = 0;
285+
internal const int ROW_EMT2_NAME = 1;
286+
internal const int ROW_EMT2_RESULTS = 2;
287+
internal const int ROW_EMT2_STATUS = 3;
288+
internal const int ROW_EMT2_TIMEOUT = 4;
289+
290+
291+
public static SpcTable GetEngineMapTableBase()
292+
{
293+
var table = new SpcTable();
294+
295+
var columns = GetColumns("Thread", nameof(BaseSearchEngine.Name), nameof(SearchResult.Results),
296+
nameof(SearchResult.Status), nameof(BaseSearchEngine.Timeout));
297+
298+
table.AddColumns(columns.ToArray());
299+
return table;
300+
}
301+
302+
private static TableColumn GetColumn(string name)
303+
{
304+
return new TableColumn(new Text(name, Sty_Grid1)) { };
305+
}
306+
307+
public static IEnumerable<TableColumn> GetColumns(params string[] names)
308+
{
309+
return names.Select(GetColumn);
310+
}
311+
312+
#endregion
313+
310314
}

SmartImage.Rdx/Shell/Renderables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public IEnumerable<IRenderable[]> GetFullResultRows()
4141

4242
public IRenderable GetResolution() => (sri.HasDimensions) ? new Text($"{sri.Width}x{sri.Height}") : Elements.Txt_NA;
4343

44-
public IRenderable GetSimilarity() => sri.Similarity.HasValue ? new Text($"{sri.Similarity}") : Elements.Txt_NA;
44+
public IRenderable GetSimilarity() => AsRenderable(sri.Similarity);
4545

4646
public IRenderable[] GetFullResultRow(int i, Style style)
4747
{

0 commit comments

Comments
 (0)