-
Notifications
You must be signed in to change notification settings - Fork 20
/
colors.go
272 lines (241 loc) · 6.25 KB
/
colors.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
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"path/filepath"
"sync"
"github.com/mgutz/ansi"
)
var (
// global ANSI terminal color profile
cp = &ansiProfile{}
// ErrUnknownColorProfile means the color profile does not exist
ErrUnknownColorProfile = errors.New("unknown color profile")
)
// ANSI Escape Sequence Representation of a ColorProfile
// contains a mutex to make changes on the fly possible
// without a data race
type ansiProfile struct {
Text string
Prompt string
CmdOutput string
CmdName string
CmdFields string
CmdArgs string
CmdArgType string
Reset string
sync.RWMutex
}
// ColorProfile for terminal colors
// it contains the colors following the ansi package style format
// https://github.com/mgutz/ansi
type ColorProfile struct {
Text string `yaml:"Text"`
Prompt string `yaml:"Prompt"`
CmdOutput string `yaml:"CmdOutput"`
CmdName string `yaml:"CmdName"`
CmdFields string `yaml:"CmdFields"`
CmdArgs string `yaml:"CmdArgs"`
CmdArgType string `yaml:"CmdArgType"`
}
func printColorsUsageErr() {
conf.Lock()
l.Println("current color profile: " + conf.fields.ColorProfile)
conf.Unlock()
l.Println("usage: colors [default | off" + getAvailableColorProfiles() + "]")
}
func getAvailableColorProfiles() (res string) {
conf.Lock()
defer conf.Unlock()
for name := range conf.fields.ColorProfiles {
res += " | " + name
}
return
}
// low contrast profile
func darkProfile() *ColorProfile {
return &ColorProfile{
Text: "cyan",
Prompt: "blue",
CmdOutput: "white",
CmdName: "blue",
CmdFields: "yellow",
CmdArgs: "white+h",
CmdArgType: "green",
}
}
// high contrast profile
func lightProfile() *ColorProfile {
return &ColorProfile{
Text: "white",
Prompt: "yellow",
CmdOutput: "white+h",
CmdName: "red",
CmdFields: "blue",
CmdArgs: "cyan",
CmdArgType: "green",
}
}
// default terminal color profile
func defaultProfile() *ColorProfile {
return &ColorProfile{
Text: "green",
Prompt: "red",
CmdOutput: "white+h",
CmdName: "red",
CmdFields: "yellow",
CmdArgs: "red",
CmdArgType: "cyan+h",
}
}
// black terminal color profile
func blackProfile() *ColorProfile {
return &ColorProfile{
Text: "black",
Prompt: "black",
CmdOutput: "black",
CmdName: "black",
CmdFields: "black",
CmdArgs: "black",
CmdArgType: "black",
}
}
// profile with disabled colors
func colorsOffProfile() *ColorProfile {
return &ColorProfile{
Text: ansi.ColorCode("off"),
Prompt: ansi.ColorCode("off"),
CmdOutput: ansi.ColorCode("off"),
CmdName: ansi.ColorCode("off"),
CmdFields: ansi.ColorCode("off"),
CmdArgs: ansi.ColorCode("off"),
CmdArgType: ansi.ColorCode("off"),
}
}
// handle colors shell command
func handleColorsCommand(args []string) {
if len(args) < 2 {
printColorsUsageErr()
return
}
var (
err error
profile = args[1]
)
// lock to prevent a race on the global ansiProfile instance
cp.Lock()
switch profile {
case "off":
cp = colorsOffProfile().parse()
case "default":
cp = defaultProfile().parse()
case "black":
cp = blackProfile().parse()
default:
// lookup profile name in config
conf.Lock()
if p, ok := conf.fields.ColorProfiles[profile]; ok {
conf.Unlock()
cp = p.parse()
} else {
// no change to colorProfile - Unlock it
cp.Unlock()
conf.Unlock()
Log.Error(ErrUnknownColorProfile)
return
}
}
Log.Info("color profile set to: ", profile)
// update value in config
conf.Lock()
conf.fields.ColorProfile = profile
if profile == "off" {
conf.fields.Colors = false
// load uncolored ascii art
asciiArt, err = assetBox.String("ascii_art.txt")
if err != nil {
Log.WithError(err).Fatal("failed to get ascii_art.txt from rice box")
}
} else {
conf.fields.Colors = true
// load colored ascii art
asciiArt, err = assetBox.String("ascii_art_color.txt")
if err != nil {
Log.WithError(err).Fatal("failed to get ascii_art_color.txt from rice box")
}
}
conf.Unlock()
blockWriteEvent()
// update config on disk
conf.update()
readlineMutex.Lock()
if rl != nil {
rl.SetPrompt(printPrompt())
readlineMutex.Unlock()
clearScreen()
l.Println(cp.Text + asciiArt + "v" + version)
conf.Lock()
if conf.fields.Debug {
l.Println(cp.Text + "Project Name: " + cp.Prompt + filepath.Base(workingDir) + cp.Text + "\n")
}
conf.Unlock()
printBuiltins()
printCommands()
}
}
// init the current color profile from config
func initColorProfile() {
conf.Lock()
defer conf.Unlock()
// look up current profile string from config
profile := conf.fields.ColorProfile
// lock to prevent a race on the global ansiProfile instance
cp.Lock()
switch profile {
case "off":
cp = colorsOffProfile().parse()
cp.Reset = ""
case "default":
cp = defaultProfile().parse()
case "black":
cp = blackProfile().parse()
default:
if p, ok := conf.fields.ColorProfiles[profile]; ok {
cp = p.parse()
} else {
// no change to colorProfile - unlock it
cp.Unlock()
Log.Error(ErrUnknownColorProfile, " : ", conf.fields.ColorProfile)
return
}
}
}
// convert a ColorProfile to an ansiProfile
func (cp *ColorProfile) parse() *ansiProfile {
return &ansiProfile{
Text: ansi.ColorCode(cp.Text),
Prompt: ansi.ColorCode(cp.Prompt),
CmdArgs: ansi.ColorCode(cp.CmdArgs),
CmdArgType: ansi.ColorCode(cp.CmdArgType),
CmdFields: ansi.ColorCode(cp.CmdFields),
CmdName: ansi.ColorCode(cp.CmdName),
CmdOutput: ansi.ColorCode(cp.CmdOutput),
Reset: ansi.Reset,
}
}