Dont change the tmux allow-passthrough mode if it is already set

This commit is contained in:
Kovid Goyal 2023-03-03 15:06:49 +05:30
parent 1bf911a81b
commit e6662e11c3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -3,6 +3,7 @@
package tui package tui
import ( import (
"errors"
"fmt" "fmt"
"kitty/tools/utils" "kitty/tools/utils"
"os" "os"
@ -10,6 +11,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/shirou/gopsutil/v3/process" "github.com/shirou/gopsutil/v3/process"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -50,7 +52,34 @@ func tmux_socket_address() (socket string) {
var TmuxSocketAddress = (&utils.Once[string]{Run: tmux_socket_address}).Get var TmuxSocketAddress = (&utils.Once[string]{Run: tmux_socket_address}).Get
func tmux_allow_passthrough() error { func tmux_allow_passthrough() error {
return exec.Command("tmux", "set", "-p", "allow-passthrough", "on").Run() c := exec.Command("tmux", "show", "-Ap", "allow-passthrough")
allowed, not_allowed := errors.New("allowed"), errors.New("not allowed")
get_result := make(chan error)
go func() {
output, err := c.Output()
if err != nil {
get_result <- err
} else {
q := strings.TrimSpace(utils.UnsafeBytesToString(output))
if strings.HasSuffix(q, " on") || strings.HasSuffix(q, " all") {
get_result <- allowed
} else {
get_result <- not_allowed
}
}
}()
select {
case r := <-get_result:
if r == allowed {
return nil
}
if r != not_allowed {
return r
}
return exec.Command("tmux", "set", "-p", "allow-passthrough", "on").Run()
case <-time.After(2 * time.Second):
return fmt.Errorf("Tmux command timed out. This often happens when the version of tmux on your PATH is older than the version of the running tmux server")
}
} }
var TmuxAllowPassthrough = (&utils.Once[error]{Run: tmux_allow_passthrough}).Get var TmuxAllowPassthrough = (&utils.Once[error]{Run: tmux_allow_passthrough}).Get