-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy paththemes.go
More file actions
283 lines (232 loc) · 7.17 KB
/
themes.go
File metadata and controls
283 lines (232 loc) · 7.17 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package list
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cloudposse/atmos/internal/tui/templates/term"
"github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/flags"
"github.com/cloudposse/atmos/pkg/flags/global"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
"github.com/cloudposse/atmos/pkg/ui/theme"
)
var themesParser *flags.StandardParser
// ThemesOptions contains parsed flags for the themes command.
type ThemesOptions struct {
global.Flags
All bool
}
// themesCmd lists available terminal themes (alias for 'theme list').
var themesCmd = &cobra.Command{
Use: "themes",
Short: "List available terminal themes (alias for 'theme list')",
Long: "Display available terminal themes that can be used for markdown rendering. By default shows recommended themes.\nThis is an alias for 'atmos theme list'.",
Example: "atmos list themes\n" +
"atmos list themes --all",
Args: cobra.NoArgs,
RunE: executeListThemes,
}
const (
maxSourceLength = 50
sourceEllipsisLen = 47
lineWidth = 80
)
func init() {
// Create parser with themes-specific flags using functional options
themesParser = flags.NewStandardParser(
flags.WithBoolFlag("all", "", false, "Show all available themes (default: show only recommended themes)"),
flags.WithEnvVars("all", "ATMOS_LIST_ALL_THEMES"),
)
// Register flags
themesParser.RegisterFlags(themesCmd)
// Bind flags to Viper for environment variable support
if err := themesParser.BindToViper(viper.GetViper()); err != nil {
panic(err)
}
}
// executeListThemes runs the list themes command.
func executeListThemes(cmd *cobra.Command, args []string) error {
// Parse flags using StandardParser with Viper precedence
v := viper.GetViper()
if err := themesParser.BindFlagsToViper(cmd, v); err != nil {
return err
}
opts := &ThemesOptions{
Flags: flags.ParseGlobalFlags(cmd, v),
All: v.GetBool("all"),
}
return executeListThemesWithOptions(opts)
}
func executeListThemesWithOptions(opts *ThemesOptions) error {
// Get the current active theme from configuration
configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := config.InitCliConfig(configAndStacksInfo, false)
activeTheme := ""
if err == nil {
activeTheme = atmosConfig.Settings.Terminal.Theme
}
themes, err := listThemes()
if err != nil {
return err
}
// Default behavior: show only recommended themes
// --all flag: show everything
if !opts.All {
themes = filterRecommendedThemes(themes, activeTheme)
}
return displayThemes(opts, themes, activeTheme, !opts.All)
}
// listThemes retrieves all available themes.
func listThemes() ([]*theme.Theme, error) {
registry, err := theme.NewRegistry()
if err != nil {
return nil, fmt.Errorf("failed to load theme registry: %w", err)
}
return registry.List(), nil
}
// filterRecommendedThemes returns only recommended themes, but ensures the active theme is included.
func filterRecommendedThemes(themes []*theme.Theme, activeTheme string) []*theme.Theme {
var recommended []*theme.Theme
hasActiveTheme := false
for _, t := range themes {
if theme.IsRecommended(t.Name) {
recommended = append(recommended, t)
if strings.EqualFold(t.Name, activeTheme) {
hasActiveTheme = true
}
}
}
// If the active theme is not in the recommended list, add it
if activeTheme != "" && !hasActiveTheme {
for _, t := range themes {
if strings.EqualFold(t.Name, activeTheme) {
recommended = append(recommended, t)
break
}
}
}
// Sort the themes by name for consistent output
sort.Slice(recommended, func(i, j int) bool {
return recommended[i].Name < recommended[j].Name
})
return recommended
}
// displayThemes formats and displays the themes to the terminal.
func displayThemes(opts *ThemesOptions, themes []*theme.Theme, activeTheme string, showingRecommendedOnly bool) error {
// Check if we're in TTY mode
if !term.IsTTYSupportForStdout() {
// Fall back to simple text output for non-TTY
output := formatSimpleOutput(opts, themes, activeTheme, showingRecommendedOnly)
ui.Write(output)
return nil
}
output := formatThemesTable(opts, themes, activeTheme, showingRecommendedOnly)
ui.Write(output)
return nil
}
// formatThemesTable formats themes into a styled Charmbracelet table.
func formatThemesTable(opts *ThemesOptions, themes []*theme.Theme, activeTheme string, showingRecommendedOnly bool) string {
// Prepare headers and rows
headers := []string{"", "Name", "Type", "Source"}
var rows [][]string
for _, t := range themes {
// Active indicator
activeIndicator := " "
if t.Name == activeTheme {
activeIndicator = "> "
}
// Theme name with recommended indicator (only show star when --all is used)
name := t.Name
if opts.All && theme.IsRecommended(t.Name) {
name += " ★"
}
// Theme type (Dark/Light)
themeType := getThemeType(t)
// Source
source := getThemeSource(t)
if len(source) > maxSourceLength {
source = source[:sourceEllipsisLen] + "..."
}
row := []string{
activeIndicator,
name,
themeType,
source,
}
rows = append(rows, row)
}
// Use the new themed table creation
output := theme.CreateThemedTable(headers, rows) + "\n"
// Footer message
styles := theme.GetCurrentStyles()
footer := fmt.Sprintf("\n%d theme", len(themes))
if len(themes) != 1 {
footer += "s"
}
if showingRecommendedOnly {
footer += " (recommended). Use --all to see all available themes."
} else {
footer += " available."
}
if activeTheme != "" {
footer += fmt.Sprintf("\nActive theme: %s", activeTheme)
}
output += styles.Footer.Render(footer) + "\n"
return output
}
// formatSimpleOutput formats themes as simple text for non-TTY output.
func formatSimpleOutput(opts *ThemesOptions, themes []*theme.Theme, activeTheme string, showingRecommendedOnly bool) string {
var output string
// Header
output += fmt.Sprintf(" %-30s %-8s %-4s %s\n", "Name", "Type", "Rec", "Source")
output += fmt.Sprintf("%s\n", strings.Repeat("=", lineWidth))
// Theme rows
for _, t := range themes {
activeIndicator := " "
if t.Name == activeTheme {
activeIndicator = "> "
}
recommended := ""
if opts.All && theme.IsRecommended(t.Name) {
recommended = "★"
}
themeType := getThemeType(t)
source := getThemeSource(t)
output += fmt.Sprintf("%-2s %-30s %-8s %-4s %s\n", activeIndicator, t.Name, themeType, recommended, source)
}
// Footer message
output += fmt.Sprintf("\n%d theme", len(themes))
if len(themes) != 1 {
output += "s"
}
if showingRecommendedOnly {
output += " (recommended). Use --all to see all available themes.\n"
} else {
output += " available.\n"
}
if activeTheme != "" {
output += fmt.Sprintf("Active theme: %s\n", activeTheme)
}
return output
}
// getThemeType returns "Dark" or "Light" based on theme metadata.
func getThemeType(t *theme.Theme) string {
if t.Meta.IsDark {
return "Dark"
}
return "Light"
}
// getThemeSource extracts the source information from theme credits.
func getThemeSource(t *theme.Theme) string {
if t.Meta.Credits != nil && len(*t.Meta.Credits) > 0 {
credits := *t.Meta.Credits
if credits[0].Link != "" {
return credits[0].Link
}
return credits[0].Name
}
return ""
}