jesseduffield.lazygit/pkg/commands/oscommands/pty.go
Stefan Haller 1935117141 Add pty support on Windows via ConPTY
Replace the StartPty stub with a real ConPTY implementation:
CreatePipe + CreatePseudoConsole + PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
+ CreateProcess. Pagers and external diff tools now get real terminal
behavior instead of being handed pipes.

One Windows-specific quirk worth flagging: ConPTY does not EOF the
output pipe when the child exits; conhost keeps it alive until
ClosePseudoConsole is called explicitly. A background waiter goroutine
calls ClosePseudoConsole as soon as proc.Wait returns, so callers see
EOF on outRead — restoring the Unix master-fd-EOFs-when-slave-closes
semantics they depend on.

The ErrPtyUnsupported sentinel and the no-pty fallback in newPtyTask
are gone now that both platforms have a real implementation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-07-03 18:47:15 +02:00

35 lines
1.1 KiB
Go

package oscommands
import (
"io"
"os"
)
// Pty is the master side of a pseudo-terminal running a subprocess. The
// concrete implementation is platform-specific: creack/pty on Unix and
// ConPTY on Windows.
type Pty interface {
io.ReadWriteCloser
Resize(cols, rows uint16) error
}
// StartedPty is the result of StartPty.
type StartedPty struct {
// Pty is the master side of the pseudo-terminal; read from it to get
// the child's combined stdout/stderr and write to it to feed stdin.
Pty Pty
// Process is the spawned child. Useful for signalling; on Windows the
// original *exec.Cmd was not Start()ed (ConPTY spawns via
// CreateProcess, not os/exec) so cmd.Process is nil and this is the
// only handle.
Process *os.Process
// Wait blocks until the child exits and returns a non-nil error on a
// nonzero exit status, matching *exec.Cmd.Wait semantics.
Wait func() error
}
// StartPty runs cmd in a pseudo-terminal with the given initial dimensions.
// Implemented per-platform in pty_unix.go / pty_windows.go.
//
// func StartPty(cmd *exec.Cmd, cols, rows uint16) (StartedPty, error)