Skip to content

Commit cf4acd9

Browse files
committed
Work on minimal UI mode
1 parent d8c7ab9 commit cf4acd9

File tree

20 files changed

+572
-370
lines changed

20 files changed

+572
-370
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ Use the following parameters to invoke SmartImage:
109109

110110
| Parameter | Definition |
111111
| - | - |
112-
| `-i` | Input |
113-
| `-auto` | Start search immediately |
112+
| `--i` | Input |
113+
| `--auto` | Start search immediately |
114114

115115
## Wiki
116116

SmartImage 3/App/Integration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
using Novus.Win32.Structures.User32;
1515
using SmartImage.Lib;
1616
using Command = Novus.OS.Command;
17-
using SmartImage.Shell;
1817
using Clipboard = Novus.Win32.Clipboard;
18+
using SmartImage.Utilities;
1919

2020
#endregion
2121

SmartImage 3/CliMain.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Read Stanton SmartImage CliMain.cs
2+
// 2023-01-30 @ 10:37 PM
3+
4+
using System.Collections.Concurrent;
5+
using SmartImage.Lib;
6+
using SmartImage.Lib.Engines;
7+
using SmartImage.Lib.Results;
8+
using Spectre.Console;
9+
10+
// ReSharper disable InconsistentNaming
11+
12+
namespace SmartImage;
13+
14+
public class CliMain : IDisposable
15+
{
16+
private const int COMPLETE = 100;
17+
18+
private static readonly ProgressColumn[] PrgCol_1 =
19+
{
20+
new TaskDescriptionColumn()
21+
{
22+
Alignment = Justify.Left
23+
},
24+
new SpinnerColumn(),
25+
new ElapsedTimeColumn(),
26+
new ProgressBarColumn()
27+
};
28+
29+
static CliMain()
30+
{
31+
}
32+
33+
private static readonly Progress Prg_1 = AnsiConsole.Progress().Columns(PrgCol_1);
34+
35+
public static async Task RunCliAsync(string c)
36+
{
37+
var q = await Prg_1.StartAsync(async ctx =>
38+
{
39+
var t = ctx.AddTask("Validating input");
40+
41+
var qInner = await SearchQuery.TryCreateAsync(c);
42+
t.Increment(COMPLETE);
43+
return qInner;
44+
});
45+
46+
AnsiConsole.WriteLine($"{q}");
47+
48+
var url = await Prg_1.StartAsync(async (p) =>
49+
{
50+
var pt = p.AddTask($"Upload");
51+
var urlInner= await q.UploadAsync();
52+
pt.Increment(COMPLETE);
53+
return urlInner;
54+
55+
});
56+
57+
AnsiConsole.MarkupLine($"[green]{q.Upload}[/]");
58+
59+
var cfg = new SearchConfig();
60+
var sc = new SearchClient(cfg);
61+
62+
AnsiConsole.WriteLine($"{cfg}");
63+
64+
var cts = new CancellationTokenSource();
65+
66+
System.Console.CancelKeyPress += (sender, args) =>
67+
{
68+
args.Cancel = false;
69+
cts.Cancel();
70+
AnsiConsole.MarkupLine($"[red]Cancellation requested {args}[/]");
71+
};
72+
73+
var results = await Prg_1.StartAsync(async ctx =>
74+
{
75+
var ptMap = new Dictionary<BaseSearchEngine, ProgressTask>();
76+
77+
foreach (var e in sc.Engines) {
78+
var t = ctx.AddTask($"{e}");
79+
ptMap.Add(e, t);
80+
}
81+
82+
var pt1 = ctx.AddTask("[yellow]Searching[/]");
83+
var rg = new ConcurrentBag<SearchResult>();
84+
85+
sc.OnComplete += (sender, searchResults) =>
86+
{
87+
pt1.Increment(COMPLETE);
88+
};
89+
90+
sc.OnResult += (sender, result) =>
91+
{
92+
ptMap[result.Engine].Increment(COMPLETE);
93+
pt1.Increment((double) rg.Count / sc.Engines.Length);
94+
95+
};
96+
97+
var resultsInner = await sc.RunSearchAsync(q, cts.Token);
98+
99+
return resultsInner;
100+
});
101+
102+
foreach (SearchResult sr in results) {
103+
AnsiConsole.MarkupLine($"[cyan]{sr.Engine.Name}[/]");
104+
foreach (SearchResultItem sri in sr.Results) {
105+
AnsiConsole.MarkupLine($"[link={sri.Url}]{sr.Engine} #[/]");
106+
107+
}
108+
}
109+
}
110+
111+
public void Dispose() { }
112+
}

SmartImage 3/Program.cs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#nullable disable
22

33
global using static Kantan.Diagnostics.LogCategories;
4+
using Console = Spectre.Console.AnsiConsole;
5+
using System.CommandLine;
46
using System.Diagnostics;
57
using System.Runtime.CompilerServices;
68
// using Windows.UI.Notifications;
79
// using CommunityToolkit.WinUI.Notifications;
810
using Novus;
911
using Terminal.Gui;
10-
using SmartImage.Shell;
1112
using SmartImage.App;
13+
using SmartImage.Lib;
14+
using SmartImage.Lib.Engines;
15+
using SmartImage.Lib.Results;
16+
using SmartImage.Utilities;
17+
using Spectre.Console;
18+
using Command = System.CommandLine.Command;
1219

1320
#pragma warning disable CS0168
1421

@@ -23,54 +30,83 @@ namespace SmartImage;
2330

2431
public static class Program
2532
{
26-
private static ShellMain _main;
2733

2834
[ModuleInitializer]
2935
public static void Init()
3036
{
3137
Global.Setup();
3238
Trace.WriteLine("Init", Resources.Name);
3339
// Gui.Init();
34-
Console.Title = R2.Name;
40+
System.Console.Title = R2.Name;
3541

3642
if (Compat.IsWin) {
3743
ConsoleUtil.SetConsoleMode();
3844
}
3945

40-
Application.Init();
41-
4246
AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
4347
{
4448
Trace.WriteLine($"Exiting", R2.Name);
4549
};
4650
}
4751

48-
public static async Task Main(string[] args)
52+
public static async Task<int> Main(string[] args)
4953
{
5054
// Console.OutputEncoding = Encoding.Unicode;
5155

5256
// ToastNotificationManagerCompat.OnActivated += AppNotification.OnActivated;
5357

5458
#if TEST
5559
// args = new String[] { null };
56-
args = new[] { R2.Arg_Input, "https://i.imgur.com/QtCausw.png",R2.Arg_AutoSearch };
60+
// args = new[] { R2.Arg_Input, "https://i.imgur.com/QtCausw.png",R2.Arg_AutoSearch };
5761

5862
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
63+
64+
Debug.WriteLine($"TEST");
5965
#endif
66+
6067
bool cli = args is { } && args.Any();
6168

62-
_main = new ShellMain(args);
69+
if (cli && args.Contains(Resources.Arg_NoUI)) {
70+
71+
var main = new CliMain();
72+
73+
var rc = new RootCommand()
74+
{ };
75+
76+
var arg = new Option<string>(Resources.Arg_Input)
77+
{ };
78+
79+
var opt2 = new Option<bool>(Resources.Arg_NoUI)
80+
{
81+
Arity = ArgumentArity.Zero,
82+
83+
};
84+
85+
rc.AddOption(arg);
86+
rc.AddOption(opt2);
87+
88+
rc.SetHandler(CliMain.RunCliAsync, arg);
89+
90+
var i = await rc.InvokeAsync(args);
91+
92+
return i;
93+
}
94+
else {
95+
main1:
96+
Application.Init();
6397

64-
main1:
98+
var main = new ShellMain(args);
99+
object status;
65100

66-
object status;
101+
var run = main.RunAsync(null);
102+
status = await run;
67103

68-
var run = _main.RunAsync(null);
69-
status = await run;
104+
if (status is bool { } and true) {
105+
main.Dispose();
106+
goto main1;
107+
}
70108

71-
if (status is bool { } and true) {
72-
_main.Dispose();
73-
goto main1;
109+
return 0;
74110
}
75111
}
76112
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"profiles": {
3+
"WSL": {
4+
"commandName": "WSL2",
5+
"distributionName": ""
6+
},
7+
"SmartImage": {
8+
"commandName": "Project"
9+
},
10+
"CLI Test 1": {
11+
"commandName": "Project",
12+
"commandLineArgs": "--i \"C:\\Users\\Deci\\Pictures\\lilith___the_maid_i_hired_recently_is_mysterious_by_sciamano240_dfmo4dr.png\" --noui"
13+
},
14+
"CLI Test 2": {
15+
"commandName": "Project",
16+
"commandLineArgs": "--i \"https://i.imgur.com/QtCausw.png\" --noui"
17+
}
18+
}
19+
}

SmartImage 3/Resources.Designer.cs

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SmartImage 3/Resources.resx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
<value>Context Menu</value>
134134
</data>
135135
<data name="Arg_Input" xml:space="preserve">
136-
<value>-i</value>
136+
<value>--i</value>
137137
</data>
138138
<data name="Author" xml:space="preserve">
139139
<value>Read Stanton</value>
@@ -145,7 +145,7 @@
145145
<value>Canceled</value>
146146
</data>
147147
<data name="Arg_AutoSearch" xml:space="preserve">
148-
<value>-auto</value>
148+
<value>--auto</value>
149149
</data>
150150
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
151151
<data name="ding" type="System.Resources.ResXFileRef, System.Windows.Forms">
@@ -154,4 +154,7 @@
154154
<data name="hint" type="System.Resources.ResXFileRef, System.Windows.Forms">
155155
<value>Resources\hint.wav;System.IO.MemoryStream, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
156156
</data>
157+
<data name="Arg_NoUI" xml:space="preserve">
158+
<value>--noui</value>
159+
</data>
157160
</root>

SmartImage 3/ShellMain.Handlers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private void Browse_Clicked()
114114
if (!string.IsNullOrWhiteSpace(f)) {
115115
Tf_Input.DeleteAll();
116116
Debug.WriteLine($"Picked file: {f}", nameof(Browse_Clicked));
117+
117118
SetInputText(f);
118119
Btn_Run.SetFocus();
119120

SmartImage 3/ShellMain.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
using SmartImage.Lib;
1717
using SmartImage.Shell;
1818
using Terminal.Gui;
19-
using Clipboard = Novus.Win32.Clipboard;
2019
using Window = Terminal.Gui.Window;
2120
using SmartImage.Lib.Results;
21+
using SmartImage.Utilities;
2222

2323
// ReSharper disable IdentifierTypo
2424

@@ -217,7 +217,8 @@ public sealed partial class ShellMain : IDisposable
217217
#region Static
218218

219219
private static readonly TimeSpan TimeoutTimeSpan = TimeSpan.FromSeconds(1.5);
220-
220+
221+
[SupportedOSPlatform(Compat.OS)]
221222
private static readonly SoundPlayer Player = new(R2.hint);
222223

223224
#endregion
@@ -595,8 +596,6 @@ private async Task<bool> SetQuery(ustring text)
595596
return null;
596597
}
597598

598-
private static int m_prevSeq;
599-
600599
private bool ClipboardCallback(MainLoop c)
601600
{
602601

@@ -606,7 +605,7 @@ private bool ClipboardCallback(MainLoop c)
606605
* - Input is already ready
607606
* - Clipboard history contains it already
608607
*/
609-
608+
610609
int sequenceNumber = Novus.Win32.Clipboard.SequenceNumber;
611610

612611
var s = Tf_Input.Text.ToString();
@@ -637,10 +636,7 @@ private bool ClipboardCallback(MainLoop c)
637636
Debug.WriteLine($"{e.Message}", nameof(ClipboardCallback));
638637
}
639638

640-
finally {
641-
m_prevSeq = Clipboard.SequenceNumber;
642-
643-
}
639+
finally { }
644640

645641
return true;
646642
}

0 commit comments

Comments
 (0)