-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ui.go
76 lines (61 loc) · 1.96 KB
/
ui.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
package ui
import (
"fmt"
"io"
"math"
"os"
"regexp"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
const ESC = 27
const ansiEscapeStr = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var IsTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
var IsCI = os.Getenv("CI") == "true" || os.Getenv("BUILD_NUMBER") == "true" || os.Getenv("TEAMCITY_VERSION") != ""
var gray = color.New(color.Faint)
var bold = color.New(color.Bold)
var ERROR_PREFIX = color.New(color.Bold, color.FgRed, color.ReverseVideo).Sprint(" ERROR ")
var WARNING_PREFIX = color.New(color.Bold, color.FgYellow, color.ReverseVideo).Sprint(" WARNING ")
// clear the line and move the cursor up
var clear = fmt.Sprintf("%c[%dA%c[2K", ESC, 1, ESC)
func ClearLines(writer io.Writer, count int) {
_, _ = fmt.Fprint(writer, strings.Repeat(clear, count))
}
var ansiRegex = regexp.MustCompile(ansiEscapeStr)
func StripAnsi(str string) string {
if !IsTTY {
return ansiRegex.ReplaceAllString(str, "")
}
return str
}
// Dim prints out dimmed text
func Dim(str string) string {
return gray.Sprint(str)
}
func Bold(str string) string {
return bold.Sprint(str)
}
func Warn(str string) string {
return fmt.Sprintf("%s %s", WARNING_PREFIX, color.YellowString(str))
}
func rgb(i int) (int, int, int) {
var f = 0.275
return int(math.Sin(f*float64(i)+4*math.Pi/3)*127 + 128),
// int(math.Sin(f*float64(i)+2*math.Pi/3)*127 + 128),
int(45),
int(math.Sin(f*float64(i)+0)*127 + 128)
}
// Rainbow function returns a formated colorized string ready to print it to the shell/terminal
func Rainbow(text string) string {
if !IsTTY {
return text
}
var rainbowStr []string
for index, value := range text {
r, g, b := rgb(index)
str := fmt.Sprintf("\033[1m\033[38;2;%d;%d;%dm%c\033[0m\033[0;1m", r, g, b, value)
rainbowStr = append(rainbowStr, str)
}
return strings.Join(rainbowStr, "")
}