mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-24 09:18:08 +02:00
Functions to punch DCS escapes through tmux
This commit is contained in:
28
tools/tui/dcs_to_kitty.go
Normal file
28
tools/tui/dcs_to_kitty.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package tui
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func DCSToKitty(msgtype, payload string) (string, error) {
|
||||
data := base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(payload))
|
||||
ans := "\x1bP@kitty-" + msgtype + "|" + data
|
||||
tmux := TmuxSocketAddress()
|
||||
if tmux != "" {
|
||||
err := TmuxAllowPassthrough()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ans = "\033Ptmux;\033" + ans + "\033\033\\\033\\"
|
||||
} else {
|
||||
ans += "\033\\"
|
||||
}
|
||||
return ans, nil
|
||||
}
|
||||
56
tools/tui/tmux.go
Normal file
56
tools/tui/tmux.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kitty/tools/utils"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func tmux_socket_address() (socket string) {
|
||||
socket = os.Getenv("TMUX")
|
||||
if socket == "" {
|
||||
return ""
|
||||
}
|
||||
addr, pid_str, found := strings.Cut(socket, ",")
|
||||
if !found {
|
||||
return ""
|
||||
}
|
||||
if unix.Access(addr, unix.R_OK|unix.W_OK) != nil {
|
||||
return ""
|
||||
}
|
||||
pid, err := strconv.ParseInt(pid_str, 10, 32)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
p, err := process.NewProcess(int32(pid))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
cmd, err := p.CmdlineSlice()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(cmd) > 0 && strings.ToLower(filepath.Base(cmd[0])) != "tmux" {
|
||||
return ""
|
||||
}
|
||||
return socket
|
||||
}
|
||||
|
||||
var TmuxSocketAddress = (&utils.Once[string]{Run: tmux_socket_address}).Get
|
||||
|
||||
func tmux_allow_passthrough() error {
|
||||
return exec.Command("tmux", "set", "-p", "allow-passthrough", "on").Run()
|
||||
}
|
||||
|
||||
var TmuxAllowPassthrough = (&utils.Once[error]{Run: tmux_allow_passthrough}).Get
|
||||
Reference in New Issue
Block a user