-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtint.go
More file actions
329 lines (280 loc) · 7.89 KB
/
tint.go
File metadata and controls
329 lines (280 loc) · 7.89 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2019 Ashish Shekar a.k.a codekidX
*
* 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.
*/
// This document demonstrates the working of tint: a minimal bare-bone version of terminal styling implemented for
// Go applications with no external dependencies.
// It provides you with different types of functions that you can use to style your terminal output with ease.
//
// Tint was originally created to use in the 'orbpkg' project: https://github.com/orbpkg/orb and uses near to 0ms
// for processing color expressions: https://godoc.org/github.com/printzero/tint/#Tint.Exp. Although the time taken to process is directly propotional to the number
// of characters in the string input.
package tint
import (
"fmt"
"log"
)
// Tint struct holds the whole library
type Tint struct {
Level TerminalLevel
SupportsColor bool
LogInstance *log.Logger
}
// TerminalLevel of color support for terminal and information of
// current terminal that is useful to tint.
type TerminalLevel int
// Color is a struct that holds the innate escape characters for color variables.
type color struct {
open string // opening escape character for a given color
close string // closing escape character for a given color
}
func (c color) Dim() color {
return color{
open: "\u001b[2m" + c.open,
close: c.close + "\u001b[0m",
}
}
func (c color) Bold() color {
return color{
open: "\u001b[1m" + c.open,
close: c.close + "\u001b[0m",
}
}
func (c color) Underline() color {
return color{
open: "\u001b[4m" + c.open,
close: c.close + "\u001b[0m",
}
}
func (c color) Italic() color {
return color{
open: "\u001b[3m" + c.open,
close: c.close + "\u001b[0m",
}
}
func (c color) Strike() color {
return color{
open: "\u001b[9m" + c.open,
close: c.close + "\u001b[0m",
}
}
func (c color) Add(this color) color {
return color{
open: c.open + this.open,
close: this.close + c.close,
}
}
// Normal equates to no style
var Normal = color{
open: "\u001b[0m",
close: "\u001b[0m",
}
// Black color
var Black = color{
open: "\u001b[30m",
close: "\u001b[39m",
}
// Red color
var Red = color{
open: "\u001b[31m",
close: "\u001b[39m",
}
// Green color
var Green = color{
open: "\u001b[32m",
close: "\u001b[39m",
}
// Yellow color
var Yellow = color{
open: "\u001b[33m",
close: "\u001b[39m",
}
// Blue color
var Blue = color{
open: "\u001b[34m",
close: "\u001b[39m",
}
// Magenta color
var Magenta = color{
open: "\u001b[35m",
close: "\u001b[39m",
}
// Cyan color
var Cyan = color{
open: "\u001b[36m",
close: "\u001b[39m",
}
// White color
var White = color{
open: "\u001b[37m",
close: "\u001b[39m",
}
// BgBlack applies Black Background color
var BgBlack = color{
open: "\u001b[40m",
close: "\u001b[49m",
}
// BgRed applies Red Background color
var BgRed = color{
open: "\u001b[41m",
close: "\u001b[49m",
}
// BgGreen applies Green Background color
var BgGreen = color{
open: "\u001b[42m",
close: "\u001b[49m",
}
// BgYellow applies Yellow Background color
var BgYellow = color{
open: "\u001b[43m",
close: "\u001b[49m",
}
// BgBlue applies Blue Background color
var BgBlue = color{
open: "\u001b[44m",
close: "\u001b[49m",
}
// BgMagenta applies Magenta Background color
var BgMagenta = color{
open: "\u001b[45m",
close: "\u001b[49m",
}
// BgCyan applies Cyan Background color
var BgCyan = color{
open: "\u001b[46m",
close: "\u001b[49m",
}
// BgLightGrey applies Light Grey Background color
var BgLightGrey = color{
open: "\u001b[47m",
close: "\u001b[49m",
}
// BgWhite applies White Background color
var BgWhite = color{
open: "\u001b[107m",
close: "\u001b[49m",
}
// Hyperlink text
//Hyperlink
const (
// LevelNone for terminal that supports no colot
LevelNone TerminalLevel = iota + 1
// Level16bit for terminal that supports 16bit colors
Level16bit
// Level256 for terminal that supports 256 bit colors
Level256
// Level16m for terminal that supports 16 million colors (truecolor)
Level16m
)
// Init initializes variables that tint uses and then returns the
// pointer to a Tint struct
func Init() *Tint {
return &Tint{
Level: LevelNone,
SupportsColor: false,
LogInstance: &log.Logger{},
}
}
// Exp returns a string constructed from a series of color expressions given as an argument.
// The colors are passed as a replacement to each word that is wrapped around `@()`.
//
// The string "@(Hello), @(World)!" where 'Hello' is inside a tint color expression and 'World' inside another,
// 'Hello' will get replaced by the first color and 'World' will get replaced by the second color passed inside
// this function.
//
// Take a look at the below example.
func (t *Tint) Exp(expStr string, colors ...color) string {
return replaceExp(expStr, colors)
}
// Raw returns the raw string with applied colors
func (t *Tint) Raw(text string, colors ...color) string {
return apply(text, colors)
}
// Print single line of text with specified color
func (t *Tint) Print(text string, colors ...color) {
fmt.Print(apply(text, colors))
}
// Println single line of text with enter character
func (t *Tint) Println(text string, colors ...color) {
fmt.Println(apply(text, colors))
}
// Log text with the standard lib log module
func (t *Tint) Log(text string, colors ...color) {
t.LogInstance.Println(apply(text, colors))
}
// Swatch will return a function for specific colors given as a parameter.
func (t *Tint) Swatch(colors ...color) func(text string) {
return func(text string) {
t.Println(text, colors...)
}
}
// SwatchRaw returns a functions that returns a raw colored string
func (t *Tint) SwatchRaw(colors ...color) func(text string) string {
return func(text string) string {
return t.Raw(text, colors...)
}
}
// apply the colors to the text by wrapping text with ANSI styling
func apply(text string, colors []color) string {
output := text
for _, c := range colors {
output = c.open + output + c.close
}
return output
}
func replaceExp(text string, colors []color) string {
if tc, proceed := isAtCountSame(text, len(colors)); !proceed {
//tintedError := replaceExp(, []color{Magenta.Bold()})
panic(fmt.Errorf("mismatching apply - Trigger count: %d, Color count: %d \n\n%sTip%s: Does your function have colors passed as much as it has '@' character?",
tc, len(colors), Magenta.open, Magenta.close))
return text
}
workingString := ""
workingColors := colors
// this is used as the current color on operation
//var colorStart int
//var colorEnd = -1
var colorBox = Normal
var triggered = false
// so here is what we'll do --- we'll find the first @( which serves as opening
// of a color expression, and then look for the next close with colorBox in memory
for i, c := range text {
if string(c) == "@" && string(text[i+1]) == "(" {
triggered = true
workingString = workingString + ""
continue
} else if triggered && string(c) == "(" {
colorBox = workingColors[0]
workingString = workingString + colorBox.open
continue
} else if triggered && string(c) == ")" {
triggered = false
workingString = workingString + colorBox.close
colorBox = Normal
workingColors = workingColors[1:]
continue
}
workingString = workingString + colorBox.open + string(c) + colorBox.close
}
return workingString
}
func isAtCountSame(text string, colorCount int) (int, bool) {
counter := 0
for i, c := range text {
if string(c) == "@" && string(text[i+1]) == "(" {
counter += 1
}
}
return counter, counter == colorCount
}