|
| 1 | +package theme |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/CosmicPredator/chibi/internal/kvdb" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + ThemeEnvName = "CHIBI_THEME" |
| 18 | + themeKey = "theme" |
| 19 | +) |
| 20 | + |
| 21 | +type Palette struct { |
| 22 | + SuccessText string |
| 23 | + ErrorText string |
| 24 | + HighlightText string |
| 25 | + |
| 26 | + Spinner string |
| 27 | + PromptTitle string |
| 28 | + PromptDefault string |
| 29 | + |
| 30 | + KeyText string |
| 31 | + ValueText string |
| 32 | + |
| 33 | + TableHeader string |
| 34 | + TableID string |
| 35 | + TableFormat string |
| 36 | + TableMetric string |
| 37 | + TableRepeating string |
| 38 | + |
| 39 | + MessageError string |
| 40 | + MessageSuccess string |
| 41 | + MessageOther string |
| 42 | +} |
| 43 | + |
| 44 | +var palettes = map[string]Palette{ |
| 45 | + "default": { |
| 46 | + SuccessText: "#00FF00", |
| 47 | + ErrorText: "#CC0000", |
| 48 | + HighlightText: "#00FFFF", |
| 49 | + Spinner: "5", |
| 50 | + PromptTitle: "5", |
| 51 | + PromptDefault: "1", |
| 52 | + KeyText: "#FF79C6", |
| 53 | + ValueText: "#8BE9FD", |
| 54 | + TableHeader: "5", |
| 55 | + TableID: "6", |
| 56 | + TableFormat: "2", |
| 57 | + TableMetric: "3", |
| 58 | + TableRepeating: "4", |
| 59 | + MessageError: "#FF0000", |
| 60 | + MessageSuccess: "#00FF00", |
| 61 | + MessageOther: "#00FFFF", |
| 62 | + }, |
| 63 | + "nord": { |
| 64 | + SuccessText: "#A3BE8C", |
| 65 | + ErrorText: "#BF616A", |
| 66 | + HighlightText: "#88C0D0", |
| 67 | + Spinner: "#81A1C1", |
| 68 | + PromptTitle: "#81A1C1", |
| 69 | + PromptDefault: "#D08770", |
| 70 | + KeyText: "#B48EAD", |
| 71 | + ValueText: "#8FBCBB", |
| 72 | + TableHeader: "#81A1C1", |
| 73 | + TableID: "#88C0D0", |
| 74 | + TableFormat: "#A3BE8C", |
| 75 | + TableMetric: "#EBCB8B", |
| 76 | + TableRepeating: "#5E81AC", |
| 77 | + MessageError: "#BF616A", |
| 78 | + MessageSuccess: "#A3BE8C", |
| 79 | + MessageOther: "#88C0D0", |
| 80 | + }, |
| 81 | + "sunset": { |
| 82 | + SuccessText: "#43A047", |
| 83 | + ErrorText: "#E53935", |
| 84 | + HighlightText: "#00ACC1", |
| 85 | + Spinner: "#FB8C00", |
| 86 | + PromptTitle: "#FB8C00", |
| 87 | + PromptDefault: "#F4511E", |
| 88 | + KeyText: "#8E24AA", |
| 89 | + ValueText: "#039BE5", |
| 90 | + TableHeader: "#FB8C00", |
| 91 | + TableID: "#1E88E5", |
| 92 | + TableFormat: "#43A047", |
| 93 | + TableMetric: "#FDD835", |
| 94 | + TableRepeating: "#3949AB", |
| 95 | + MessageError: "#E53935", |
| 96 | + MessageSuccess: "#43A047", |
| 97 | + MessageOther: "#00ACC1", |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +var ( |
| 102 | + mu sync.RWMutex |
| 103 | + current = "default" |
| 104 | +) |
| 105 | + |
| 106 | +func normalizeThemeName(name string) string { |
| 107 | + return strings.TrimSpace(strings.ToLower(name)) |
| 108 | +} |
| 109 | + |
| 110 | +func Current() Palette { |
| 111 | + mu.RLock() |
| 112 | + defer mu.RUnlock() |
| 113 | + return palettes[current] |
| 114 | +} |
| 115 | + |
| 116 | +func CurrentName() string { |
| 117 | + mu.RLock() |
| 118 | + defer mu.RUnlock() |
| 119 | + return current |
| 120 | +} |
| 121 | + |
| 122 | +func Available() []string { |
| 123 | + out := make([]string, 0, len(palettes)) |
| 124 | + for name := range palettes { |
| 125 | + out = append(out, name) |
| 126 | + } |
| 127 | + sort.Strings(out) |
| 128 | + return out |
| 129 | +} |
| 130 | + |
| 131 | +func SetCurrent(name string) error { |
| 132 | + normalized := normalizeThemeName(name) |
| 133 | + if normalized == "" { |
| 134 | + return errors.New("theme name cannot be empty") |
| 135 | + } |
| 136 | + if _, ok := palettes[normalized]; !ok { |
| 137 | + return fmt.Errorf( |
| 138 | + "unknown theme %q (available: %s)", |
| 139 | + name, |
| 140 | + strings.Join(Available(), ", "), |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + mu.Lock() |
| 145 | + current = normalized |
| 146 | + mu.Unlock() |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func Save(name string) error { |
| 151 | + if err := SetCurrent(name); err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + db, err := kvdb.Open() |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + defer db.Close() |
| 160 | + |
| 161 | + return db.Set(context.TODO(), themeKey, []byte(CurrentName())) |
| 162 | +} |
| 163 | + |
| 164 | +func Load() error { |
| 165 | + if envTheme := strings.TrimSpace(os.Getenv(ThemeEnvName)); envTheme != "" { |
| 166 | + return SetCurrent(envTheme) |
| 167 | + } |
| 168 | + |
| 169 | + db, err := kvdb.Open() |
| 170 | + if err != nil { |
| 171 | + return nil |
| 172 | + } |
| 173 | + defer db.Close() |
| 174 | + |
| 175 | + rawTheme, err := db.Get(context.TODO(), themeKey) |
| 176 | + if err != nil { |
| 177 | + if errors.Is(err, sql.ErrNoRows) { |
| 178 | + return nil |
| 179 | + } |
| 180 | + return nil |
| 181 | + } |
| 182 | + |
| 183 | + name := strings.TrimSpace(string(rawTheme)) |
| 184 | + if name == "" { |
| 185 | + return nil |
| 186 | + } |
| 187 | + |
| 188 | + // Ignore stale values from old/invalid config and fallback to default. |
| 189 | + if err := SetCurrent(name); err != nil { |
| 190 | + return nil |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
0 commit comments