aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/ui/ui.go
blob: 9084c76c5a1c999476c626ac3bc4e00e91beb1e3 (plain) (blame)
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
package ui

import (
	"fmt"
	"io"
	"math"
	"os"
	"regexp"
	"strings"

	"github.com/fatih/color"
	"github.com/mattn/go-isatty"
	"github.com/mitchellh/cli"
	"github.com/vercel/turbo/cli/internal/ci"
)

const ansiEscapeStr = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

// IsTTY is true when stdout appears to be a tty
var IsTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())

// IsCI is true when we appear to be running in a non-interactive context.
var IsCI = !IsTTY || ci.IsCi()
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 ")

// InfoPrefix is a colored string for warning level log messages
var InfoPrefix = color.New(color.Bold, color.FgWhite, color.ReverseVideo).Sprint(" INFO ")

var ansiRegex = regexp.MustCompile(ansiEscapeStr)

// Dim prints out dimmed text
func Dim(str string) string {
	return gray.Sprint(str)
}

func Bold(str string) string {
	return bold.Sprint(str)
}

// Adapted from go-rainbow
// Copyright (c) 2017 Raphael Amorim
// Source: https://github.com/raphamorim/go-rainbow
// SPDX-License-Identifier: MIT
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
//
// Adapted from go-rainbow
// Copyright (c) 2017 Raphael Amorim
// Source: https://github.com/raphamorim/go-rainbow
// SPDX-License-Identifier: MIT
func Rainbow(text string) string {
	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, "")
}

type stripAnsiWriter struct {
	wrappedWriter io.Writer
}

func (into *stripAnsiWriter) Write(p []byte) (int, error) {
	n, err := into.wrappedWriter.Write(ansiRegex.ReplaceAll(p, []byte{}))
	if err != nil {
		// The number of bytes returned here isn't directly related to the input bytes
		// if ansi color codes were being stripped out, but we are counting on Stdout.Write
		// not failing under typical operation as well.
		return n, err
	}

	// Write must return a non-nil error if it returns n < len(p). Consequently, if the
	// wrappedWrite.Write call succeeded we will return len(p) as the number of bytes
	// written.
	return len(p), nil
}

// Default returns the default colored ui
func Default() *cli.ColoredUi {
	return BuildColoredUi(ColorModeUndefined)
}

func BuildColoredUi(colorMode ColorMode) *cli.ColoredUi {
	colorMode = applyColorMode(colorMode)

	var outWriter, errWriter io.Writer

	if colorMode == ColorModeSuppressed {
		outWriter = &stripAnsiWriter{wrappedWriter: os.Stdout}
		errWriter = &stripAnsiWriter{wrappedWriter: os.Stderr}
	} else {
		outWriter = os.Stdout
		errWriter = os.Stderr
	}

	return &cli.ColoredUi{
		Ui: &cli.BasicUi{
			Reader:      os.Stdin,
			Writer:      outWriter,
			ErrorWriter: errWriter,
		},
		OutputColor: cli.UiColorNone,
		InfoColor:   cli.UiColorNone,
		WarnColor:   cli.UiColor{Code: int(color.FgYellow), Bold: false},
		ErrorColor:  cli.UiColorRed,
	}
}