diff --git a/term.go b/term.go index cdcc963..ce34a02 100644 --- a/term.go +++ b/term.go @@ -1,6 +1,7 @@ package terminal import ( + "github.com/creack/pty" "image/color" "io" "math" @@ -86,8 +87,11 @@ type Terminal struct { printer Printer cmd *exec.Cmd readWriterConfigurator ReadWriterConfigurator + onResize ResizeHandler } +type ResizeHandler = func(size *pty.Winsize) + // Printer is used for spooling print data when its received. type Printer interface { Print([]byte) @@ -549,3 +553,7 @@ type ReadWriterConfiguratorFunc func(r io.Reader, w io.WriteCloser) (io.Reader, func (m ReadWriterConfiguratorFunc) SetupReadWriter(r io.Reader, w io.WriteCloser) (io.Reader, io.WriteCloser) { return m(r, w) } + +func (t *Terminal) SetResizeHandler(h ResizeHandler) { + t.onResize = h +} diff --git a/term_unix.go b/term_unix.go index 93f6255..1ac5ee3 100644 --- a/term_unix.go +++ b/term_unix.go @@ -13,7 +13,7 @@ import ( ) func (t *Terminal) updatePTYSize() { - if t.pty == nil { // SSH or other direct connection? + if t.pty == nil && t.onResize == nil { // SSH or other direct connection? return } scale := float32(1.0) @@ -21,9 +21,15 @@ func (t *Terminal) updatePTYSize() { if c != nil { scale = c.Scale() } - _ = pty.Setsize(t.pty.(*os.File), &pty.Winsize{ + ws := &pty.Winsize{ Rows: uint16(t.config.Rows), Cols: uint16(t.config.Columns), - X: uint16(t.Size().Width * scale), Y: uint16(t.Size().Height * scale)}) + X: uint16(t.Size().Width * scale), Y: uint16(t.Size().Height * scale)} + if t.onResize != nil { + t.onResize(ws) + } + if t.pty != nil { + _ = pty.Setsize(t.pty.(*os.File), ws) + } } func (t *Terminal) startPTY() (io.WriteCloser, io.Reader, io.Closer, error) {