diff --git a/client/main.go b/client/main.go index 9a14abf..8fb7c04 100644 --- a/client/main.go +++ b/client/main.go @@ -848,6 +848,7 @@ func detectTerminalAndCalibrate() { setDefaultCharSize() if cfg.Renderer == "auto" { cfg.Renderer = detectRenderer() + Debug(fmt.Sprintf("Graphics auto-detection selected %s", cfg.Renderer), INFO) } if cfg.Renderer == "tcell" { return @@ -867,6 +868,11 @@ func detectRenderer() string { if err == nil && strings.Contains(response, "i=31;OK") { return "kitty" } + if err != nil { + Debug(fmt.Sprintf("Kitty graphics probe failed: %v", err), DEBUG) + } else { + Debug("Kitty graphics probe did not report support", DEBUG) + } response, err = queryTerminalWithTimeout("\033[c", graphicsProbeTimeoutMs) if err == nil { for _, parameter := range strings.Split(strings.TrimSuffix(strings.TrimPrefix(response, "\033[?"), "c"), ";") { @@ -874,12 +880,17 @@ func detectRenderer() string { return "sixel" } } + } else { + Debug(fmt.Sprintf("Sixel graphics probe failed: %v", err), DEBUG) } return "tcell" } func queryTerminalWithTimeout(query string, timeoutMs int) (string, error) { fd := int(os.Stdin.Fd()) + if fd < 0 || fd >= unix.FD_SETSIZE { + return "", fmt.Errorf("terminal descriptor %d is outside select range", fd) + } old, err := term.MakeRaw(fd) if err != nil { return "", err @@ -891,25 +902,36 @@ func queryTerminalWithTimeout(query string, timeoutMs int) (string, error) { deadline := time.Now().Add(time.Duration(timeoutMs) * time.Millisecond) response := make([]byte, 0, 128) for time.Now().Before(deadline) { - poll := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} - n, err := unix.Poll(poll, max(1, int(time.Until(deadline).Milliseconds()))) + remaining := time.Until(deadline) + if remaining <= 0 { + break + } + // The installer reopens stdin through /dev/tty. Darwin's poll returns + // POLLNVAL for that device even though reads work; select supports it. + var readable unix.FdSet + readable.Set(fd) + timeout := unix.NsecToTimeval(remaining.Nanoseconds()) + n, err := unix.Select(fd+1, &readable, nil, nil, &timeout) if err == unix.EINTR { continue } if err != nil { - return "", err + return "", fmt.Errorf("waiting for terminal response: %w", err) } if n == 0 { break } - if poll[0].Revents&unix.POLLIN == 0 { - return "", fmt.Errorf("terminal closed") - } buf := make([]byte, 128) n, err = unix.Read(fd, buf) + if err == unix.EINTR { + continue + } if err != nil { return "", err } + if n == 0 { + return "", io.EOF + } response = append(response, buf[:n]...) text := string(response) if strings.HasSuffix(text, "t") || strings.HasSuffix(text, "c") || strings.HasSuffix(text, "\033\\") { diff --git a/client/terminal_probe_test.go b/client/terminal_probe_test.go new file mode 100644 index 0000000..00215ac --- /dev/null +++ b/client/terminal_probe_test.go @@ -0,0 +1,108 @@ +package main + +import ( + "bytes" + "context" + "fmt" + "io" + "os" + "os/exec" + "strings" + "testing" + "time" + + "github.com/creack/pty" + "golang.org/x/sys/unix" +) + +// A direct PTY slave and an explicitly reopened /dev/tty are different devices +// on macOS. Exercise the installer-style pipe and redirection, not just a PTY. +func TestGraphicsProbePTY(t *testing.T) { + executable, err := os.Executable() + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name, launch, renderer string + delay time.Duration + }{ + {"direct-kitty", "direct", "kitty", 500 * time.Millisecond}, + {"redirect-kitty", "redirect", "kitty", 0}, + {"installer-kitty", "pipe", "kitty", 500 * time.Millisecond}, + {"installer-sixel", "pipe", "sixel", 500 * time.Millisecond}, + {"installer-no-response", "pipe", "tcell", 0}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, executable, "-test.run=^TestGraphicsProbePTYHelper$") + switch tc.launch { + case "redirect": + cmd = exec.CommandContext(ctx, "bash", "-c", `exec "$TERMIUM_PROBE_EXECUTABLE" -test.run=^TestGraphicsProbePTYHelper$