Use FindExe to find the tmux executable and return a nicer error message when running tmux fails

This commit is contained in:
Kovid Goyal 2023-04-28 20:11:15 +05:30
parent 09ebdcd809
commit 514888a274
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -5,7 +5,6 @@ package tui
import (
"errors"
"fmt"
"kitty/tools/utils"
"os"
"os/exec"
"path/filepath"
@ -15,10 +14,16 @@ import (
"github.com/shirou/gopsutil/v3/process"
"golang.org/x/sys/unix"
"kitty/tools/utils"
)
var _ = fmt.Print
var TmuxExe = (&utils.Once[string]{Run: func() string {
return utils.FindExe("tmux")
}}).Get
func tmux_socket_address() (socket string) {
socket = os.Getenv("TMUX")
if socket == "" {
@ -53,13 +58,14 @@ func tmux_socket_address() (socket string) {
var TmuxSocketAddress = (&utils.Once[string]{Run: tmux_socket_address}).Get
func tmux_allow_passthrough() error {
c := exec.Command("tmux", "show", "-Ap", "allow-passthrough")
cmd := []string{TmuxExe(), "show", "-Ap", "allow-passthrough"}
c := exec.Command(cmd[0], cmd[1:]...)
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
get_result <- fmt.Errorf("Running %s failed with error: %w", strings.Join(cmd, " "), err)
} else {
q := strings.TrimSpace(utils.UnsafeBytesToString(output))
if strings.HasSuffix(q, " on") || strings.HasSuffix(q, " all") {
@ -77,7 +83,12 @@ func tmux_allow_passthrough() error {
if r != not_allowed {
return r
}
return exec.Command("tmux", "set", "-p", "allow-passthrough", "on").Run()
cmd := []string{TmuxExe(), "set", "-p", "allow-passthrough", "on"}
err := exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
err = fmt.Errorf("Running %s failed with error: %w", strings.Join(cmd, " "), err)
}
return err
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")
}