author | Mikael Berthe <mikael@lilotux.net> |
Mon, 01 May 2017 12:25:43 +0200 | |
changeset 49 | d6b4e3b7c6c6 |
permissions | -rw-r--r-- |
49
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
2 |
// |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
// Licensed under the MIT license. |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 |
// Please see the LICENSE file is this directory. |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
package colors |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
import ( |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
"strconv" |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
"strings" |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
"github.com/pkg/errors" |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
// Style values |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
const ( |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
Bold = iota + 1 |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
Faint |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
Italic |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
Underline |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
BlinkSlow |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
BlinkFast |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
Inverse |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
Hide |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
CrossedOut |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
// Styles contains style names |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
var Styles = [...]string{ |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
"bold", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
"faint", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
"italic", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
"underline", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
"blink-slow", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
"blink-fast", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
"inverse", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
"hide", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
"crossed-out", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
// Color values |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
const ( |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
Black = iota |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
Red |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
Green |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
Yellow |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
Blue |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
Magenta |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
Cyan |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
White |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
// Colors contains color names |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
var Colors = [...]string{ |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 |
"black", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
"red", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
"green", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
"yellow", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
"blue", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
"magenta", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
"cyan", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
"white", |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
// ANSICode returns an ANSI escape sequence string for the requested output |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
func ANSICode(fg, bg, style int) string { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
var seq []string |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
if fg >= 0 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
seq = append(seq, strconv.Itoa(30+fg)) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
if bg >= 0 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
seq = append(seq, strconv.Itoa(40+bg)) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
if style >= 0 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
seq = append(seq, strconv.Itoa(1+style)) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
if len(seq) == 0 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
return "\x1b[" + "0" + "m" // Reset sequence |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 |
return "\x1b[" + strings.Join(seq, ";") + "m" |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
// ANSICodeString returns an ANSI escape sequence string for the requested |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 |
// output. |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
// The description is a coma-separated list of foreground color, background |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
// color and style: [fg],[bg],[style] |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
// An empty description or "reset" returns the ANSI reset sequence. |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
func ANSICodeString(desc string) (string, error) { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
col := [2]int{-1, -1} // fg, bg |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
style := -1 |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
if desc == "" || desc == "reset" { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
return ANSICode(col[0], col[1], style), nil |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 |
styles := strings.SplitN(desc, ",", 3) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
for i, s := range styles { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
if s == "" { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
continue |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
switch { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 |
case i < 2: // Color |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
for n, c := range Colors { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
if c == s { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
col[i] = n |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
break |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
if col[i] == -1 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
return ANSICode(-1, -1, -1), |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
errors.Errorf("color name '%s' not found", s) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
case i == 2: // Style |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 |
for n, sn := range Styles { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 |
if sn == s { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
style = n |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
break |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
if style == -1 { |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
return ANSICode(-1, -1, -1), |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 |
errors.Errorf("style name '%s' not found", s) |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 |
} |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 |
|
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
return ANSICode(col[0], col[1], style), nil |
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
} |