setscreenidx and getdbversion

This commit is contained in:
sawka
2023-02-20 15:41:39 -08:00
parent 5fa7bcc108
commit d0b03c359a
2 changed files with 73 additions and 0 deletions
+9
View File
@@ -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"),
+64
View File
@@ -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
}