-
-
Notifications
You must be signed in to change notification settings - Fork 933
Expand file tree
/
Copy pathwshcmd-setbg.go
More file actions
230 lines (203 loc) · 6.85 KB
/
wshcmd-setbg.go
File metadata and controls
230 lines (203 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/wavetermdev/waveterm/pkg/util/fileutil"
"github.com/wavetermdev/waveterm/pkg/wavebase"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
)
var setBgCmd = &cobra.Command{
Use: "setbg [--opacity value] [--tile|--center] [--scale value] [--border-color color] [--active-border-color color] (image-path|\"#color\"|color-name)",
Short: "set background image or color for a tab",
Long: `Set a background image or color for a tab. Colors can be specified as:
- A quoted hex value like "#ff0000" (quotes required to prevent # being interpreted as a shell comment)
- A CSS color name like "blue" or "forestgreen"
Or provide a path to a supported image file (jpg, png, gif, webp, or svg).
You can also:
- Use --clear to remove the background
- Use --opacity without other arguments to change just the opacity
- Use --center for centered images without scaling (good for logos)
- Use --scale with --center to control image size
- Use --border-color to set the block frame border color
- Use --active-border-color to set the block frame focused border color
- Use --print to see the metadata without applying it`,
RunE: setBgRun,
PreRunE: preRunSetupRpcClient,
}
var (
setBgOpacity float64
setBgTile bool
setBgCenter bool
setBgSize string
setBgClear bool
setBgPrint bool
setBgBorderColor string
setBgActiveBorderColor string
)
func init() {
rootCmd.AddCommand(setBgCmd)
setBgCmd.Flags().Float64Var(&setBgOpacity, "opacity", 0.5, "background opacity (0.0-1.0)")
setBgCmd.Flags().BoolVar(&setBgTile, "tile", false, "tile the background image")
setBgCmd.Flags().BoolVar(&setBgCenter, "center", false, "center the image without scaling")
setBgCmd.Flags().StringVar(&setBgSize, "size", "auto", "size for centered images (px, %, or auto)")
setBgCmd.Flags().BoolVar(&setBgClear, "clear", false, "clear the background")
setBgCmd.Flags().BoolVar(&setBgPrint, "print", false, "print the metadata without applying it")
setBgCmd.Flags().StringVar(&setBgBorderColor, "border-color", "", "block frame border color (#RRGGBB, #RRGGBBAA, or CSS color name)")
setBgCmd.Flags().StringVar(&setBgActiveBorderColor, "active-border-color", "", "block frame focused border color (#RRGGBB, #RRGGBBAA, or CSS color name)")
setBgCmd.MarkFlagsMutuallyExclusive("tile", "center")
}
func validateHexColor(color string) error {
if !strings.HasPrefix(color, "#") {
return fmt.Errorf("color must start with #")
}
colorHex := color[1:]
if len(colorHex) != 6 && len(colorHex) != 8 {
return fmt.Errorf("color must be in #RRGGBB or #RRGGBBAA format")
}
_, err := hex.DecodeString(colorHex)
if err != nil {
return fmt.Errorf("invalid hex color: %v", err)
}
return nil
}
func validateColor(color string) error {
if strings.HasPrefix(color, "#") {
return validateHexColor(color)
}
if !CssColorNames[strings.ToLower(color)] {
return fmt.Errorf("invalid color %q: must be a hex color (#RRGGBB or #RRGGBBAA) or a CSS color name", color)
}
return nil
}
func setBgRun(cmd *cobra.Command, args []string) (rtnErr error) {
defer func() {
sendActivity("setbg", rtnErr == nil)
}()
borderColorChanged := cmd.Flags().Changed("border-color")
activeBorderColorChanged := cmd.Flags().Changed("active-border-color")
if borderColorChanged {
if err := validateColor(setBgBorderColor); err != nil {
return fmt.Errorf("--border-color: %v", err)
}
}
if activeBorderColorChanged {
if err := validateColor(setBgActiveBorderColor); err != nil {
return fmt.Errorf("--active-border-color: %v", err)
}
}
// Create base metadata
meta := map[string]interface{}{}
// Handle opacity-only change or clear
if len(args) == 0 {
if !cmd.Flags().Changed("opacity") && !setBgClear && !borderColorChanged && !activeBorderColorChanged {
OutputHelpMessage(cmd)
return fmt.Errorf("setbg requires an image path or color value")
}
if setBgOpacity < 0 || setBgOpacity > 1 {
return fmt.Errorf("opacity must be between 0.0 and 1.0")
}
if setBgClear {
meta["bg:*"] = true
} else if cmd.Flags().Changed("opacity") {
meta["bg:opacity"] = setBgOpacity
}
} else if len(args) > 1 {
OutputHelpMessage(cmd)
return fmt.Errorf("too many arguments")
} else {
// Handle background setting
meta["bg:*"] = true
meta["tab:background"] = nil
if setBgOpacity < 0 || setBgOpacity > 1 {
return fmt.Errorf("opacity must be between 0.0 and 1.0")
}
meta["bg:opacity"] = setBgOpacity
input := args[0]
var bgStyle string
// Check for hex color
if strings.HasPrefix(input, "#") {
if err := validateHexColor(input); err != nil {
return err
}
bgStyle = input
} else if CssColorNames[strings.ToLower(input)] {
// Handle CSS color name
bgStyle = strings.ToLower(input)
} else {
// Handle image input
absPath, err := filepath.Abs(wavebase.ExpandHomeDirSafe(input))
if err != nil {
return fmt.Errorf("resolving image path: %v", err)
}
fileInfo, err := os.Stat(absPath)
if err != nil {
return fmt.Errorf("cannot access image file: %v", err)
}
if fileInfo.IsDir() {
return fmt.Errorf("path is a directory, not an image file")
}
mimeType := fileutil.DetectMimeType(absPath, fileInfo, true)
switch mimeType {
case "image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml":
// Valid image type
default:
return fmt.Errorf("file does not appear to be a valid image (detected type: %s)", mimeType)
}
// Create URL-safe path
escapedPath := filepath.ToSlash(absPath)
escapedPath = strings.ReplaceAll(escapedPath, "'", "\\'")
bgStyle = fmt.Sprintf("url('%s')", escapedPath)
switch {
case setBgTile:
bgStyle += " repeat"
case setBgCenter:
bgStyle += fmt.Sprintf(" no-repeat center/%s", setBgSize)
default:
bgStyle += " center/cover no-repeat"
}
}
meta["bg"] = bgStyle
}
if borderColorChanged {
meta["bg:bordercolor"] = setBgBorderColor
}
if activeBorderColorChanged {
meta["bg:activebordercolor"] = setBgActiveBorderColor
}
if setBgPrint {
jsonBytes, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return fmt.Errorf("error formatting metadata: %v", err)
}
WriteStdout("%s\n", string(jsonBytes))
return nil
}
// Resolve tab reference
id := blockArg
if id == "" {
id = "tab"
}
oRef, err := resolveSimpleId(id)
if err != nil {
return err
}
// Send RPC request
setMetaWshCmd := wshrpc.CommandSetMetaData{
ORef: *oRef,
Meta: meta,
}
err = wshclient.SetMetaCommand(RpcClient, setMetaWshCmd, &wshrpc.RpcOpts{Timeout: 2000})
if err != nil {
return fmt.Errorf("setting background: %v", err)
}
WriteStdout("background set\n")
return nil
}