forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
91 lines (79 loc) · 2.45 KB
/
transform.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
package styled
import (
"strings"
)
// FindTransformer looks up a transformer name and if successful returns a
// function that can be used to transform a styled Segment.
func FindTransformer(transformerName string) func(Segment) Segment {
var innerTransformer func(*Segment)
switch {
// Catch special colors early
case transformerName == "default":
innerTransformer = func(s *Segment) { s.Foreground = "" }
case transformerName == "bg-default":
innerTransformer = func(s *Segment) { s.Background = "" }
case strings.HasPrefix(transformerName, "bg-"):
innerTransformer = buildColorTransformer(strings.TrimPrefix(transformerName, "bg-"), false)
case strings.HasPrefix(transformerName, "no-"):
innerTransformer = buildBoolTransformer(strings.TrimPrefix(transformerName, "no-"), false, false)
case strings.HasPrefix(transformerName, "toggle-"):
innerTransformer = buildBoolTransformer(strings.TrimPrefix(transformerName, "toggle-"), false, true)
default:
innerTransformer = buildColorTransformer(transformerName, true)
if innerTransformer == nil {
innerTransformer = buildBoolTransformer(transformerName, true, false)
}
}
if innerTransformer == nil {
return nil
}
return func(segment Segment) Segment {
innerTransformer(&segment)
return segment
}
}
func buildColorTransformer(transformerName string, setForeground bool) func(*Segment) {
if isValidColorName(transformerName) {
if setForeground {
return func(s *Segment) { s.Foreground = transformerName }
} else {
return func(s *Segment) { s.Background = transformerName }
}
}
return nil
}
func buildBoolTransformer(transformerName string, val, toggle bool) func(*Segment) {
switch transformerName {
case "bold":
if toggle {
return func(s *Segment) { s.Bold = !s.Bold }
}
return func(s *Segment) { s.Bold = val }
case "dim":
if toggle {
return func(s *Segment) { s.Dim = !s.Dim }
}
return func(s *Segment) { s.Dim = val }
case "italic":
if toggle {
return func(s *Segment) { s.Italic = !s.Italic }
}
return func(s *Segment) { s.Italic = val }
case "underlined":
if toggle {
return func(s *Segment) { s.Underlined = !s.Underlined }
}
return func(s *Segment) { s.Underlined = val }
case "blink":
if toggle {
return func(s *Segment) { s.Blink = !s.Blink }
}
return func(s *Segment) { s.Blink = val }
case "inverse":
if toggle {
return func(s *Segment) { s.Inverse = !s.Inverse }
}
return func(s *Segment) { s.Inverse = val }
}
return nil
}