-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
335 lines (270 loc) · 7.54 KB
/
registry.go
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
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package tint
import (
"sort"
"sync"
"github.com/charmbracelet/lipgloss"
)
// NewRegistry returns a new Registry, with the provided tints registered.
func NewRegistry(defaultTint Tint, tints ...Tint) *Registry {
r := &Registry{
tints: make(map[string]Tint),
}
r.Register(tints...)
if defaultTint != nil {
r.SetTint(defaultTint)
}
return r
}
type Registry struct {
mu sync.RWMutex
currentTint string
tints map[string]Tint
}
// Register registers one or more tints within the registry. It does not change
// to the provided tint.
func (r *Registry) Register(tint ...Tint) {
r.mu.Lock()
for _, t := range tint {
r.tints[t.ID()] = t
}
r.mu.Unlock()
}
// Unregister unregisters one or more provided tints from the registry.
func (r *Registry) Unregister(tint ...Tint) {
r.mu.Lock()
for _, t := range tint {
delete(r.tints, t.ID())
}
r.mu.Unlock()
}
// UnregisterIDs unregisters the tints with the provided ID(s) from the registry.
func (r *Registry) UnregisterIDs(id ...string) {
r.mu.Lock()
for _, t := range id {
delete(r.tints, t)
}
r.mu.Unlock()
}
// UnregisterAll unregisters all tints from the registry.
func (r *Registry) UnregisterAll() {
r.mu.Lock()
for id := range r.tints {
delete(r.tints, id)
}
r.mu.Unlock()
}
// Tints returns a list of all registered tints, sorted alphabetically by their ID.
func (r *Registry) Tints() []Tint {
v := []Tint{}
r.mu.RLock()
for _, tint := range r.tints {
v = append(v, tint)
}
r.mu.RUnlock()
sort.Slice(v, func(i, j int) bool {
return v[i].ID() < v[j].ID()
})
return v
}
// TintIDs returns a list of all registered tint IDs, sorted alphabetically by their
// ID.
func (r *Registry) TintIDs() (ids []string) {
r.mu.RLock()
for _, tint := range r.tints {
ids = append(ids, tint.ID())
}
r.mu.RUnlock()
sort.Strings(ids)
return ids
}
// GetTint returns the tint with the provided ID, or nil if it doesn't exist.
func (r *Registry) GetTint(id string) (tint Tint, ok bool) {
r.mu.RLock()
tint, ok = r.tints[id]
r.mu.RUnlock()
return tint, ok
}
// SetTint sets the current tint to the provided tint, and adds it to the list of
// registered tints if it isn't already.
func (r *Registry) SetTint(tint Tint) {
r.Register(tint) // Register if not already done.
id := tint.ID()
r.mu.Lock()
r.currentTint = id
r.mu.Unlock()
}
// SetTintID sets the current tint to the provided tint ID.
func (r *Registry) SetTintID(id string) (ok bool) {
_, ok = r.GetTint(id)
if !ok {
return ok
}
r.mu.Lock()
r.currentTint = id
r.mu.Unlock()
return true
}
// PreviousTint sets the current tint to the previous tint in the list of
// registered tints. If the current tint is the first tint in the list, it will
// pin to the first tint in the list (i.e. won't rollback to the end).
//
// PreviousTint uses a sorted list of tint IDs.
func (r *Registry) PreviousTint() {
id := r.ID()
tints := r.TintIDs()
if len(tints) == 0 {
return
}
if id == "" {
r.SetTintID(tints[0])
return
}
currentI := 0
for i, t := range tints {
if t == id {
currentI = i
break
}
}
prevI := currentI - 1
if prevI < 1 {
prevI = 0
}
r.SetTintID(tints[prevI])
}
// NextTint sets the current tint to the next tint in the list of registered
// tints. If the current tint is the last tint in the list, it will pin to the
// last tint in the list (i.e. won't rollback to the start).
//
// NextTint uses a sorted list of tint IDs.
func (r *Registry) NextTint() {
id := r.ID()
tints := r.TintIDs()
if len(tints) == 0 {
return
}
if id == "" {
r.SetTintID(tints[0])
return
}
currentI := 0
for i, t := range tints {
if t == id {
currentI = i
break
}
}
nextI := currentI + 1
if nextI >= len(tints) {
nextI = len(tints) - 1
}
r.SetTintID(tints[nextI])
}
// GetCurrenTint returns the current tint.
func (r *Registry) GetCurrentTint() (tint Tint) {
id := r.ID()
if id == "" {
panic("no tint set")
}
tint, _ = r.GetTint(id)
return tint
}
// DisplayName returns the display name of the tint.
func (r *Registry) DisplayName() string {
return r.GetCurrentTint().DisplayName()
}
// ID returns the name of the current tint (normalized, snakecase style).
func (r *Registry) ID() (id string) {
r.mu.RLock()
id = r.currentTint
r.mu.RUnlock()
return id
}
// About returns information about the tint (and if we have credit for who
// assisted with/created it).
func (r *Registry) About() string {
return r.GetCurrentTint().About()
}
// Fg returns the recommended default foreground color for this tint.
func (r *Registry) Fg() lipgloss.TerminalColor {
return r.GetCurrentTint().Fg()
}
// Bg returns the recommended default background color for this tint.
func (r *Registry) Bg() lipgloss.TerminalColor {
return r.GetCurrentTint().Bg()
}
// SelectionBg returns the recommended background color for selected text.
func (r *Registry) SelectionBg() lipgloss.TerminalColor {
return r.GetCurrentTint().SelectionBg()
}
// Cursor returns the recommended color for the cursor.
func (r *Registry) Cursor() lipgloss.TerminalColor {
return r.GetCurrentTint().Cursor()
}
// BrightBlack returns the recommended color for bright black.
func (r *Registry) BrightBlack() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightBlack()
}
// BrightBlue returns the recommended color for bright blue.
func (r *Registry) BrightBlue() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightBlue()
}
// BrightCyan returns the recommended color for bright cyan.
func (r *Registry) BrightCyan() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightCyan()
}
// BrightGreen returns the recommended color for bright green.
func (r *Registry) BrightGreen() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightGreen()
}
// BrightPurple returns the recommended color for bright purple.
func (r *Registry) BrightPurple() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightPurple()
}
// BrightRed returns the recommended color for bright red.
func (r *Registry) BrightRed() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightRed()
}
// BrightWhite returns the recommended color for bright white.
func (r *Registry) BrightWhite() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightWhite()
}
// BrightYellow returns the recommended color for bright yellow.
func (r *Registry) BrightYellow() lipgloss.TerminalColor {
return r.GetCurrentTint().BrightYellow()
}
// Black returns the recommended color for black.
func (r *Registry) Black() lipgloss.TerminalColor {
return r.GetCurrentTint().Black()
}
// Blue returns the recommended color for blue.
func (r *Registry) Blue() lipgloss.TerminalColor {
return r.GetCurrentTint().Blue()
}
// Cyan returns the recommended color for cyan.
func (r *Registry) Cyan() lipgloss.TerminalColor {
return r.GetCurrentTint().Cyan()
}
// Green returns the recommended color for green.
func (r *Registry) Green() lipgloss.TerminalColor {
return r.GetCurrentTint().Green()
}
// Purple returns the recommended color for purple.
func (r *Registry) Purple() lipgloss.TerminalColor {
return r.GetCurrentTint().Purple()
}
// Red returns the recommended color for red.
func (r *Registry) Red() lipgloss.TerminalColor {
return r.GetCurrentTint().Red()
}
// White returns the recommended color for white.
func (r *Registry) White() lipgloss.TerminalColor {
return r.GetCurrentTint().White()
}
// Yellow returns the recommended color for yellow.
func (r *Registry) Yellow() lipgloss.TerminalColor {
return r.GetCurrentTint().Yellow()
}