| 1 |
package jl |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
) |
| 6 |
|
| 7 |
type sequentialColorizer struct { |
| 8 |
assigned map[string]Color |
| 9 |
seq int |
| 10 |
colors []Color |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
func ColorSequence(colors []Color) *sequentialColorizer { |
| 16 |
return &sequentialColorizer{ |
| 17 |
assigned: make(map[string]Color), |
| 18 |
colors: colors, |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func (a *sequentialColorizer) Transform(ctx *Context, input string) string { |
| 23 |
if ctx.DisableColor { |
| 24 |
return input |
| 25 |
} |
| 26 |
if color, ok := a.assigned[ctx.Original]; ok { |
| 27 |
return ColorText(color, input) |
| 28 |
} |
| 29 |
color := a.colors[a.seq%len(AllColors)] |
| 30 |
a.seq++ |
| 31 |
a.assigned[ctx.Original] = color |
| 32 |
return ColorText(color, input) |
| 33 |
} |
| 34 |
|
| 35 |
type mappingColorizer struct { |
| 36 |
mapping map[string]Color |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func ColorMap(mapping map[string]Color) *mappingColorizer { |
| 41 |
lowered := make(map[string]Color, len(mapping)) |
| 42 |
for k, v := range mapping { |
| 43 |
lowered[strings.ToLower(k)] = v |
| 44 |
} |
| 45 |
return &mappingColorizer{lowered} |
| 46 |
} |
| 47 |
|
| 48 |
func (c *mappingColorizer) Transform(ctx *Context, input string) string { |
| 49 |
if ctx.DisableColor { |
| 50 |
return input |
| 51 |
} |
| 52 |
if color, ok := c.mapping[strings.ToLower(ctx.Original)]; ok { |
| 53 |
return ColorText(color, input) |
| 54 |
} |
| 55 |
return input |
| 56 |
} |