Skip to content

Commit 3aadf34

Browse files
committed
增强跨平台 pty 支持,优化窗口大小调整逻辑 #11
1 parent c2761f0 commit 3aadf34

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

internal/crosspty/pty.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import (
1313
)
1414

1515
func setPytSize(p pty.Pty) error {
16-
w, h, err := term.GetSize(int(os.Stdin.Fd()))
17-
18-
if err != nil {
19-
return err
16+
if w, h, err := getConsoleSize(p); err == nil {
17+
return p.Resize(w, h)
2018
}
2119

22-
return p.Resize(w, h)
20+
return nil
2321
}
2422

2523
func Start(shellPath string, env map[string]string) error {
@@ -35,11 +33,13 @@ func Start(shellPath string, env map[string]string) error {
3533
return err
3634
}
3735

38-
_ = setPytSize(ptmx) // Set the initial size of the pty.
36+
if err := setPytSize(ptmx); err != nil {
37+
return errors.WithMessage(err, "failed to set initial pty size")
38+
}
3939

4040
ch := make(chan os.Signal, 1)
4141

42-
listenOnResize(ch, ptmx, setPytSize)
42+
go listenOnResize(ch, ptmx, setPytSize)
4343

4444
defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done.
4545

internal/crosspty/size_unix.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ import (
88
"syscall"
99

1010
"github.com/aymanbagabas/go-pty"
11+
"golang.org/x/term"
1112
)
1213

14+
func getConsoleSize(p pty.Pty) (int, int, error) {
15+
w, h, err := term.GetSize(int(os.Stdin.Fd()))
16+
17+
if err != nil {
18+
return 0, 0, err
19+
}
20+
21+
return w, h, nil
22+
}
23+
1324
func listenOnResize(ch chan os.Signal, p pty.Pty, onResize func(p pty.Pty) error) {
1425
signal.Notify(ch, syscall.SIGWINCH)
1526

16-
go func() {
17-
for range ch {
18-
_ = onResize(p)
19-
}
20-
}()
27+
for range ch {
28+
_ = onResize(p)
29+
}
2130
}

internal/crosspty/size_windows.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,47 @@ package crosspty
44

55
import (
66
"os"
7+
"time"
78

89
"github.com/aymanbagabas/go-pty"
10+
"golang.org/x/sys/windows"
911
)
1012

1113
func listenOnResize(ch chan os.Signal, p pty.Pty, onResize func(p pty.Pty) error) {
1214
// Windows does not support resizing pty, so we do nothing here.
1315
// This is a no-op function to satisfy the interface.
16+
var prevCols, prevRows int
17+
18+
for {
19+
time.Sleep(500 * time.Millisecond)
20+
21+
cols, rows, err := getConsoleSize(p)
22+
23+
if err != nil {
24+
continue
25+
}
26+
27+
if cols != prevCols || rows != prevRows {
28+
err := onResize(p)
29+
30+
if err == nil {
31+
prevCols = cols
32+
prevRows = rows
33+
}
34+
}
35+
}
36+
}
37+
38+
func getConsoleSize(p pty.Pty) (int, int, error) {
39+
handle := windows.Handle(os.Stdout.Fd())
40+
var info windows.ConsoleScreenBufferInfo
41+
42+
if err := windows.GetConsoleScreenBufferInfo(handle, &info); err != nil {
43+
return 0, 0, err
44+
}
45+
46+
cols := int(info.Window.Right - info.Window.Left + 1)
47+
rows := int(info.Window.Bottom - info.Window.Top + 1)
48+
49+
return cols, rows, nil
1450
}

0 commit comments

Comments
 (0)