Refactor windows terminal classes

This commit is contained in:
crschnick 2024-03-21 03:57:53 +00:00
parent cde12be5c8
commit 11d6997752
8 changed files with 100 additions and 83 deletions

View file

@ -10,6 +10,7 @@ import io.xpipe.app.fxcomps.Comp;
import io.xpipe.app.fxcomps.util.PlatformThread; import io.xpipe.app.fxcomps.util.PlatformThread;
import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.storage.DataStorage; import io.xpipe.app.storage.DataStorage;
import io.xpipe.app.terminal.ExternalTerminalType;
import io.xpipe.app.util.ApplicationHelper; import io.xpipe.app.util.ApplicationHelper;
import io.xpipe.app.util.PasswordLockSecretValue; import io.xpipe.app.util.PasswordLockSecretValue;
import io.xpipe.core.util.InPlaceSecretValue; import io.xpipe.core.util.InPlaceSecretValue;

View file

@ -25,8 +25,6 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
public abstract boolean isAvailable(); public abstract boolean isAvailable();
public abstract boolean isSelectable();
@Override @Override
public String getId() { public String getId() {
return id; return id;

View file

@ -7,6 +7,7 @@ import io.xpipe.app.fxcomps.Comp;
import io.xpipe.app.fxcomps.impl.ChoiceComp; import io.xpipe.app.fxcomps.impl.ChoiceComp;
import io.xpipe.app.fxcomps.impl.StackComp; import io.xpipe.app.fxcomps.impl.StackComp;
import io.xpipe.app.fxcomps.impl.TextFieldComp; import io.xpipe.app.fxcomps.impl.TextFieldComp;
import io.xpipe.app.terminal.ExternalTerminalType;
import io.xpipe.app.util.OptionsBuilder; import io.xpipe.app.util.OptionsBuilder;
import io.xpipe.app.util.TerminalLauncher; import io.xpipe.app.util.TerminalLauncher;
import io.xpipe.app.util.ThreadHelper; import io.xpipe.app.util.ThreadHelper;

View file

@ -1,18 +1,21 @@
package io.xpipe.app.prefs; package io.xpipe.app.terminal;
import io.xpipe.app.ext.PrefsChoiceValue; import io.xpipe.app.ext.PrefsChoiceValue;
import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.prefs.ExternalApplicationType;
import io.xpipe.app.storage.DataStoreColor; import io.xpipe.app.storage.DataStoreColor;
import io.xpipe.app.util.*; import io.xpipe.app.util.ApplicationHelper;
import io.xpipe.app.util.LocalShell;
import io.xpipe.app.util.MacOsPermissions;
import io.xpipe.app.util.WindowsRegistry;
import io.xpipe.core.process.*; import io.xpipe.core.process.*;
import io.xpipe.core.store.FileNames;
import lombok.Getter; import lombok.Getter;
import lombok.Value; import lombok.Value;
import lombok.With; import lombok.With;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -92,69 +95,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
ExternalTerminalType WINDOWS_TERMINAL_PREVIEW = new ExternalTerminalType() {
@Override
public boolean supportsTabs() {
return true;
}
@Override
public void launch(LaunchConfiguration configuration) throws Exception {
// A weird behavior in Windows Terminal causes the trailing
// backslash of a filepath to escape the closing quote in the title argument
// So just remove that slash
var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle());
LocalShell.getShell()
.executeSimpleCommand(CommandBuilder.of()
.addFile(getPath().toString())
.add("-w", "1", "nt", "--title")
.addQuoted(fixedName)
.addFile(configuration.getScriptFile()));
}
private Path getPath() {
var local = System.getenv("LOCALAPPDATA");
return Path.of(local)
.resolve("Microsoft\\WindowsApps\\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\\wt.exe");
}
@Override
public boolean isAvailable() {
return Files.exists(getPath());
}
@Override
public String getId() {
return "app.windowsTerminalPreview";
}
};
ExternalTerminalType WINDOWS_TERMINAL = new SimplePathType("app.windowsTerminal", "wt.exe") {
@Override
public boolean supportsTabs() {
return true;
}
@Override
protected CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception {
// A weird behavior in Windows Terminal causes the trailing
// backslash of a filepath to escape the closing quote in the title argument
// So just remove that slash
var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle());
var toExec = !ShellDialects.isPowershell(LocalShell.getShell())
? CommandBuilder.of().addFile(configuration.getScriptFile())
: CommandBuilder.of()
.add("powershell", "-ExecutionPolicy", "Bypass", "-File")
.addQuoted(configuration.getScriptFile());
return CommandBuilder.of()
.add("-w", "1", "nt", "--title")
.addQuoted(fixedName)
.add(toExec);
}
};
ExternalTerminalType ALACRITTY_WINDOWS = new SimplePathType("app.alacritty", "alacritty") { ExternalTerminalType ALACRITTY_WINDOWS = new SimplePathType("app.alacritty", "alacritty") {
@Override @Override
@ -692,8 +632,8 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
TABBY_WINDOWS, TABBY_WINDOWS,
ALACRITTY_WINDOWS, ALACRITTY_WINDOWS,
WEZ_WINDOWS, WEZ_WINDOWS,
WINDOWS_TERMINAL_PREVIEW, WindowsTerminalType.WINDOWS_TERMINAL_PREVIEW,
WINDOWS_TERMINAL, WindowsTerminalType.WINDOWS_TERMINAL,
CMD, CMD,
PWSH, PWSH,
POWERSHELL); POWERSHELL);
@ -845,11 +785,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
public boolean isAvailable() { public boolean isAvailable() {
return true; return true;
} }
@Override
public boolean isSelectable() {
return true;
}
} }
abstract class MacOsType extends ExternalApplicationType.MacApplication implements ExternalTerminalType { abstract class MacOsType extends ExternalApplicationType.MacApplication implements ExternalTerminalType {
@ -865,11 +800,6 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
public PathCheckType(String id, String executable) { public PathCheckType(String id, String executable) {
super(id, executable); super(id, executable);
} }
@Override
public boolean isSelectable() {
return true;
}
} }
@Getter @Getter
@ -887,4 +817,5 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
protected abstract CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception; protected abstract CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception;
} }
} }

View file

@ -0,0 +1,85 @@
package io.xpipe.app.terminal;
import io.xpipe.app.util.LocalShell;
import io.xpipe.core.process.CommandBuilder;
import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.store.FileNames;
import java.nio.file.Files;
import java.nio.file.Path;
public class WindowsTerminalType {
public static final ExternalTerminalType WINDOWS_TERMINAL = new ExternalTerminalType.SimplePathType("app.windowsTerminal", "wt.exe") {
@Override
protected CommandBuilder toCommand(LaunchConfiguration configuration) throws Exception {
return WindowsTerminalType.toCommand(configuration);
}
@Override
public boolean supportsTabs() {
return true;
}
@Override
public boolean supportsColoredTitle() {
return false;
}
};
public static final ExternalTerminalType WINDOWS_TERMINAL_PREVIEW = new ExternalTerminalType() {
@Override
public boolean supportsColoredTitle() {
return false;
}
@Override
public boolean supportsTabs() {
return true;
}
@Override
public void launch(ExternalTerminalType.LaunchConfiguration configuration) throws Exception {
LocalShell.getShell()
.executeSimpleCommand(CommandBuilder.of()
.addFile(getPath().toString())
.add(toCommand(configuration)));
}
private Path getPath() {
var local = System.getenv("LOCALAPPDATA");
return Path.of(local)
.resolve("Microsoft\\WindowsApps\\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\\wt.exe");
}
@Override
public boolean isAvailable() {
return Files.exists(getPath());
}
@Override
public String getId() {
return "app.windowsTerminalPreview";
}
};
private static CommandBuilder toCommand(ExternalTerminalType.LaunchConfiguration configuration) throws Exception {
// A weird behavior in Windows Terminal causes the trailing
// backslash of a filepath to escape the closing quote in the title argument
// So just remove that slash
var fixedName = FileNames.removeTrailingSlash(configuration.getColoredTitle());
var toExec = !ShellDialects.isPowershell(LocalShell.getShell()) ?
CommandBuilder.of().addFile(configuration.getScriptFile()) :
CommandBuilder.of().add("powershell", "-ExecutionPolicy", "Bypass", "-File").addQuoted(configuration.getScriptFile());
var cmd = CommandBuilder.of().add("-w", "1", "nt");
if (configuration.getColor() != null) {
cmd.add("--tabColor").addQuoted(configuration.getColor().toHexString());
}
return cmd.add("--title").addQuoted(fixedName).add(toExec);
}
}

View file

@ -45,7 +45,7 @@ public class FileOpener {
public static void openInTextEditor(FileSystem.FileEntry entry) { public static void openInTextEditor(FileSystem.FileEntry entry) {
var editor = AppPrefs.get().externalEditor().getValue(); var editor = AppPrefs.get().externalEditor().getValue();
if (editor == null || !editor.isSelectable()) { if (editor == null) {
return; return;
} }

View file

@ -3,7 +3,7 @@ package io.xpipe.app.util;
import io.xpipe.app.core.AppI18n; import io.xpipe.app.core.AppI18n;
import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.prefs.ExternalTerminalType; import io.xpipe.app.terminal.ExternalTerminalType;
import io.xpipe.app.storage.DataStorage; import io.xpipe.app.storage.DataStorage;
import io.xpipe.app.storage.DataStoreEntry; import io.xpipe.app.storage.DataStoreEntry;
import io.xpipe.core.process.ProcessControl; import io.xpipe.core.process.ProcessControl;

View file

@ -41,6 +41,7 @@ open module io.xpipe.app {
exports io.xpipe.app.browser; exports io.xpipe.app.browser;
exports io.xpipe.app.browser.icon; exports io.xpipe.app.browser.icon;
exports io.xpipe.app.core.check; exports io.xpipe.app.core.check;
exports io.xpipe.app.terminal;
requires com.sun.jna; requires com.sun.jna;
requires com.sun.jna.platform; requires com.sun.jna.platform;