-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorize.go
164 lines (144 loc) · 4.16 KB
/
colorize.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
// Copyright 2019 Soluble Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
colorize does ANSI colored output and emoji substitution.
Colorize takes a fmt.Printf() template string and looks for styles
in the format:
{style-name:text}
And inserts the color esacpe sequences around the text. It then passes
the template off to emoji.Sprintf() from https://github.com/kyokomi/emoji.
It uses the color attribute names, color.Output, and color.NoColor from
https://github.com/fatih/color.
The style names are modeled off the basic bootstrap color names.
Example use:
colorize.Colorize("{primary:We} want {bg-success:beer :beer:} and {warning:pizza} :pizza:")
*/
package colorize
import (
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/kyokomi/emoji"
)
const escape = "\x1b"
// Style is a ANSI escape code sequence
type Style struct {
sequence string
enable *bool
}
// NewStyle creates a style from a set of attributes
func NewStyle(attributes ...color.Attribute) *Style {
codes := make([]string, len(attributes))
for i, attr := range attributes {
codes[i] = strconv.Itoa(int(attr))
}
sequence := fmt.Sprintf("%s[%sm", escape, strings.Join(codes, ";"))
return &Style{sequence, nil}
}
// Styles maps style names to styles
var Styles = map[string]*Style{
"primary": NewStyle(color.FgCyan, color.Bold),
"bg-primary": NewStyle(color.FgHiWhite, color.BgCyan),
"secondary": NewStyle(color.FgHiBlack, color.Bold),
"bg-secondary": NewStyle(color.FgHiWhite, color.BgHiBlack),
"light": NewStyle(color.FgHiBlack),
"info": NewStyle(color.FgBlue, color.Bold),
"bg-info": NewStyle(color.FgWhite, color.BgBlue),
"success": NewStyle(color.FgGreen),
"bg-success": NewStyle(color.FgHiWhite, color.BgGreen),
"warning": NewStyle(color.FgYellow),
"bg-warning": NewStyle(color.FgBlack, color.BgYellow),
"danger": NewStyle(color.FgHiRed),
"bg-danger": NewStyle(color.FgBlack, color.BgHiRed),
}
var reset = NewStyle(color.Reset)
// IsEnabled returns true if the style is enabled, either specifically
// to this style or globally via color.NoColor
func (style *Style) IsEnabled() bool {
if style == nil {
return false
}
if style.enable != nil {
return *style.enable
}
return !color.NoColor
}
// Enable or disable this style
func (style *Style) Enable(val bool) *Style {
style.enable = &val
return style
}
// Colorize some text, printing the output to color.Output
func Colorize(template string, values ...interface{}) {
fmt.Fprint(color.Output, SColorize(template, values...))
}
// Colorize some text, returning a string
func SColorize(template string, values ...interface{}) string {
var b strings.Builder
state := ' '
var nameStart int
var style *Style
var frag strings.Builder
for i, ch := range template {
switch state {
case ' ':
if ch == '{' {
nameStart = i
state = ch
} else {
b.WriteRune(ch)
if ch == '\\' {
state = ch
}
}
case '\\':
b.WriteRune(ch)
state = ' '
case '{':
if ch == ':' {
name := template[nameStart+1 : i]
style = Styles[name]
frag.Reset()
state = '}'
}
case '}':
if ch == '\\' {
state = ']'
} else if ch == '}' {
var enabled = style.IsEnabled()
if enabled {
b.WriteString(style.sequence)
}
b.WriteString(frag.String())
if enabled {
b.WriteString(reset.sequence)
}
state = ' '
} else {
frag.WriteRune(ch)
}
case ']':
frag.WriteRune(ch)
state = '}'
}
}
switch state {
case '{', '}', ']':
// if we found a '{' w/o completion, append everything from the start
b.WriteString(template[nameStart:])
}
template = b.String()
return emoji.Sprintf(template, values...)
}