-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
324 lines (303 loc) · 12.9 KB
/
Main.cs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using Flow.Launcher.Plugin;
using System.Windows;
using System.Linq.Expressions;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Collections;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;
using Microsoft.VisualBasic;
namespace Flow.Launcher.Plugin.Add2Path;
public class Add2Path : IPlugin
{
// ReSharper disable once InconsistentNaming
private PluginInitContext Context;
private struct Add2PathCommand
{
public string Name;
public string Description;
public Func<ActionContext, bool> Action { get; set; }
public string GetDefaultSubtitle()
{
string buildSubtitle = Name;
if (Description.Contains("<system_or_user>"))
{
buildSubtitle += " <!system or leave blank>";
}
if (Description.Contains("<folder_path>"))
{
buildSubtitle += " <folder_path>";
}
return buildSubtitle;
}
}
public void Init(PluginInitContext context)
{
Context = context;
}
private static bool _IsValidPath(string path)
{
// From https://stackoverflow.com/questions/2435894/how-do-i-check-for-illegal-characters-in-a-path (modified)
for (var i = 0; i < path.Length; i++)
{
int c = path[i];
if (c == '<' || c == '>' || c == '|' || c == '*' || c == '?' || c < 32)
return false;
}
return true;
}
private static bool IsDirectory(string path)
{
// From https://learn.microsoft.com/en-us/answers/questions/1128930/c-filedirectory-check and https://stackoverflow.com/a/1395226/22081657
try
{
// Fails if path does not exist
FileAttributes attr = File.GetAttributes(path);
return attr.HasFlag(FileAttributes.Directory);
}
catch
{
// Fallback if path does not exist: Check if it has an extension (if it has an extension it's a file, if not it's a directory)
return !Path.HasExtension(path);
}
}
private static void ParseValuePart(string valuepart, out string? folderPath, out bool system)
{
system = false;
folderPath = valuepart;
if (valuepart.StartsWith("!system"))
{
system = true;
folderPath = valuepart["!system".Length..].TrimStart();
}
folderPath = folderPath.Trim(new char[3] { '"', ' ', '\t' }); // trim whitespace and quotes
if (folderPath == "")
{
folderPath = null;
}
}
private bool ValidateFolderPath(string? folderPath)
{
if (String.IsNullOrWhiteSpace(folderPath))
{
return false;
}
if (!_IsValidPath(folderPath) || folderPath.Contains("\""))
{
Context.API.ShowMsgError("Invalid PATH Value", $"\"{folderPath}\" is not a valid folder path - verify that the path is correct and valid");
return false;
}
if (!IsDirectory(folderPath))
{
Context.API.ShowMsg("WARNING: This is a file path", $"PATH is for folder paths, but \"{folderPath}\" is a file path. File paths are ignored in PATH." +
" To add this file to PATH, add the folder it is in rather than the file itself.");
}
if (!Directory.Exists(folderPath))
{
Context.API.ShowMsg("WARNING: Directory does not exist", $"The directory \"{folderPath}\" does not exist. Did you mistype it?");
}
return true;
}
private static string getResultSubtitle(Add2PathCommand command, Query query)
{
// When blank, list all args: Like "add <!system or leave blank> <folder path>" or get <!system or leave blank>
string blankSubtitle = !String.IsNullOrEmpty(query.ActionKeyword) ? $"{query.ActionKeyword} {command.GetDefaultSubtitle()}" : command.GetDefaultSubtitle();
if (!query.Search.TrimStart().StartsWith(command.Name))
{
return blankSubtitle;
}
string valuepart = query.Search.TrimStart()[command.Name.Length..].TrimStart();
if (String.IsNullOrEmpty(valuepart) && command.Description.Contains("<folder_path>"))
{
return blankSubtitle;
}
ParseValuePart(valuepart, out string? folderPath, out bool system);
string subtitle = command.Description;
subtitle = folderPath is not null ? subtitle.Replace("<folder_path>", $"\"{folderPath}\"") : subtitle;
subtitle = system ? subtitle.Replace("<system_or_user>", "system") : subtitle.Replace("<system_or_user>", "user");
return subtitle;
}
private static List<Result> GetMatchedResults(List<Result> allResults, string searchQuery)
{
/* Finds all matched results based on the user's query
* A result is considered a match if its title is contained in the user's input
* For example, using a result with title "add":
* "add" - match
* " add" - match
* "add foo" - match
* "a" - match
* "ad" - match
* ". add" - not a match
* "addx" - not a match
* "adx" - not a match
*
* If the query is empty or whitespace, all results will be considered matches
*/
List<Result> results = new List<Result>();
foreach (Result result in allResults)
{
// Add a space to make sure the user has not typed the start of the command but not the command itself
// For example, `addx` instead of just `add`
string title = result.Title + " ";
var zippedTitleAndQuery = title.TrimStart().Zip(searchQuery);
bool match = true;
foreach (List<char> chars in zippedTitleAndQuery)
{
if (chars[0] != chars[1])
{
match = false;
break;
}
}
if (match) { results.Add(result); }
}
return results;
}
public List<Result> Query(Query query)
{
List<Add2PathCommand> commands = new List<Add2PathCommand>
{
new Add2PathCommand
{
Name = "add",
Description = "add <folder_path> to <system_or_user> PATH",
Action = c =>
{
if (!query.Search.TrimStart().StartsWith("add"))
{
return false;
}
string valuepart = query.Search.TrimStart()["add".Length..].TrimStart();
ParseValuePart(valuepart, out string? folderPath, out bool system);
if (folderPath is null || !ValidateFolderPath(folderPath))
{
return false;
}
if (PathUtils.Contains(folderPath, system))
{
Context.API.ShowMsg($"Cannot add {$"\"{folderPath}\""} to {(system ? "system" : "user")} PATH because it is already in {(system ? "system" : "user")} PATH.");
return true;
}
try {
PathUtils.Add(folderPath, system);
if (!PathUtils.Contains(folderPath, system))
{
throw new Exception("Sanity check: Though no errors were detected, value is still not in PATH.");
}
} catch (Exception ex){
Context.API.ShowMsgError("Failed to add to PATH", $"Failed to add {$"\"{folderPath}\""} to {(system ? "system" : "user")} PATH - {ex.Message}");
return true;
}
Context.API.ShowMsg("Successfully added to PATH", $"Successfully added {$"\"{folderPath}\""} to {(system ? "system" : "user")} PATH.");
return true;
},
},
new Add2PathCommand
{
Name = "remove",
Description = "remove <folder_path> from <system_or_user> PATH",
Action = c =>
{
if (!query.Search.TrimStart().StartsWith("remove"))
{
return false;
}
string valuepart = query.Search.TrimStart()["remove".Length..].TrimStart();
ParseValuePart(valuepart, out string? folderPath, out bool system);
if (folderPath is null || !ValidateFolderPath(folderPath))
{
return false;
}
if (!PathUtils.Contains(folderPath, system))
{
Context.API.ShowMsg("Folder is not in PATH",
$"Cannot remove {$"\"{folderPath}\""} from {(system ? "system" : "user")} PATH because it is not in PATH." +
$" Double-check the file path and whether it is in system or user PATH." +
$" Use the \"{query.ActionKeyword} list\" command to list all items in PATH.");
return true;
}
try {
PathUtils.Remove(folderPath, system);
if (PathUtils.Contains(folderPath, system))
{
throw new Exception("Sanity check: Though no errors were detected, value is still in PATH.");
}
} catch (Exception ex){
Context.API.ShowMsgError($"Failed to remove from PATH",
$"Failed to remove {$"\"{folderPath}\""} from {(system ? "system" : "user")} PATH - {ex.Message}");
return true;
}
Context.API.ShowMsg($"Successfully removed from PATH",
$"Successfully removed {$"\"{folderPath}\""} from {(system ? "system" : "user")} PATH.");
return true;
},
},
new Add2PathCommand
{
Name = "list",
Description = "list items in <system_or_user> PATH",
Action = c =>
{
if (!query.Search.TrimStart().StartsWith("list"))
{
return false;
}
string valuepart = query.Search.TrimStart()["list".Length..].TrimStart();
ParseValuePart(valuepart, out _, out bool system);
string pathliststring;
try
{
pathliststring = String.Join("\n", PathUtils.Get(system));
} catch (Exception ex)
{
Context.API.ShowMsgError("Failed to get PATH list", $"Failed to get {(system ? "system" : "user")} PATH list - {ex.Message}");
return true;
}
Clipboard.SetText(pathliststring);
Context.API.ShowMsg($"Copied {(system ? "system" : "user")} PATH to clipboard");
return true;
},
},
new Add2PathCommand
{
Name = "get",
Description = "get raw string of <system_or_user> PATH",
Action = c =>
{
if (!query.Search.TrimStart().StartsWith("get"))
{
return false;
}
string valuepart = query.Search.TrimStart()["get".Length..].TrimStart();
ParseValuePart(valuepart, out _, out bool system);
string pathstring;
try
{
pathstring = PathUtils.GetFullString(system);
} catch (Exception ex)
{
Context.API.ShowMsgError("Failed to get PATH", $"Failed to get {(system ? "system" : "user")} PATH - {ex.Message}");
return true;
}
Clipboard.SetText(pathstring);
Context.API.ShowMsg($"Copied {(system ? "system" : "user")} PATH to clipboard");
return true;
},
}
};
List<Result> allResults = (from command in commands select new Result
{
Title = command.Name,
SubTitle = getResultSubtitle(command, query),
Action = command.Action,
IcoPath = "icon.png"
}).ToList();
return GetMatchedResults(allResults, query.Search);
}
}