From 9d6cc1f67a3ec16bcc9f081cb1a7b75c43a44189 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 26 Aug 2022 22:01:29 -0700 Subject: [PATCH] clear window --- pkg/cmdrunner/cmdrunner.go | 17 +++++++++++++++++ pkg/cmdrunner/shparse.go | 1 + pkg/sstore/dbops.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 718c20e5..352bd59a 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -48,6 +48,7 @@ func init() { registerCmdFn("compgen", CompGenCommand) registerCmdFn("setenv", SetEnvCommand) registerCmdFn("unset", UnSetCommand) + registerCmdFn("clear", ClearCommand) registerCmdFn("session", SessionCommand) registerCmdFn("session:open", SessionOpenCommand) @@ -886,6 +887,22 @@ func SessionCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto return update, nil } +func ClearCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { + ids, err := resolveIds(ctx, pk, R_Session|R_Screen|R_Window) + if err != nil { + return nil, err + } + update, err := sstore.ClearWindow(ctx, ids.SessionId, ids.WindowId) + if err != nil { + return nil, fmt.Errorf("clearing window: %v", err) + } + update.Info = &sstore.InfoMsgType{ + InfoMsg: fmt.Sprintf("window cleared"), + TimeoutMs: 2000, + } + return update, nil +} + func splitLinesForInfo(str string) []string { rtn := strings.Split(str, "\n") if rtn[len(rtn)-1] == "" { diff --git a/pkg/cmdrunner/shparse.go b/pkg/cmdrunner/shparse.go index 6d463be1..bd28184d 100644 --- a/pkg/cmdrunner/shparse.go +++ b/pkg/cmdrunner/shparse.go @@ -96,6 +96,7 @@ var BareMetaCmds = []BareMetaCmdDecl{ BareMetaCmdDecl{"setenv", "setenv"}, BareMetaCmdDecl{"export", "setenv"}, BareMetaCmdDecl{"unset", "unset"}, + BareMetaCmdDecl{"clear", "clear"}, } func SubMetaCmd(cmd string) string { diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index f5f55c1f..3a073b0a 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -772,3 +772,35 @@ func SetScreenOpts(ctx context.Context, sessionId string, screenId string, opts }) return txErr } + +func ClearWindow(ctx context.Context, sessionId string, windowId string) (*ModelUpdate, error) { + var lineIds []string + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?` + if !tx.Exists(query, sessionId, windowId) { + return fmt.Errorf("window does not exist") + } + query = `SELECT lineid FROM line WHERE sessionid = ? AND windowid = ?` + lineIds = tx.SelectStrings(query, sessionId, windowId) + query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` + tx.ExecWrap(query, sessionId, windowId) + return nil + }) + if txErr != nil { + return nil, txErr + } + win, err := GetWindowById(ctx, sessionId, windowId) + if err != nil { + return nil, err + } + for _, lineId := range lineIds { + line := &LineType{ + SessionId: sessionId, + WindowId: windowId, + LineId: lineId, + Remove: true, + } + win.Lines = append(win.Lines, line) + } + return &ModelUpdate{Window: win}, nil +}