File tree Expand file tree Collapse file tree 3 files changed +57
-12
lines changed Expand file tree Collapse file tree 3 files changed +57
-12
lines changed Original file line number Diff line number Diff line change @@ -13,13 +13,11 @@ import (
13
13
)
14
14
15
15
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 )
20
18
}
21
19
22
- return p . Resize ( w , h )
20
+ return nil
23
21
}
24
22
25
23
func Start (shellPath string , env map [string ]string ) error {
@@ -35,11 +33,13 @@ func Start(shellPath string, env map[string]string) error {
35
33
return err
36
34
}
37
35
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
+ }
39
39
40
40
ch := make (chan os.Signal , 1 )
41
41
42
- listenOnResize (ch , ptmx , setPytSize )
42
+ go listenOnResize (ch , ptmx , setPytSize )
43
43
44
44
defer func () { signal .Stop (ch ); close (ch ) }() // Cleanup signals when done.
45
45
Original file line number Diff line number Diff line change @@ -8,14 +8,23 @@ import (
8
8
"syscall"
9
9
10
10
"github.com/aymanbagabas/go-pty"
11
+ "golang.org/x/term"
11
12
)
12
13
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
+
13
24
func listenOnResize (ch chan os.Signal , p pty.Pty , onResize func (p pty.Pty ) error ) {
14
25
signal .Notify (ch , syscall .SIGWINCH )
15
26
16
- go func () {
17
- for range ch {
18
- _ = onResize (p )
19
- }
20
- }()
27
+ for range ch {
28
+ _ = onResize (p )
29
+ }
21
30
}
Original file line number Diff line number Diff line change @@ -4,11 +4,47 @@ package crosspty
4
4
5
5
import (
6
6
"os"
7
+ "time"
7
8
8
9
"github.com/aymanbagabas/go-pty"
10
+ "golang.org/x/sys/windows"
9
11
)
10
12
11
13
func listenOnResize (ch chan os.Signal , p pty.Pty , onResize func (p pty.Pty ) error ) {
12
14
// Windows does not support resizing pty, so we do nothing here.
13
15
// 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
14
50
}
You can’t perform that action at this time.
0 commit comments