Skip to content

Commit db42d65

Browse files
committed
Remove Spectre: corrupts console buffer and causes other issues
1 parent 4355631 commit db42d65

File tree

5 files changed

+47
-79
lines changed

5 files changed

+47
-79
lines changed

SmartImage 3/App/Integration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public static void HandlePath(bool option)
148148
}
149149

150150
bool appFolderInPath = oldValue
151-
.Split(FileSystem.PATH_DELIM)
152-
.Any(p => p == appFolder);
151+
.Split(FileSystem.PATH_DELIM)
152+
.Any(p => p == appFolder);
153153

154154
string cd = Environment.CurrentDirectory;
155155
string exe = Path.Combine(cd, ExeLocation);

SmartImage 3/Mode/CliMode.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace SmartImage.Mode;
1717

18-
public sealed class CliMode : IDisposable, IMode
18+
public sealed class CliMode : IDisposable, IMode, IProgress<int>
1919
{
2020
#region
2121

@@ -31,19 +31,18 @@ static CliMode()
3131

3232
private SearchResult[] m_results2;
3333

34-
private readonly SearchConfig m_cfg;
35-
3634
private SearchQuery m_query;
3735

3836
private readonly SearchClient m_client;
3937

4038
private readonly CancellationTokenSource m_cts;
4139

42-
public SearchConfig Config => m_cfg;
40+
public SearchConfig Config { get; }
41+
4342
public CliMode()
4443
{
45-
m_cfg = new SearchConfig();
46-
m_client = new SearchClient(m_cfg);
44+
Config = new SearchConfig();
45+
m_client = new SearchClient(Config);
4746
m_query = SearchQuery.Null;
4847
m_cts = new CancellationTokenSource();
4948
}
@@ -52,7 +51,7 @@ public CliMode()
5251
{
5352

5453
// await Prg_1.StartAsync(ctx => ValidateInputAsync(ctx, c as string));
55-
await ValidateInputAsync((c as string)!);
54+
await ValidateInputAsync((string) c!);
5655
AConsole.WriteLine($"{m_query}");
5756

5857
// var url = await Prg_1.StartAsync(UploadInputAsync);
@@ -61,7 +60,7 @@ public CliMode()
6160

6261
AConsole.MarkupLine($"[green]{m_query.Upload}[/]");
6362

64-
AConsole.WriteLine($"{m_cfg}");
63+
AConsole.WriteLine($"{Config}");
6564

6665
SConsole.CancelKeyPress += (sender, args) =>
6766
{
@@ -117,14 +116,17 @@ void OnComplete(object sender, SearchResult[] searchResults)
117116
// pt1.Increment(COMPLETE);
118117
}
119118

120-
var ptMap = new Dictionary<BaseSearchEngine, (object, Table)>();
119+
m_client.OnComplete += OnComplete;
121120

122-
foreach (var e in m_client.Engines) {
123-
var tt = get_table(e);
124-
ptMap.Add(e, (this, tt));
125-
}
121+
var mt = new Table()
122+
{
123+
Border = TableBorder.Heavy,
124+
Title = new($"Results"),
125+
ShowFooters = true,
126+
ShowHeaders = true,
127+
};
126128

127-
m_client.OnComplete += OnComplete;
129+
mt.AddColumns(new TableColumn("#"), new TableColumn("Link"));
128130

129131
// pt1.MaxValue = m_client.Engines.Length;
130132

@@ -135,10 +137,10 @@ void OnResult(object sender, SearchResult sr)
135137
// pt1.Increment(1.0);
136138
int i = 0;
137139

138-
var t = ptMap[sr.Engine].Item2;
140+
// var t = ptMap[sr.Engine].Item2;
139141

140142
foreach (SearchResultItem sri in sr.Results) {
141-
t.Rows.Add(new IRenderable[]
143+
mt.Rows.Add(new IRenderable[]
142144
{
143145
new Text($"{i + 1}"),
144146
Markup.FromInterpolated($"[link={sri.Url}]{sr.Engine.Name} #{i + 1}[/]")
@@ -158,20 +160,17 @@ void OnResult(object sender, SearchResult sr)
158160
while (!pt1.IsFinished) { }*/
159161
var sw = Stopwatch.StartNew();
160162

161-
var sp = AConsole.Status()
162-
.Spinner(Spinner.Known.Aesthetic)
163-
.StartAsync("Wait...", async ctx =>
163+
var sp = AConsole.Live(mt)
164+
.AutoClear(false)
165+
.Overflow(VerticalOverflow.Ellipsis)
166+
.StartAsync(async (ctx) =>
164167
{
165-
// await ttt;
166-
167168
while (!ttt.IsCompleted) {
168169
ctx.Refresh();
169-
await Task.Delay(TimeSpan.FromMilliseconds(300));
170-
171-
ctx.Status =
172-
$"{m_results.Count} | {m_results.Sum(c => c.Results.Count)} | {sw.Elapsed.TotalSeconds:3F}";
170+
mt.Caption = new TableTitle($"{sw.Elapsed.TotalSeconds:F3}");
171+
// await Task.Delay(1000);
173172
}
174-
// m_results2 = await ttt;
173+
175174
});
176175

177176
await ttt;
@@ -192,11 +191,6 @@ void OnResult(object sender, SearchResult sr)
192191

193192
await sp;
194193

195-
foreach (var vt in ptMap.Values) {
196-
// vt.Item1.StopTask();
197-
AConsole.Write(vt.Item2);
198-
}
199-
200194
}
201195

202196
private static Table get_table(BaseSearchEngine bse)
@@ -222,4 +216,9 @@ public void Dispose()
222216
m_query.Dispose();
223217
m_client.Dispose();
224218
}
219+
220+
public void Report(int value)
221+
{
222+
Debug.WriteLine($"{value}");
223+
}
225224
}

SmartImage 3/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#nullable disable
2-
global using AConsole = Spectre.Console.AnsiConsole;
32
global using SConsole = System.Console;
43
global using static Kantan.Diagnostics.LogCategories;
5-
using Console = Spectre.Console.AnsiConsole;
64
using System.CommandLine;
75
using System.Diagnostics;
86
using System.Runtime.CompilerServices;
@@ -19,7 +17,6 @@
1917
using SmartImage.Mode;
2018
using SmartImage.Mode.Shell;
2119
using SmartImage.Utilities;
22-
using Spectre.Console;
2320
using Command = System.CommandLine.Command;
2421

2522
#pragma warning disable CS0168

SmartImage 3/SmartImage.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@
7373
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
7474
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
7575
<PackageReference Include="Microsoft.NETCore.Platforms" Version="7.0.0" />
76-
<PackageReference Include="Spectre.Console" Version="0.46.0" />
77-
<PackageReference Include="Spectre.Console.Analyzer" Version="0.46.0">
78-
<PrivateAssets>all</PrivateAssets>
79-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
80-
</PackageReference>
8176
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
8277
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
8378
<PackageReference Include="System.Json" Version="4.7.1" />

SmartImage.Lib 3/SearchClient.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
global using ICBN = JetBrains.Annotations.ItemCanBeNullAttribute;
22
using System.Collections;
3+
using System.Collections.Concurrent;
34
using System.Diagnostics;
5+
using System.Net;
6+
using System.Net.NetworkInformation;
47
using System.Text.Json;
58
using Flurl.Http;
69
using Flurl.Http.Configuration;
@@ -85,7 +88,7 @@ static SearchClient()
8588
/// </summary>
8689
/// <param name="query">Search query</param>
8790
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
88-
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, CancellationToken? token = null)
91+
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, CancellationToken? token = null, [CBN] IProgress<int> p=null)
8992
{
9093
if (!ConfigApplied) {
9194
await ApplyConfigAsync();
@@ -112,13 +115,15 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, Cancellation
112115
var result = await task;
113116

114117
OnResult?.Invoke(this, result);
118+
p?.Report(i);
115119

116120
if (Config.PriorityEngines.HasFlag(result.Engine.EngineOption)) {
117121

118122
OpenResult(result);
119123
}
120124

121-
results[i++] = result;
125+
results[i] = result;
126+
i++;
122127
// results.Add(result);
123128
tasks.Remove(task);
124129
}
@@ -194,48 +199,19 @@ public async ValueTask ApplyConfigAsync()
194199
[CBN]
195200
public BaseSearchEngine TryGetEngine(SearchEngineOptions o) => Engines.FirstOrDefault(e => e.EngineOption == o);
196201

197-
public class SearchResultItemCollection : ICollection<SearchResultItem>
202+
public static ValueTask<IReadOnlyList<SearchResultItem>> Filter(IEnumerable<SearchResultItem> sri)
198203
{
199-
private readonly ICollection<SearchResultItem> m_value;
200204

201-
public IEnumerator<SearchResultItem> GetEnumerator()
202-
{
203-
return m_value.GetEnumerator();
204-
}
205-
206-
IEnumerator IEnumerable.GetEnumerator()
207-
{
208-
return ((IEnumerable) m_value).GetEnumerator();
209-
}
210-
211-
public void Add(SearchResultItem item)
212-
{
213-
m_value.Add(item);
214-
}
215-
216-
public void Clear()
217-
{
218-
m_value.Clear();
219-
}
220-
221-
public bool Contains(SearchResultItem item)
222-
{
223-
return m_value.Contains(item);
224-
}
205+
var sri2 = sri.AsParallel().DistinctBy(e => e.Url).ToList();
225206

226-
public void CopyTo(SearchResultItem[] array, int arrayIndex)
207+
/*Parallel.ForEachAsync(sri2, async (item, token) =>
227208
{
228-
m_value.CopyTo(array, arrayIndex);
229-
}
230-
231-
public bool Remove(SearchResultItem item)
232-
{
233-
return m_value.Remove(item);
234-
}
209+
var r = await item.Url.AllowAnyHttpStatus().GetAsync();
235210
236-
public int Count => m_value.Count;
211+
if (r.ResponseMessage.IsSuccessStatusCode) { }
212+
});*/
237213

238-
public bool IsReadOnly => m_value.IsReadOnly;
214+
return ValueTask.FromResult<IReadOnlyList<SearchResultItem>>(sri2);
239215
}
240216

241217
public static IReadOnlyList<SearchResultItem> Optimize(IEnumerable<SearchResultItem> sri)
@@ -295,4 +271,5 @@ public void Dispose()
295271
ConfigApplied = false;
296272
IsComplete = false;
297273
}
274+
298275
}

0 commit comments

Comments
 (0)