From d0b03c359a15f5d6a499ea2545e5c5640fb1066c Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 20 Feb 2023 15:41:39 -0800 Subject: [PATCH] setscreenidx and getdbversion --- pkg/cmdrunner/cmdrunner.go | 9 ++++++ pkg/sstore/dbops.go | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index c75a34c9..8ead6891 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -524,6 +524,10 @@ func ScreenSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } varsUpdated = append(varsUpdated, "tabcolor") } + if pk.Kwargs["pos"] != "" { + + varsUpdated = append(varsUpdated, "pos") + } if len(varsUpdated) == 0 { return nil, fmt.Errorf("/screen:set no updates, can set %s", formatStrs([]string{"name", "pos", "tabcolor"}, "or", false)) } @@ -2188,11 +2192,16 @@ func ClientShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (s if err != nil { return nil, fmt.Errorf("cannot retrieve client data: %v\n", err) } + dbVersion, err := sstore.GetDBVersion(ctx) + if err != nil { + return nil, fmt.Errorf("cannot retrieve db version: %v\n", err) + } var buf bytes.Buffer buf.WriteString(fmt.Sprintf(" %-15s %s\n", "userid", clientData.UserId)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "clientid", clientData.ClientId)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "backend", scbase.PromptVersion)) buf.WriteString(fmt.Sprintf(" %-15s %s\n", "telemetry", boolToStr(clientData.ClientOpts.NoTelemetry, "off", "on"))) + buf.WriteString(fmt.Sprintf(" %-15s %d\n", "db-version", dbVersion)) update := sstore.ModelUpdate{ Info: &sstore.InfoMsgType{ InfoTitle: fmt.Sprintf("client info"), diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 77d9d779..13d137d3 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1969,3 +1969,67 @@ func MarkActivityAsUploaded(ctx context.Context, activityArr []*ActivityType) er }) return txErr } + +func foundInStrArr(strs []string, s string) bool { + for _, sval := range strs { + if s == sval { + return true + } + } + return false +} + +// newPos is 0-indexed +func reorderStrs(strs []string, toMove string, newPos int) []string { + if !foundInStrArr(strs, toMove) { + return strs + } + var added bool + rtn := make([]string, 0, len(strs)) + for _, s := range strs { + if s == toMove { + continue + } + if len(rtn) == newPos { + added = true + rtn = append(rtn, toMove) + } + rtn = append(rtn, s) + } + if !added { + rtn = append(rtn, toMove) + } + return rtn +} + +// newScreenIdx is 1-indexed +func SetScreenIdx(ctx context.Context, sessionId string, screenId string, newScreenIdx int) error { + if newScreenIdx <= 0 { + return fmt.Errorf("invalid screenidx/pos, must be greater than 0") + } + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT screenid FROM screen WHERE sessionid = ? AND screenid = ? AND NOT archived` + if !tx.Exists(query, sessionId, screenId) { + return fmt.Errorf("invalid screen, not found (or archived)") + } + query = `SELECT screenid FROM screen WHERE sessionid = ? AND NOT archived ORDER BY screenidx` + screens := tx.SelectStrings(query, sessionId) + newScreens := reorderStrs(screens, screenId, newScreenIdx-1) + query = `UPDATE screen SET screenidx = ? WHERE sessionid = ? AND screenid = ?` + for idx, sid := range newScreens { + tx.Exec(query, idx+1, sessionId, sid) + } + return nil + }) + return txErr +} + +func GetDBVersion(ctx context.Context) (int, error) { + var version int + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT version FROM schema_migrations` + version = tx.GetInt(query) + return nil + }) + return version, txErr +}