Skip to content

Commit 16c30e9

Browse files
committed
Refactor Shell Command
1 parent b51e5d2 commit 16c30e9

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,5 @@ private static string GetWindowTitle(IntPtr hwnd)
5959
GetWindowText(hwnd, sb, sb.Capacity);
6060
return sb.ToString();
6161
}
62-
63-
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "")
64-
{
65-
var info = new ProcessStartInfo
66-
{
67-
FileName = fileName,
68-
WorkingDirectory = workingDirectory,
69-
Arguments = arguments,
70-
Verb = verb
71-
};
72-
73-
return info;
74-
}
7562
}
7663
}

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -193,51 +193,63 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
193193
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
194194
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
195195

196-
ProcessStartInfo info;
197-
if (_settings.Shell == Shell.Cmd)
196+
ProcessStartInfo info = new()
198197
{
199-
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
200-
201-
info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
202-
}
203-
else if (_settings.Shell == Shell.Powershell)
198+
Verb = runAsAdministratorArg,
199+
WorkingDirectory = workingDirectory,
200+
};
201+
switch (_settings.Shell)
204202
{
205-
string arguments;
206-
if (_settings.LeaveShellOpen)
207-
{
208-
arguments = $"-NoExit \"{command}\"";
209-
}
210-
else
211-
{
212-
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
213-
}
203+
case Shell.Cmd:
204+
{
205+
info.FileName = "cmd.exe";
206+
info.ArgumentList.Add(_settings.LeaveShellOpen ? "/k" : "/c");
207+
info.ArgumentList.Add(command);
208+
break;
209+
}
214210

215-
info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
216-
}
217-
else if (_settings.Shell == Shell.RunCommand)
218-
{
219-
var parts = command.Split(new[] { ' ' }, 2);
220-
if (parts.Length == 2)
221-
{
222-
var filename = parts[0];
223-
if (ExistInPath(filename))
211+
case Shell.Powershell:
224212
{
225-
var arguments = parts[1];
226-
info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
213+
info.FileName = "powershell.exe";
214+
if (_settings.LeaveShellOpen)
215+
{
216+
info.ArgumentList.Add("-NoExit");
217+
info.ArgumentList.Add(command);
218+
}
219+
else
220+
{
221+
info.ArgumentList.Add("-Command");
222+
info.ArgumentList.Add(command);
223+
}
224+
break;
227225
}
228-
else
226+
227+
case Shell.RunCommand:
229228
{
230-
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
229+
var parts = command.Split(new[] { ' ' }, 2);
230+
if (parts.Length == 2)
231+
{
232+
var filename = parts[0];
233+
if (ExistInPath(filename))
234+
{
235+
var arguments = parts[1];
236+
info.FileName = filename;
237+
info.ArgumentList.Add(arguments);
238+
}
239+
else
240+
{
241+
info.FileName = command;
242+
}
243+
}
244+
else
245+
{
246+
info.FileName = command;
247+
}
248+
249+
break;
231250
}
232-
}
233-
else
234-
{
235-
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
236-
}
237-
}
238-
else
239-
{
240-
throw new NotImplementedException();
251+
default:
252+
throw new NotImplementedException();
241253
}
242254

243255
info.UseShellExecute = true;

0 commit comments

Comments
 (0)