Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/app/store/wshclientapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,12 @@ export class RpcApiType {
return client.wshRpcCall("setsecrets", data, opts);
}

// command "setterminalsize" [call]
SetTerminalSizeCommand(client: WshClient, data: CommandSetTerminalSizeData, opts?: RpcOpts): Promise<void> {
if (this.mockClient) return this.mockClient.mockWshRpcCall(client, "setterminalsize", data, opts);
return client.wshRpcCall("setterminalsize", data, opts);
}

// command "setvar" [call]
SetVarCommand(client: WshClient, data: CommandVarData, opts?: RpcOpts): Promise<void> {
if (this.mockClient) return this.mockClient.mockWshRpcCall(client, "setvar", data, opts);
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/view/term/termwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ export class TermWrap {
}
const termSize: TermSize = { rows: this.terminal.rows, cols: this.terminal.cols };
try {
await RpcApi.ControllerInputCommand(TabRpcClient, { blockid: this.blockId, termsize: termSize });
await RpcApi.SetTerminalSizeCommand(TabRpcClient, { blockid: this.blockId, termsize: termSize });
} catch (e) {
console.log("error syncing terminal size", this.blockId, e);
}
Expand Down
6 changes: 6 additions & 0 deletions frontend/types/gotypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ declare global {
delete?: boolean;
};

// wshrpc.CommandSetTerminalSizeData
type CommandSetTerminalSizeData = {
blockid: string;
termsize: TermSize;
};

// wshrpc.CommandStartBuilderData
type CommandStartBuilderData = {
builderid: string;
Expand Down
52 changes: 52 additions & 0 deletions pkg/blockcontroller/blockcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Controller interface {
GetRuntimeStatus() *BlockControllerRuntimeStatus // does not return nil
GetConnName() string
SendInput(input *BlockInputUnion) error
ApplyTermSize(termSize waveobj.TermSize) error
}

// Registry for all controllers
Expand Down Expand Up @@ -176,6 +177,10 @@ func ResyncController(ctx context.Context, tabId string, blockId string, rtOpts
return fmt.Errorf("error getting block: %w", err)
}

if rtOpts != nil && rtOpts.TermSize.Rows > 0 && rtOpts.TermSize.Cols > 0 {
SetTerminalSize(blockId, rtOpts.TermSize)
}

controllerName := blockData.Meta.GetString(waveobj.MetaKey_Controller, "")
connName := blockData.Meta.GetString(waveobj.MetaKey_Connection, "")

Expand Down Expand Up @@ -510,6 +515,53 @@ func SendInput(blockId string, inputUnion *BlockInputUnion) error {
return controller.SendInput(inputUnion)
}

func setTermSizeInDB(blockId string, termSize waveobj.TermSize) error {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
ctx = waveobj.ContextWithUpdates(ctx)
bdata, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
if err != nil {
return fmt.Errorf("error getting block data: %v", err)
}
if bdata.RuntimeOpts == nil {
bdata.RuntimeOpts = &waveobj.RuntimeOpts{}
}
bdata.RuntimeOpts.TermSize = termSize
err = wstore.DBUpdate(ctx, bdata)
if err != nil {
return fmt.Errorf("error updating block data: %v", err)
}
updates := waveobj.ContextGetUpdatesRtn(ctx)
wps.Broker.SendUpdateEvents(updates)
return nil
}

func SetTerminalSize(blockId string, termSize waveobj.TermSize) {
if termSize.Rows <= 0 || termSize.Cols <= 0 {
log.Printf("[blockcontroller] invalid term size for block %s: %v", blockId, termSize)
return
}
if err := setTermSizeInDB(blockId, termSize); err != nil {
log.Printf("[blockcontroller] error writing term size to DB for block %s: %v", blockId, err)
}
if c := getController(blockId); c != nil {
if err := c.ApplyTermSize(termSize); err != nil {
log.Printf("[blockcontroller] error applying term size for block %s: %v", blockId, err)
}
}
}

func getLatestTermSize(blockId string) waveobj.TermSize {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
bdata, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
if err != nil {
log.Printf("[blockcontroller] error reading block %s for term size: %v, using defaults", blockId, err)
return waveobj.TermSize{Rows: shellutil.DefaultTermRows, Cols: shellutil.DefaultTermCols}
}
return getTermSize(bdata)
}

// only call this on shutdown
func StopAllBlockControllersForShutdown() {
controllers := getAllControllers()
Expand Down
16 changes: 9 additions & 7 deletions pkg/blockcontroller/sessiondaemoncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,7 @@ func (sdc *SessionDaemonController) syncJobIdToBlocks(ctx context.Context, jobId
}

func (sdc *SessionDaemonController) startNewJob(ctx context.Context, blockMeta waveobj.MetaMapType, rtOpts *waveobj.RuntimeOpts) (string, error) {
termSize := waveobj.TermSize{
Rows: shellutil.DefaultTermRows,
Cols: shellutil.DefaultTermCols,
}
if rtOpts != nil && rtOpts.TermSize.Rows > 0 && rtOpts.TermSize.Cols > 0 {
termSize = rtOpts.TermSize
}
termSize := getLatestTermSize(sdc.BlockId)
cmdStr := blockMeta.GetString(waveobj.MetaKey_Cmd, "")
cwd := blockMeta.GetString(waveobj.MetaKey_CmdCwd, "")
opts, err := remote.ParseOpts(sdc.ConnName)
Expand Down Expand Up @@ -199,6 +193,14 @@ func (sdc *SessionDaemonController) SendInput(inputUnion *BlockInputUnion) error
return daemon.SendInput(context.Background(), inputUnion.InputData, inputUnion.SigName, inputUnion.TermSize)
}

func (sdc *SessionDaemonController) ApplyTermSize(termSize waveobj.TermSize) error {
daemon := sessiondaemon.Manager.Get(sdc.DaemonId)
if daemon == nil {
return nil
}
return daemon.SendInput(context.Background(), nil, "", &termSize)
}

func (sdc *SessionDaemonController) GetRuntimeStatus() *BlockControllerRuntimeStatus {
var rtn BlockControllerRuntimeStatus
sdc.WithLock(func() {
Expand Down
52 changes: 11 additions & 41 deletions pkg/blockcontroller/shellcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ func (sc *ShellController) SendInput(inputUnion *BlockInputUnion) error {
return nil
}

func (sc *ShellController) ApplyTermSize(termSize waveobj.TermSize) error {
sc.Lock.Lock()
shellProc := sc.ShellProc
sc.Lock.Unlock()
if shellProc == nil {
return nil
}
return shellProc.Cmd.SetSize(termSize.Rows, termSize.Cols)
}

func (sc *ShellController) WithLock(f func()) {
sc.Lock.Lock()
defer sc.Lock.Unlock()
Expand Down Expand Up @@ -303,12 +313,7 @@ func (sc *ShellController) run(logCtx context.Context, bdata *waveobj.Block, blo
panichandler.PanicHandler("blockcontroller:run-shell-command", recover())
}()
defer sc.UnlockRunLock()
var termSize waveobj.TermSize
if rtOpts != nil {
termSize = rtOpts.TermSize
} else {
termSize = getTermSize(bdata)
}
termSize := getLatestTermSize(sc.BlockId)
err := sc.DoRunShellCommand(logCtx, &RunShellOpts{TermSize: termSize}, bdata.Meta)
if err != nil {
debugLog(logCtx, "error running shell: %v\n", err)
Expand Down Expand Up @@ -569,9 +574,6 @@ func (bc *ShellController) manageRunningShellProcess(shellProc *shellexec.ShellP
if len(ic.InputData) > 0 {
shellProc.Cmd.Write(ic.InputData)
}
if ic.TermSize != nil {
updateTermSize(shellProc, bc.BlockId, *ic.TermSize)
}
}
}()
go func() {
Expand Down Expand Up @@ -876,35 +878,3 @@ func getCustomInitScriptValue(meta waveobj.MetaMapType, connName string, shellTy
}
return "", ""
}

func updateTermSize(shellProc *shellexec.ShellProc, blockId string, termSize waveobj.TermSize) {
err := setTermSizeInDB(blockId, termSize)
if err != nil {
log.Printf("error setting pty size: %v\n", err)
}
err = shellProc.Cmd.SetSize(termSize.Rows, termSize.Cols)
if err != nil {
log.Printf("error setting pty size: %v\n", err)
}
}

func setTermSizeInDB(blockId string, termSize waveobj.TermSize) error {
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
ctx = waveobj.ContextWithUpdates(ctx)
bdata, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
if err != nil {
return fmt.Errorf("error getting block data: %v", err)
}
if bdata.RuntimeOpts == nil {
bdata.RuntimeOpts = &waveobj.RuntimeOpts{}
}
bdata.RuntimeOpts.TermSize = termSize
err = wstore.DBUpdate(ctx, bdata)
if err != nil {
return fmt.Errorf("error updating block data: %v", err)
}
updates := waveobj.ContextGetUpdatesRtn(ctx)
wps.Broker.SendUpdateEvents(updates)
return nil
}
4 changes: 4 additions & 0 deletions pkg/blockcontroller/tsunamicontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ func (c *TsunamiController) SendInput(input *BlockInputUnion) error {
return fmt.Errorf("tsunami controller send input not implemented")
}

func (c *TsunamiController) ApplyTermSize(termSize waveobj.TermSize) error {
return nil
}

func runTsunamiAppBinary(ctx context.Context, appBinPath string, appPath string, blockMeta waveobj.MetaMapType) (*TsunamiAppProc, error) {
cmd := exec.Command(appBinPath)
cmd.Env = append(os.Environ(), "TSUNAMI_CLOSEONSTDIN=1")
Expand Down
6 changes: 6 additions & 0 deletions pkg/wshrpc/wshclient/wshclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,12 @@ func SetSecretsCommand(w *wshutil.WshRpc, data map[string]*string, opts *wshrpc.
return err
}

// command "setterminalsize", wshserver.SetTerminalSizeCommand
func SetTerminalSizeCommand(w *wshutil.WshRpc, data wshrpc.CommandSetTerminalSizeData, opts *wshrpc.RpcOpts) error {
_, err := sendRpcRequestCallHelper[any](w, "setterminalsize", data, opts)
return err
}

// command "setvar", wshserver.SetVarCommand
func SetVarCommand(w *wshutil.WshRpc, data wshrpc.CommandVarData, opts *wshrpc.RpcOpts) error {
_, err := sendRpcRequestCallHelper[any](w, "setvar", data, opts)
Expand Down
6 changes: 6 additions & 0 deletions pkg/wshrpc/wshrpctypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type WshRpcInterface interface {
ControllerInputCommand(ctx context.Context, data CommandBlockInputData) error
ControllerDestroyCommand(ctx context.Context, blockId string) error
ControllerResyncCommand(ctx context.Context, data CommandControllerResyncData) error
SetTerminalSizeCommand(ctx context.Context, data CommandSetTerminalSizeData) error
ControllerAppendOutputCommand(ctx context.Context, data CommandControllerAppendOutputData) error
ResolveIdsCommand(ctx context.Context, data CommandResolveIdsData) (CommandResolveIdsRtnData, error)
CreateBlockCommand(ctx context.Context, data CommandCreateBlockData) (waveobj.ORef, error)
Expand Down Expand Up @@ -315,6 +316,11 @@ type CommandControllerResyncData struct {
RtOpts *waveobj.RuntimeOpts `json:"rtopts,omitempty"`
}

type CommandSetTerminalSizeData struct {
BlockId string `json:"blockid"`
TermSize waveobj.TermSize `json:"termsize"`
}

type CommandControllerAppendOutputData struct {
BlockId string `json:"blockid"`
Data64 string `json:"data64"`
Expand Down
14 changes: 12 additions & 2 deletions pkg/wshrpc/wshserver/wshserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,20 @@ func (ws *WshServer) ControllerResyncCommand(ctx context.Context, data wshrpc.Co
return blockcontroller.ResyncController(ctx, data.TabId, data.BlockId, data.RtOpts, data.ForceRestart)
}

func (ws *WshServer) SetTerminalSizeCommand(ctx context.Context, data wshrpc.CommandSetTerminalSizeData) error {
blockcontroller.SetTerminalSize(data.BlockId, data.TermSize)
return nil
}

func (ws *WshServer) ControllerInputCommand(ctx context.Context, data wshrpc.CommandBlockInputData) error {
if data.TermSize != nil {
blockcontroller.SetTerminalSize(data.BlockId, *data.TermSize)
}
if len(data.InputData64) == 0 && data.SigName == "" {
return nil
}
inputUnion := &blockcontroller.BlockInputUnion{
SigName: data.SigName,
TermSize: data.TermSize,
SigName: data.SigName,
}
if len(data.InputData64) > 0 {
inputBuf := make([]byte, base64.StdEncoding.DecodedLen(len(data.InputData64)))
Expand Down
Loading