forked from tektoncd/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
176 lines (154 loc) · 3.89 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
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
// Copyright © 2019 The Tekton Authors.
//
// 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.
package formatted
import (
"fmt"
"io"
"sync"
"sync/atomic"
"github.com/fatih/color"
)
var (
// Palette of colors for rainbow's tasks, Red is avoided as keeping it for errors
palette = []color.Attribute{
color.FgHiGreen,
color.FgHiYellow,
color.FgHiBlue,
color.FgHiMagenta,
color.FgHiCyan,
}
)
// DecorateAttr decorate strings with a color or an emoji, respecting the user
// preference if no colour needed.
func DecorateAttr(attrString, message string) string {
if color.NoColor {
return message
}
switch attrString {
case "bullet":
return fmt.Sprintf("∙ %s", message)
case "check":
return "✔ ️"
case "resources":
return "📦 "
case "params":
return "⚓ "
case "tasks":
return "🗒 "
case "pipelineruns":
return "⛩ "
case "status":
return "🌡️ "
case "inputresources":
return "📨 "
case "outputresources":
return "📡 "
case "steps":
return "🦶 "
case "message":
return "💌 "
case "taskruns":
return "🗂 "
case "sidecars":
return "🚗 "
case "results":
return "📝 "
case "workspaces":
return "📂 "
case "skippedtasks":
return "⏭️ "
}
attr := color.Reset
switch attrString {
case "underline":
attr = color.Underline
case "underline bold":
return color.New(color.Underline).Add(color.Bold).Sprintf(message)
case "bold":
attr = color.Bold
case "yellow":
attr = color.FgHiYellow
case "green":
attr = color.FgHiGreen
case "red":
attr = color.FgHiRed
case "blue":
attr = color.FgHiBlue
case "magenta":
attr = color.FgHiMagenta
case "cyan":
attr = color.FgHiCyan
case "black":
attr = color.FgHiBlack
case "white":
attr = color.FgHiWhite
}
return color.New(attr).Sprintf(message)
}
type atomicCounter struct {
value uint32
threshold int
}
func (c *atomicCounter) next() int {
v := atomic.AddUint32(&c.value, 1)
next := int(v-1) % c.threshold
atomic.CompareAndSwapUint32(&c.value, uint32(c.threshold), 0)
return next
}
type rainbow struct {
cache sync.Map
counter atomicCounter
}
func newRainbow() *rainbow {
return &rainbow{
counter: atomicCounter{threshold: len(palette)},
}
}
func (r *rainbow) get(x string) color.Attribute {
if value, ok := r.cache.Load(x); ok {
return value.(color.Attribute)
}
clr := palette[r.counter.next()]
r.cache.Store(x, clr)
return clr
}
// Fprintf formats according to a format specifier and writes to w.
// the first argument is a label to keep the same colour on.
func (r *rainbow) Fprintf(label string, w io.Writer, format string, args ...interface{}) {
attribute := r.get(label)
crainbow := color.Set(attribute).Add(color.Bold)
crainbow.Fprintf(w, format, args...)
}
// Color formatter to print the colored output on streams
type Color struct {
Rainbow *rainbow
red *color.Color
blue *color.Color
}
// NewColor returns a new instance color formatter
func NewColor() *Color {
return &Color{
Rainbow: newRainbow(),
red: color.New(color.FgRed),
blue: color.New(color.FgBlue),
}
}
// PrintRed prints the formatted content to given destination in red color
func (c *Color) PrintRed(w io.Writer, format string, args ...interface{}) {
c.red.Fprintf(w, format, args...)
}
// Error prints the formatted content to given destination in red color
func (c *Color) Error(w io.Writer, format string, args ...interface{}) {
c.PrintRed(w, format, args...)
}