From 1ca9e2c751892d7d41f67db558985108870fc37f Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Sat, 10 Dec 2022 00:16:05 +0100 Subject: [PATCH] Small fixes for shells --- .../core/process/ShellProcessControl.java | 33 +++++++++---------- .../java/io/xpipe/core/process/ShellType.java | 8 +++-- .../io/xpipe/core/store/MachineStore.java | 8 ++--- .../extension/util/CustomComboBoxBuilder.java | 3 +- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java index 7dbcae7d..25ee18a7 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -64,42 +64,41 @@ public interface ShellProcessControl extends ProcessControl { SecretValue getElevationPassword(); default ShellProcessControl subShell(@NonNull ShellType type) { - return subShell(type.openCommand()).elevation(getElevationPassword()); + return subShell(p -> type.openCommand(), (shellProcessControl, s) -> { + return s == null ? type.openCommand() : type.switchTo(s); + }) + .elevation(getElevationPassword()); } default ShellProcessControl subShell(@NonNull List command) { - return subShell(shellProcessControl -> shellProcessControl.getShellType().flatten(command)); + return subShell( + shellProcessControl -> shellProcessControl.getShellType().flatten(command), null); } default ShellProcessControl subShell(@NonNull String command) { - return subShell(processControl -> command); + return subShell(processControl -> command, null); } - ShellProcessControl subShell(@NonNull Function command); - - default ShellProcessControl consoleCommand(@NonNull String command) { - return consoleCommand((shellProcessControl, c) -> command); - } - - ShellProcessControl consoleCommand(@NonNull BiFunction command); + ShellProcessControl subShell( + @NonNull Function command, + BiFunction terminalCommand); void executeCommand(String command) throws Exception; @Override ShellProcessControl start() throws Exception; - default CommandProcessControl commandListFunction(Function> command) { - return commandFunction(shellProcessControl -> shellProcessControl.getShellType().flatten(command.apply(shellProcessControl))); - } + CommandProcessControl command(Function command); - CommandProcessControl commandFunction(Function command); + CommandProcessControl command( + Function command, Function terminalCommand); - default CommandProcessControl command(String command){ - return commandFunction(shellProcessControl -> command); + default CommandProcessControl command(String command) { + return command(shellProcessControl -> command); } default CommandProcessControl command(List command) { - return commandFunction(shellProcessControl -> shellProcessControl.getShellType().flatten(command)); + return command(shellProcessControl -> shellProcessControl.getShellType().flatten(command)); } void exitAndWait() throws IOException; diff --git a/core/src/main/java/io/xpipe/core/process/ShellType.java b/core/src/main/java/io/xpipe/core/process/ShellType.java index f0571419..56bd0e43 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellType.java +++ b/core/src/main/java/io/xpipe/core/process/ShellType.java @@ -24,7 +24,11 @@ public interface ShellType { default String flatten(List command) { return command.stream() - .map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s) + .map(s -> s.contains(" ") + && !(s.startsWith("\"") && s.endsWith("\"")) + && !(s.startsWith("'") && s.endsWith("'")) + ? "\"" + s + "\"" + : s) .collect(Collectors.joining(" ")); } @@ -70,7 +74,7 @@ public interface ShellType { String getPrintVariableCommand(String prefix, String name); - List openCommand(); + String openCommand(); String switchTo(String cmd); diff --git a/core/src/main/java/io/xpipe/core/store/MachineStore.java b/core/src/main/java/io/xpipe/core/store/MachineStore.java index 62f63521..9e4ae8c3 100644 --- a/core/src/main/java/io/xpipe/core/store/MachineStore.java +++ b/core/src/main/java/io/xpipe/core/store/MachineStore.java @@ -11,19 +11,19 @@ public interface MachineStore extends FileSystemStore, ShellStore { @Override public default InputStream openInput(String file) throws Exception { - return create().commandListFunction(proc -> proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file))) + return create().command(proc -> proc.getShellType().flatten(proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file)))) .startExternalStdout(); } @Override public default OutputStream openOutput(String file) throws Exception { - return create().commandFunction(proc -> proc.getShellType().createFileWriteCommand(proc.getOsType().normalizeFileName(file))) + return create().command(proc -> proc.getShellType().createFileWriteCommand(proc.getOsType().normalizeFileName(file))) .startExternalStdin(); } @Override public default boolean exists(String file) throws Exception { - try (var pc = create().commandFunction(proc -> proc.getShellType().createFileExistsCommand(proc.getOsType().normalizeFileName(file))) + try (var pc = create().command(proc -> proc.getShellType().createFileExistsCommand(proc.getOsType().normalizeFileName(file))) .start()) { return pc.discardAndCheckExit(); } @@ -31,7 +31,7 @@ public interface MachineStore extends FileSystemStore, ShellStore { @Override public default boolean mkdirs(String file) throws Exception { - try (var pc = create().commandListFunction(proc -> proc.getShellType().createMkdirsCommand(proc.getOsType().normalizeFileName(file))) + try (var pc = create().command(proc -> proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(proc.getOsType().normalizeFileName(file)))) .start()) { return pc.discardAndCheckExit(); } diff --git a/extension/src/main/java/io/xpipe/extension/util/CustomComboBoxBuilder.java b/extension/src/main/java/io/xpipe/extension/util/CustomComboBoxBuilder.java index 1e910f5a..1887c00d 100644 --- a/extension/src/main/java/io/xpipe/extension/util/CustomComboBoxBuilder.java +++ b/extension/src/main/java/io/xpipe/extension/util/CustomComboBoxBuilder.java @@ -97,7 +97,7 @@ public class CustomComboBoxBuilder { cb.setButtonCell(new SelectedCell()); SimpleChangeListener.apply(selected, c -> { var item = nodeMap.entrySet().stream() - .filter(e -> e.getValue() != null && e.getValue().equals(c)) + .filter(e -> Objects.equals(c, e.getValue())) .map(e -> e.getKey()) .findAny() .orElse(null); @@ -118,7 +118,6 @@ public class CustomComboBoxBuilder { }); if (filterPredicate != null) { - SimpleChangeListener.apply(filterString, c -> { var filteredNodes = nodes.stream() .filter(e -> e.equals(cb.getValue())