Improve substitution of variables in custom commands

This commit is contained in:
crschnick 2023-07-09 12:53:09 +00:00
parent 068e79bbe6
commit 35ca2ae716
3 changed files with 9 additions and 3 deletions

View file

@ -117,8 +117,7 @@ public interface ExternalEditorType extends PrefsChoiceValue {
}
var format = customCommand.contains("$file") ? customCommand : customCommand + " $file";
var fileString = file.toString().contains(" ") ? "\"" + file + "\"" : file.toString();
ApplicationHelper.executeLocalApplication(sc -> format.replace("$file", fileString), true);
ApplicationHelper.executeLocalApplication(sc -> ApplicationHelper.replaceFileArgument(format, "file", file.toString()), true);
}
@Override

View file

@ -349,7 +349,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
var format = custom.contains("$cmd") ? custom : custom + " $cmd";
try (var pc = LocalStore.getShell()) {
var toExecute = format.replace("$cmd", "\"" + file + "\"");
var toExecute = ApplicationHelper.replaceFileArgument(format, "cmd", file);
if (pc.getOsType().equals(OsType.WINDOWS)) {
toExecute = "start \"" + name + "\" " + toExecute;
} else {

View file

@ -9,6 +9,13 @@ import java.util.function.Function;
public class ApplicationHelper {
public static String replaceFileArgument(String format, String variable, String file) {
var fileString = file.contains(" ") ? "\"" + file + "\"" : file;
// Check if the variable is already quoted
var replaced = format.replace("\"$" + variable + "\"", fileString).replace("$" + variable, fileString);
return replaced;
}
public static void executeLocalApplication(Function<ShellControl, String> s, boolean detach) throws Exception {
try (var sc = LocalStore.getShell().start()) {
var cmd = detach ? ScriptHelper.createDetachCommand(sc, s.apply(sc)) : s.apply(sc);