GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: improvements to `exec` command (#120)
lars-berger3110c42c7136
diff --git a/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs b/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.csindex 6deec506..65f839e8 100644--- a/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs+++ b/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs@@ -1,4 +1,6 @@-using System.Diagnostics;+using System;+using System.Diagnostics;+using System.Windows.Forms;using GlazeWM.Domain.Common.Commands;using GlazeWM.Infrastructure.Bussing;@@ -8,14 +10,30 @@ namespace GlazeWM.Domain.Common.CommandHandlers{public CommandResponse Handle(ExecProcessCommand command){- using var process = new Process();- process.StartInfo = new ProcessStartInfo+ var processName = command.ProcessName;+ var args = command.Args;++ try+ {+ using var process = new Process();+ process.StartInfo = new ProcessStartInfo+ {+ // Expand env variables in the process name (eg. "%ProgramFiles%").+ FileName = Environment.ExpandEnvironmentVariables(processName),+ Arguments = string.Join(" ", args),+ UseShellExecute = true,+ // Set user profile directory as the working dir. This affects the starting directory+ // of terminal processes (eg. CMD, Git bash, etc).+ WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),+ };+ process.Start();+ }+ catch (Exception exception){- FileName = command.ProcessName,- Arguments = string.Join(" ", command.Args),- UseShellExecute = true- };- process.Start();+ // TODO: Link to documentation for `exec` command (no proper documentation yet).+ // TODO: Handle non-fatal exceptions in a generic way.+ MessageBox.Show(exception.Message);+ }return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Common/Commands/StartProcessCommand.cs b/GlazeWM.Domain/Common/Commands/StartProcessCommand.csindex de1cc3f3..101a5002 100644--- a/GlazeWM.Domain/Common/Commands/StartProcessCommand.cs+++ b/GlazeWM.Domain/Common/Commands/StartProcessCommand.cs@@ -1,13 +1,14 @@-using GlazeWM.Infrastructure.Bussing;+using System.Collections.Generic;+using GlazeWM.Infrastructure.Bussing;namespace GlazeWM.Domain.Common.Commands{public class ExecProcessCommand : Command{- public string ProcessName { get; init; }- public string[] Args { get; init; }+ public string ProcessName { get; }+ public List<string> Args { get; }- public ExecProcessCommand(string processName, string[] args)+ public ExecProcessCommand(string processName, List<string> args){ProcessName = processName;Args = args;diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.csindex 9517f606..6d1752f0 100644--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs@@ -1,4 +1,5 @@using System;+using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;using GlazeWM.Domain.Common.Commands;@@ -28,9 +29,29 @@ namespace GlazeWM.Domain.UserConfigspublic static string FormatCommand(string commandString){- var formattedCommandString = commandString.Trim().ToLowerInvariant();+ var trimmedCommandString = commandString.Trim().ToLowerInvariant();+var multipleSpacesRegex = new Regex(@"\s+");- return multipleSpacesRegex.Replace(formattedCommandString, " ");+ var formattedCommandString = multipleSpacesRegex.Replace(trimmedCommandString, " ");++ var caseSensitiveCommandRegex = new List<Regex>+ {+ new Regex("^(exec).*", RegexOptions.IgnoreCase),+ };++ // Some commands are partially case-sensitive (eg. `exec ...`). To handle such cases, only+ // format part of the command string to be lowercase.+ foreach (var regex in caseSensitiveCommandRegex)+ {+ if (regex.IsMatch(formattedCommandString))+ {+ return regex.Replace(formattedCommandString, (Match match) =>+ match.Value.ToLowerInvariant()+ );+ }+ }++ return formattedCommandString.ToLowerInvariant();}public void ValidateCommand(string commandString)@@ -63,8 +84,8 @@ namespace GlazeWM.Domain.UserConfigs: new NoopCommand(),"reload" => ParseReloadCommand(commandParts),"exec" => new ExecProcessCommand(- commandParts[1],- commandParts.Length > 2 ? commandParts[2..] : Array.Empty<string>()+ ExtractProcessName(string.Join(" ", commandParts[1..])),+ ExtractProcessArgs(string.Join(" ", commandParts[1..]))),// TODO: Temporary hack to avoid errors during `ValidateCommand`."ignore" => new NoopCommand(),@@ -199,9 +220,30 @@ namespace GlazeWM.Domain.UserConfigsprivate bool IsValidWorkspace(string workspaceName){var workspaceConfig = _userConfigService.GetWorkspaceConfigByName(workspaceName);+return workspaceConfig is not null;}+ public static string ExtractProcessName(string processNameAndArgs)+ {+ var hasSingleQuotes = processNameAndArgs.StartsWith("'");++ return hasSingleQuotes+ ? processNameAndArgs.Split("'")[1]+ : processNameAndArgs.Split(" ")[0];+ }++ public static List<string> ExtractProcessArgs(string processNameAndArgs)+ {+ var hasSingleQuotes = processNameAndArgs.StartsWith("'");++ var args = hasSingleQuotes+ ? processNameAndArgs.Split("'")[2..]+ : processNameAndArgs.Split(" ")[1..];++ return args.Where(arg => !string.IsNullOrWhiteSpace(arg)).ToList();+ }+private static RectDelta ShorthandToRectDelta(string shorthand){var shorthandParts = shorthand.Split(" ")