From aff174fa80a18c1598030f955a9d46daed91b345 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 26 Aug 2022 21:44:18 -0700 Subject: [PATCH] tab colors --- pkg/cmdrunner/cmdrunner.go | 34 +++++++++++++++++++++++++++++++++- pkg/sstore/dbops.go | 16 ++++++++++++++++ pkg/sstore/sstore.go | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index b555b2e4..718c20e5 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -25,6 +25,8 @@ import ( const DefaultUserId = "sawka" const MaxNameLen = 50 +var ColorNames = []string{"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} + var genericNameRe = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_ .()<>,/\"'\\[\\]{}=+$@!*-]*$") var positionRe = regexp.MustCompile("^((\\+|-)?[0-9]+|(\\+|-))$") var wsRe = regexp.MustCompile("\\s+") @@ -251,8 +253,29 @@ func ScreenSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss } varsUpdated = append(varsUpdated, "name") } + if pk.Kwargs["tabcolor"] != "" { + color := pk.Kwargs["tabcolor"] + err = validateColor(color, "screen tabcolor") + if err != nil { + return nil, err + } + screenObj, err := sstore.GetScreenById(ctx, ids.SessionId, ids.ScreenId) + if err != nil { + return nil, err + } + opts := screenObj.ScreenOpts + if opts == nil { + opts = &sstore.ScreenOptsType{} + } + opts.TabColor = color + err = sstore.SetScreenOpts(ctx, ids.SessionId, ids.ScreenId, opts) + if err != nil { + return nil, fmt.Errorf("setting screen opts: %v", err) + } + varsUpdated = append(varsUpdated, "tabcolor") + } if len(varsUpdated) == 0 { - return nil, fmt.Errorf("/screen:set no updates, can set %s", formatStrs([]string{"name", "pos"}, "or", false)) + return nil, fmt.Errorf("/screen:set no updates, can set %s", formatStrs([]string{"name", "pos", "tabcolor"}, "or", false)) } screenObj, err := sstore.GetScreenById(ctx, ids.SessionId, ids.ScreenId) if err != nil { @@ -779,6 +802,15 @@ func validateName(name string, typeStr string) error { return nil } +func validateColor(color string, typeStr string) error { + for _, c := range ColorNames { + if color == c { + return nil + } + } + return fmt.Errorf("invalid %s, valid colors are: %s", typeStr, formatStrs(ColorNames, "or", false)) +} + func SessionOpenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { activate := resolveBool(pk.Kwargs["activate"], true) newName := pk.Kwargs["name"] diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 4ea5bfa4..f5f55c1f 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -756,3 +756,19 @@ func SetScreenName(ctx context.Context, sessionId string, screenId string, name }) return txErr } + +func SetScreenOpts(ctx context.Context, sessionId string, screenId string, opts *ScreenOptsType) error { + if opts == nil { + return fmt.Errorf("invalid screen opts cannot be nil") + } + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT screenid FROM screen WHERE sessionid = ? AND screenid = ?` + if !tx.Exists(query, sessionId, screenId) { + return fmt.Errorf("screen does not exist") + } + query = `UPDATE screen SET screenopts = ? WHERE sessionid = ? AND screenid = ?` + tx.ExecWrap(query, opts, sessionId, screenId) + return nil + }) + return txErr +} diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 0eea99bd..075cf85f 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -207,7 +207,7 @@ func WindowFromMap(m map[string]interface{}) *WindowType { } type ScreenOptsType struct { - TabColor string `json:"tabcolor"` + TabColor string `json:"tabcolor,omitempty"` } func (opts *ScreenOptsType) Scan(val interface{}) error {