-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
199 lines (162 loc) · 6.7 KB
/
Copy pathProgram.cs
File metadata and controls
199 lines (162 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using FishTools.App;
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cts.Cancel();
};
try
{
await RunAsync(cts.Token);
}
catch (OperationCanceledException) { }
internal static partial class Program
{
public static async Task RunAsync(CancellationToken cancellationToken = default)
{
var paths = new AppPaths(AppContext.BaseDirectory);
paths.EnsureCreated();
var settings = AppSettingsStore.Load(paths.SettingsFilePath);
var context = new ToolContext(paths, settings);
var tools = ToolCatalog.Create().OrderBy(t => ToolCategories.GetOrder(t.Category)).ThenBy(t => t.Name).ToArray();
while (!cancellationToken.IsCancellationRequested)
{
ConsoleUi.ResetScreen("Main Menu", ConsoleColor.Green);
var categoryGroups = tools.Where(settings.IsEnabled).GroupBy(t => t.Category).OrderBy(g => ToolCategories.GetOrder(g.Key)).ToList();
var menuOptions = categoryGroups.Select(g => $"{g.Key} ({g.Count()})").Append("Search for a tool").Append("Manage tool library").Append("Exit").ToList();
var selected = ConsoleUi.ShowMenu("Select a section", menuOptions);
if (selected < categoryGroups.Count)
{
await ShowCategoryAsync(categoryGroups[selected].Key, tools, context, cancellationToken);
continue;
}
var offset = selected - categoryGroups.Count;
switch (offset)
{
case 0:
await SearchAsync(tools, context, cancellationToken);
break;
case 1:
ShowManageTools(tools, context);
break;
default:
return;
}
}
}
private static async Task ShowCategoryAsync(string category, IReadOnlyList<ITool> tools, ToolContext context, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
ConsoleUi.ResetScreen(category, ConsoleColor.Cyan);
var categoryTools = tools.Where(t => t.Category == category && context.Settings.IsEnabled(t)).OrderBy(t => t.Name).ToArray();
if (categoryTools.Length == 0)
{
ConsoleUi.Warning("No tools are currently enabled in this category.");
ConsoleUi.Pause();
return;
}
var options = categoryTools.Select(t => t.Name).Append("Go back").ToArray();
var selected = ConsoleUi.ShowMenu("Available Tools", options);
if (selected == categoryTools.Length)
return;
var tool = categoryTools[selected];
await ShowToolDetailAsync(tool, context, cancellationToken);
}
}
private static async Task ShowToolDetailAsync(ITool tool, ToolContext context, CancellationToken cancellationToken)
{
ConsoleUi.ResetScreen(tool.Name, ConsoleColor.Magenta);
ConsoleUi.Section("Description");
ConsoleUi.Info(tool.Description);
Fish.Console.FishConsole.WriteLine();
if (!ConsoleUi.Confirm("Launch this tool?"))
return;
await RunToolSafeAsync(tool, context, cancellationToken);
}
private static async Task RunToolSafeAsync(ITool tool, ToolContext context, CancellationToken cancellationToken = default)
{
try
{
await tool.RunAsync(context);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception ex)
{
ConsoleUi.ResetScreen(tool.Name, ConsoleColor.Red);
ConsoleUi.Error("An error occurred during execution:");
ConsoleUi.Info(ex.Message);
ConsoleUi.Pause();
}
}
private static async Task SearchAsync(IReadOnlyList<ITool> tools, ToolContext context, CancellationToken cancellationToken)
{
ConsoleUi.ResetScreen("Search Tools");
var query = ConsoleUi.Prompt("Enter tool name or description").Trim();
if (string.IsNullOrEmpty(query))
return;
var matches = tools.Where(t => t.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || t.Description.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList();
if (matches.Count == 0)
{
ConsoleUi.Warning("No tools matched your search.");
ConsoleUi.Pause();
return;
}
var options = matches.Select(t => $"{t.Name} [{t.Category}]").Append("Back").ToList();
var selected = ConsoleUi.ShowMenu($"Results for \"{query}\"", options);
if (selected >= matches.Count)
return;
var tool = matches[selected];
if (!context.Settings.IsEnabled(tool))
{
ConsoleUi.Warning("This tool is currently disabled.");
if (!ConsoleUi.Confirm("Enable and run it?"))
return;
context.Settings.SetEnabled(tool, true);
}
await ShowToolDetailAsync(tool, context, cancellationToken);
}
private static void ShowManageTools(IReadOnlyList<ITool> tools, ToolContext context)
{
while (true)
{
ConsoleUi.ResetScreen("Manage Tool Library", ConsoleColor.Yellow);
var enabledCount = tools.Count(context.Settings.IsEnabled);
ConsoleUi.Info($"Enabled: {enabledCount} / {tools.Count}");
Fish.Console.FishConsole.WriteLine();
var sortedTools = tools.OrderBy(t => ToolCategories.GetOrder(t.Category)).ThenBy(t => t.Name).ToList();
var options = sortedTools
.Select(t =>
{
var status = context.Settings.IsEnabled(t) ? "[ON] " : "[OFF]";
return $"{status} {t.Name, -24} {t.Category}";
})
.Append("Enable all")
.Append("Disable all")
.Append("Back")
.ToList();
var selected = ConsoleUi.ShowMenu("Toggle tools", options);
if (selected < sortedTools.Count)
{
var tool = sortedTools[selected];
context.Settings.SetEnabled(tool, !context.Settings.IsEnabled(tool));
continue;
}
var offset = selected - sortedTools.Count;
switch (offset)
{
case 0:
context.Settings.EnableAll(tools);
break;
case 1:
context.Settings.DisableAll(tools);
break;
default:
return;
}
}
}
}