Kitty fixes

This commit is contained in:
crschnick 2024-04-01 05:12:44 +00:00
parent a8f07c3e0f
commit beb467af5a
3 changed files with 38 additions and 9 deletions

View file

@ -1,7 +1,10 @@
package io.xpipe.app.terminal; package io.xpipe.app.terminal;
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.xpipe.app.util.ApplicationHelper;
import io.xpipe.app.util.LocalShell; import io.xpipe.app.util.LocalShell;
import io.xpipe.app.util.ShellTemp;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.core.process.CommandBuilder; import io.xpipe.core.process.CommandBuilder;
import io.xpipe.core.store.FilePath; import io.xpipe.core.store.FilePath;
import io.xpipe.core.util.XPipeInstallation; import io.xpipe.core.util.XPipeInstallation;
@ -12,7 +15,7 @@ public class KittyTerminalType {
@Override @Override
public String getId() { public String getId() {
return "app.tabby"; return "app.kitty";
} }
@Override @Override
@ -22,33 +25,42 @@ public class KittyTerminalType {
@Override @Override
public void launch(LaunchConfiguration configuration) throws Exception { public void launch(LaunchConfiguration configuration) throws Exception {
launchInstanceIfNeeded(); var toClose = prepare();
open(configuration); open(configuration);
if (toClose) {
closeInitial();
}
} }
}; };
private static FilePath getSocket() throws Exception { private static FilePath getSocket() throws Exception {
try (var sc = LocalShell.getShell().start()) { try (var sc = LocalShell.getShell().start()) {
var temp = sc.getSystemTemporaryDirectory(); var temp = ShellTemp.getUserSpecificTempDataDirectory(sc, null);
sc.executeSimpleCommand(sc.getShellDialect().getMkdirsCommand(temp.toString()));
return temp.join("xpipe_kitty"); return temp.join("xpipe_kitty");
} }
} }
private static void launchInstanceIfNeeded() throws Exception { private static boolean prepare() throws Exception {
var socket = getSocket(); var socket = getSocket();
try (var sc = LocalShell.getShell().start()) { try (var sc = LocalShell.getShell().start()) {
if (sc.getShellDialect().createFileExistsCommand(sc,socket.toString()).executeAndCheck()) { ApplicationHelper.checkIsInPath(sc, "kitty", "Kitty", null);
return; ApplicationHelper.checkIsInPath(sc, "socat", "socat", null);
if (sc.executeSimpleBooleanCommand("test -w " + sc.getShellDialect().fileArgument(socket))) {
return false;
} }
sc.executeSimpleCommand(CommandBuilder.of().add("kitty").add("-o", "allow_remote_control=socket-only", "--listen-on", "unix:" + getSocket(), "--detach")); sc.executeSimpleCommand(CommandBuilder.of().add("kitty").add("-o", "allow_remote_control=socket-only", "--listen-on", "unix:" + getSocket(), "--detach"));
ThreadHelper.sleep(1500);
return true;
} }
} }
private static void open(ExternalTerminalType.LaunchConfiguration configuration) throws Exception { private static void open(ExternalTerminalType.LaunchConfiguration configuration) throws Exception {
try (var sc = LocalShell.getShell().start()) { try (var sc = LocalShell.getShell().start()) {
var payload = JsonNodeFactory.instance.objectNode(); var payload = JsonNodeFactory.instance.objectNode();
payload.put("args", configuration.getDialectLaunchCommand().buildString(sc)); payload.putArray("args").add("bash");
payload.put("tab_title",configuration.getColoredTitle()); payload.put("tab_title",configuration.getColoredTitle());
payload.put("type", "tab"); payload.put("type", "tab");
payload.put("logo_alpha", 0.01); payload.put("logo_alpha", 0.01);
@ -57,6 +69,23 @@ public class KittyTerminalType {
var json = JsonNodeFactory.instance.objectNode(); var json = JsonNodeFactory.instance.objectNode();
json.put("cmd", "launch"); json.put("cmd", "launch");
json.set("payload", payload); json.set("payload", payload);
json.putArray("version").add(0).add(14).add(2);
var jsonString = json.toString();
var echoString = "'\\eP@kitty-cmd" + jsonString + "\\e\\\\'";
sc.executeSimpleCommand(CommandBuilder.of().add("echo", "-en", echoString, "|", "socat", "-").addFile(getSocket()));
}
}
private static void closeInitial() throws Exception {
try (var sc = LocalShell.getShell().start()) {
var payload = JsonNodeFactory.instance.objectNode();
payload.put("match", "not recent:0");
var json = JsonNodeFactory.instance.objectNode();
json.put("cmd", "close-tab");
json.set("payload", payload);
json.putArray("version").add(0).add(14).add(2);
var jsonString = json.toString(); var jsonString = json.toString();
var echoString = "'\\eP@kitty-cmd" + jsonString + "\\e\\\\'"; var echoString = "'\\eP@kitty-cmd" + jsonString + "\\e\\\\'";

View file

@ -52,7 +52,7 @@ public class ShellTemp {
var temp = proc.getSystemTemporaryDirectory(); var temp = proc.getSystemTemporaryDirectory();
base = temp.join("xpipe"); base = temp.join("xpipe");
} }
return base.join(sub); return sub != null ? base.join(sub) : base;
} }
public static void checkTempDirectory(ShellControl proc) throws Exception { public static void checkTempDirectory(ShellControl proc) throws Exception {

View file

@ -187,7 +187,7 @@ public class CommandBuilder {
} }
public CommandBuilder addFile(FilePath s) { public CommandBuilder addFile(FilePath s) {
return addFile(shellControl -> shellControl.getShellDialect().fileArgument(s)); return addFile(s.toString());
} }
public CommandBuilder addLiteral(String s) { public CommandBuilder addLiteral(String s) {