-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.go
137 lines (117 loc) · 3.47 KB
/
color.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
)
//
// Some code copied from here:
// https://github.com/Marak/colors.js/blob/master/lib/styles.js
//
/*
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
type CodePair struct {
Open int
Close int
}
var (
CpBold = CodePair{1, 22}
CpDim = CodePair{2, 22}
CpItalic = CodePair{3, 23}
CpUnderline = CodePair{4, 24}
CpStrikethrough = CodePair{9, 29}
)
var codes = map[string]CodePair{
"reset": {0, 0},
"bold": CpBold,
"dim": CpDim,
"italic": CpItalic,
"underline": CpUnderline,
"inverse": {7, 27},
"hidden": {8, 28},
"strikethrough": CpStrikethrough,
"black": {30, 39},
"red": {31, 39},
"green": {32, 39},
"yellow": {33, 39},
"blue": {34, 39},
"magenta": {35, 39},
"cyan": {36, 39},
"white": {37, 39},
"gray": {90, 39},
"grey": {90, 39},
"bgBlack": {40, 49},
"bgRed": {41, 49},
"bgGreen": {42, 49},
"bgYellow": {43, 49},
"bgBlue": {44, 49},
"bgMagenta": {45, 49},
"bgCyan": {46, 49},
"bgWhite": {47, 49},
}
const keyEscape = 0x1b
func colorByteSequence(code int) []byte {
b := []byte(fmt.Sprintf("%d", code))
ret := []byte{keyEscape, '['}
ret = append(ret, b...)
ret = append(ret, 'm')
return ret
}
func (cp CodePair) OpenBytes() []byte { return colorByteSequence(cp.Open) }
func (cp CodePair) CloseBytes() []byte { return colorByteSequence(cp.Close) }
func ColorOpen(which string) []byte {
if !HasColor() {
return nil
}
if cp, ok := codes[which]; ok {
return colorByteSequence(cp.Open)
}
return nil
}
func GetColorCode(which string) *CodePair {
if tmp, ok := codes[which]; ok {
return &tmp
}
return nil
}
func ColorClose(which string) []byte {
if !HasColor() {
return nil
}
if cp, ok := codes[which]; ok {
return colorByteSequence(cp.Close)
}
return nil
}
func ColorBytes(which string, text []byte) []byte {
if !HasColor() {
return text
} else if cp, ok := codes[which]; ok {
ret := colorByteSequence(cp.Open)
ret = append(ret, text...)
ret = append(ret, colorByteSequence(cp.Close)...)
return ret
} else {
return text
}
}
func ColorString(which, text string) string {
return string(ColorBytes(which, []byte(text)))
}