From 2c628909124575c1c19a834ac680729dc4ab9ec5 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 20 Jun 2022 17:51:28 -0700 Subject: [PATCH] call SetWinsize to set terminal size always for pty --- pkg/packet/packet.go | 4 +++- pkg/shexec/shexec.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/packet/packet.go b/pkg/packet/packet.go index ea791e48..2e145f3b 100644 --- a/pkg/packet/packet.go +++ b/pkg/packet/packet.go @@ -297,9 +297,11 @@ type RunPacketType struct { ChDir string `json:"chdir,omitempty"` Env map[string]string `json:"env,omitempty"` Command string `json:"command"` + Rows int `json:"rows"` + Cols int `json:'cols"` } -func (ct *RunPacketType) GetType() string { +func (*RunPacketType) GetType() string { return RunPacketStr } diff --git a/pkg/shexec/shexec.go b/pkg/shexec/shexec.go index e872e34b..cd155702 100644 --- a/pkg/shexec/shexec.go +++ b/pkg/shexec/shexec.go @@ -21,6 +21,11 @@ import ( "github.com/scripthaus-dev/sh2-runner/pkg/packet" ) +const DefaultRows = 25 +const DefaultCols = 80 +const MaxRows = 1024 +const MaxCols = 1024 + type ShExecType struct { FileNames *base.CommandFileNames Cmd *exec.Cmd @@ -148,6 +153,18 @@ func ValidateRunPacket(pk *packet.RunPacketType) error { return nil } +func GetWinsize(p *packet.RunPacketType) *pty.Winsize { + rows := DefaultRows + cols := DefaultCols + if p.Rows > 0 && p.Rows <= MaxRows { + rows = p.Rows + } + if p.Cols > 0 && p.Cols <= MaxCols { + cols = p.Cols + } + return &pty.Winsize{Rows: uint16(rows), Cols: uint16(cols)} +} + // when err is nil, the command will have already been started func RunCommand(pk *packet.RunPacketType, sender *packet.PacketSender) (*ShExecType, error) { if pk.CmdId == "" { @@ -172,6 +189,7 @@ func RunCommand(pk *packet.RunPacketType, sender *packet.PacketSender) (*ShExecT if err != nil { return nil, fmt.Errorf("opening new pty: %w", err) } + pty.Setsize(cmdPty, GetWinsize(pk)) defer func() { cmdTty.Close() }()