turbo-editors/turbo-corepublic Fork 0
v0.9.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v0.9.0 · k33g · 17h ago
load.go · 325 lines · 9.3 KBGo Blame HistoryRaw
  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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package theme

import (
	"embed"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"github.com/BurntSushi/toml"
	"github.com/gdamore/tcell/v2"
)

//go:embed themes/*.toml
var embeddedThemes embed.FS

// embeddedDir is where the built-in theme files live inside the binary.
const embeddedDir = "themes"

// DefaultName is the theme used when the user has not chosen one.
const DefaultName = "turbo-classic"

// ErrNotFound is returned by Load when no theme of that name exists, either in
// the user's directory or among the embedded ones.
var ErrNotFound = errors.New("theme: not found")

// maxInheritDepth stops a chain of "inherits" from looping forever.
const maxInheritDepth = 16

// retiredNames maps a name a shipped theme used to answer to onto the name it
// answers to now.
//
// A theme's name is not this project's to change: it is what somebody wrote in
// a settings file their whole team shares, and what they typed after -theme.
// When "monochrome" gained a light counterpart, keeping the pair symmetric was
// worth a rename and breaking those files was not, so the old name still
// loads.
//
// An alias is deliberately *not* listed by Available, so the theme dialog and
// -list-themes show each theme once, under the name it has now. What it costs
// is that a name here can never be reused for a different theme, which is the
// same promise the rename was made to keep.
var retiredNames = map[string]string{
	"monochrome": "monochrome-dark",
}

// file is the on-disk shape of a theme.
type file struct {
	Name        string           `toml:"name"`
	Description string           `toml:"description"`
	Inherits    string           `toml:"inherits"`
	Colors      map[string]Entry `toml:"colors"`
}

// Load returns the theme called name, looking in userDir before the themes
// embedded in the binary.
//
// A file in userDir wins over an embedded theme of the same name, so a shipped
// theme can be overridden without being replaced. Pass "" for userDir to offer
// the embedded themes alone, which is what a system with no configuration
// directory gets.
//
//	th, err := theme.Load("turbo-classic", p.ThemeDir())
//	if err != nil {
//		th = theme.Default(p.ThemeDir())
//	}
func Load(name, userDir string) (*Theme, error) {
	return loadNamed(name, userDir, 0)
}

// Default returns the theme used when the user has not chosen one, honouring
// a file of that name in userDir the way Load does.
//
// The embedded file it reads is checked by a test, so this cannot fail in
// practice; if it ever did, a plain white-on-black theme is returned rather
// than nothing, because the editor must always have something to draw with.
func Default(userDir string) *Theme {
	th, err := Load(DefaultName, userDir)
	if err != nil {
		return &Theme{
			name:   DefaultName,
			styles: map[string]tcell.Style{KeyDefault: fallbackStyle()},
		}
	}
	return th
}

// Available returns the names of every theme that can be loaded, from userDir
// and from the ones embedded in the binary, sorted and without duplicates.
func Available(userDir string) []string {
	seen := map[string]bool{}
	for _, name := range embeddedNames() {
		seen[name] = true
	}
	for _, name := range userNames(userDir) {
		seen[name] = true
	}

	names := make([]string, 0, len(seen))
	for name := range seen {
		names = append(names, name)
	}
	sort.Strings(names)
	return names
}

// LoadFile reads a theme from a TOML file on disk.
//
// userDir is where a theme this one inherits from is looked for, and is
// otherwise unused.
func LoadFile(path, userDir string) (*Theme, error) {
	return loadFileAt(path, userDir, 0)
}

// loadFileAt is LoadFile plus the recursion depth, which must be carried
// across every hop of an "inherits" chain for the loop guard to see it.
func loadFileAt(path, userDir string, depth int) (*Theme, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("theme: read %s: %w", path, err)
	}

	th, err := parse(data, userDir, depth)
	if err != nil {
		return nil, fmt.Errorf("theme: %s: %w", path, err)
	}
	if th.name == "" {
		th.name = strings.TrimSuffix(filepath.Base(path), ".toml")
	}
	return th, nil
}

// Parse reads a theme from the content of a TOML file.
//
// Every colour is resolved eagerly, so a typo in a colour name is reported
// here rather than silently drawn in the terminal's default colour halfway
// through an editing session.
func Parse(data []byte, userDir string) (*Theme, error) {
	return parse(data, userDir, 0)
}

// parse is Parse plus the recursion depth, which caps chains of "inherits".
func parse(data []byte, userDir string, depth int) (*Theme, error) {
	var f file
	if _, err := toml.Decode(string(data), &f); err != nil {
		return nil, fmt.Errorf("invalid TOML: %w", err)
	}

	base, err := inheritedStyles(f.Inherits, userDir, depth)
	if err != nil {
		return nil, err
	}

	styles, err := resolveStyles(f.Colors, base)
	if err != nil {
		return nil, err
	}

	return &Theme{name: f.Name, description: f.Description, styles: styles}, nil
}

// inheritedStyles returns the resolved styles of the parent theme, or a bare
// default when there is no parent.
func inheritedStyles(parent, userDir string, depth int) (map[string]tcell.Style, error) {
	if parent == "" {
		return map[string]tcell.Style{KeyDefault: fallbackStyle()}, nil
	}
	if depth >= maxInheritDepth {
		return nil, fmt.Errorf("inherits: chain deeper than %d, probably a loop", maxInheritDepth)
	}

	th, err := loadNamed(parent, userDir, depth+1)
	if err != nil {
		return nil, fmt.Errorf("inherits %q: %w", parent, err)
	}

	styles := make(map[string]tcell.Style, len(th.styles))
	for key, style := range th.styles {
		styles[key] = style
	}
	return styles, nil
}

// resolveStyles turns the entries of a theme file into styles, resolving
// shallower keys first so that "syntax.keyword" can inherit from "syntax",
// and "syntax" from "default".
func resolveStyles(entries map[string]Entry, base map[string]tcell.Style) (map[string]tcell.Style, error) {
	if _, ok := base[KeyDefault]; !ok {
		base[KeyDefault] = fallbackStyle()
	}

	for _, key := range byDepth(entries) {
		style, err := entries[key].style(lookup(base, parentKey(key)))
		if err != nil {
			return nil, fmt.Errorf("colors.%q: %w", key, err)
		}
		base[key] = style
	}
	return base, nil
}

// byDepth returns the keys of entries sorted by how many dots they contain,
// then alphabetically so that the order is stable.
func byDepth(entries map[string]Entry) []string {
	keys := make([]string, 0, len(entries))
	for key := range entries {
		keys = append(keys, key)
	}
	sort.Slice(keys, func(i, j int) bool {
		di, dj := strings.Count(keys[i], "."), strings.Count(keys[j], ".")
		if di != dj {
			return di < dj
		}
		return keys[i] < keys[j]
	})
	return keys
}

// parentKey returns the key one level up: "syntax.keyword" gives "syntax", and
// a key with no dot gives KeyDefault.
func parentKey(key string) string {
	if dot := strings.LastIndexByte(key, '.'); dot >= 0 {
		return key[:dot]
	}
	return KeyDefault
}

// lookup reads a key from resolved styles, walking up the dots as Style does.
func lookup(styles map[string]tcell.Style, key string) tcell.Style {
	for {
		if style, ok := styles[key]; ok {
			return style
		}
		dot := strings.LastIndexByte(key, '.')
		if dot < 0 {
			return styles[KeyDefault]
		}
		key = key[:dot]
	}
}

// loadNamed is Load plus the recursion depth used by "inherits".
func loadNamed(name, userDir string, depth int) (*Theme, error) {
	if err := validateName(name); err != nil {
		return nil, err
	}

	if userDir != "" {
		path := filepath.Join(userDir, name+".toml")
		if _, err := os.Stat(path); err == nil {
			return loadFileAt(path, userDir, depth)
		}
	}

	// After the user's directory, never before it: somebody who has written
	// their own monochrome.toml gets theirs, exactly as they would for any
	// other name a shipped theme also answers to.
	if current, retired := retiredNames[name]; retired {
		name = current
	}

	data, err := embeddedThemes.ReadFile(embeddedDir + "/" + name + ".toml")
	if err != nil {
		return nil, fmt.Errorf("%w: %q", ErrNotFound, name)
	}

	th, err := parse(data, userDir, depth)
	if err != nil {
		return nil, fmt.Errorf("theme: embedded %s: %w", name, err)
	}
	if th.name == "" {
		th.name = name
	}
	return th, nil
}

// validateName rejects names that would let a theme reference escape the
// directories themes are meant to come from.
func validateName(name string) error {
	if name == "" {
		return fmt.Errorf("%w: empty name", ErrNotFound)
	}
	if strings.ContainsAny(name, `/\`) || strings.Contains(name, "..") {
		return fmt.Errorf("%w: %q is not a plain theme name", ErrNotFound, name)
	}
	return nil
}

// embeddedNames lists the themes compiled into the binary.
func embeddedNames() []string {
	entries, err := embeddedThemes.ReadDir(embeddedDir)
	if err != nil {
		return nil
	}
	return tomlBaseNames(entries)
}

// userNames lists the themes in the user's theme directory, which may well not
// exist.
func userNames(userDir string) []string {
	if userDir == "" {
		return nil
	}
	entries, err := os.ReadDir(userDir)
	if err != nil {
		return nil
	}
	return tomlBaseNames(entries)
}

// tomlBaseNames returns the names of the .toml files among entries, without
// their extension.
func tomlBaseNames(entries []fs.DirEntry) []string {
	var names []string
	for _, entry := range entries {
		if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".toml") {
			continue
		}
		names = append(names, strings.TrimSuffix(entry.Name(), ".toml"))
	}
	return names
}