Skip to content

Commit 54b84d0

Browse files
committed
Set PTY size from host terminal before starting the agent
- Pass stdinFd to startPTY and apply current terminal dimensions to the PTY before cmd.Start, so the agent initializes with the correct cols/rows instead of defaulting to 80x24
1 parent 6bdb768 commit 54b84d0

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ func displayHeader(daemon *Daemon, session, command, workDir, agentVersion strin
292292

293293
// startPTY starts the PTY and returns the pty master and command.
294294
// socketPath is set as AIPILOT_HOOK_SOCKET env var for the agent process.
295-
func startPTY(command, workDir, socketPath string) (pty.Pty, *pty.Cmd) {
295+
// stdinFd is used to get the current terminal size and apply it to the PTY
296+
// before starting the command, so the agent initializes with the correct dimensions.
297+
func startPTY(command, workDir, socketPath string, stdinFd int) (pty.Pty, *pty.Cmd) {
296298
fmt.Printf("Starting %s...\n", command)
297299

298300
// Resolve full path before setting cmd.Dir, otherwise on Windows
@@ -306,6 +308,15 @@ func startPTY(command, workDir, socketPath string) (pty.Pty, *pty.Cmd) {
306308
if err != nil {
307309
log.Fatal("Failed to create PTY:", err)
308310
}
311+
312+
// Set PTY size BEFORE starting the command so the agent
313+
// initializes its UI with the correct terminal dimensions.
314+
if term.IsTerminal(stdinFd) {
315+
if width, height, err := term.GetSize(stdinFd); err == nil && width > 0 && height > 0 {
316+
ptmx.Resize(width, height)
317+
}
318+
}
319+
309320
cmd := ptmx.Command(commandPath)
310321
cmd.Dir = workDir
311322
cmd.Env = append(os.Environ(),
@@ -583,7 +594,7 @@ func main() {
583594
socketPath := filepath.Join(os.TempDir(), "aipilot-"+session+".sock")
584595
daemon.startHookSocket(socketPath)
585596

586-
ptmx, cmd := startPTY(selectedCommand, workDir, socketPath)
597+
ptmx, cmd := startPTY(selectedCommand, workDir, socketPath, daemon.stdinFd)
587598
defer ptmx.Close()
588599

589600
daemon.mu.Lock()

0 commit comments

Comments
 (0)