2018-02-17 16:12:41 -02:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2023-09-12 16:42:54 -05:00
|
|
|
"bufio"
|
2018-02-17 16:12:41 -02:00
|
|
|
"io"
|
2022-06-27 21:47:56 -04:00
|
|
|
"os"
|
2023-08-29 21:04:01 +00:00
|
|
|
"slices"
|
2022-06-27 21:47:56 -04:00
|
|
|
"strconv"
|
2023-09-12 16:42:54 -05:00
|
|
|
"strings"
|
2019-05-26 18:36:39 -03:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2023-10-07 16:55:43 -05:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/errors"
|
|
|
|
"github.com/go-task/task/v3/internal/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrPromptCancelled = errors.New("prompt cancelled")
|
|
|
|
ErrNoTerminal = errors.New("no terminal")
|
2019-05-26 18:36:39 -03:00
|
|
|
)
|
|
|
|
|
2023-03-31 19:13:29 +00:00
|
|
|
type (
|
|
|
|
Color func() PrintFunc
|
|
|
|
PrintFunc func(io.Writer, string, ...any)
|
|
|
|
)
|
2019-05-26 18:36:39 -03:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Default() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_RESET", color.Reset)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Blue() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Green() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Cyan() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Yellow() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Magenta() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2023-03-31 19:13:29 +00:00
|
|
|
|
2022-06-27 21:47:56 -04:00
|
|
|
func Red() PrintFunc {
|
2024-04-09 01:49:44 +01:00
|
|
|
return color.New(envColor("TASK_COLOR_RED", color.FgRed)...).FprintfFunc()
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
|
|
|
|
2024-06-03 11:37:24 +02:00
|
|
|
func BrightBlue() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_BLUE", color.FgHiBlue)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrightGreen() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_GREEN", color.FgHiGreen)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrightCyan() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_CYAN", color.FgHiCyan)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrightYellow() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_YELLOW", color.FgHiYellow)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrightMagenta() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_MAGENTA", color.FgHiMagenta)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrightRed() PrintFunc {
|
|
|
|
return color.New(envColor("TASK_COLOR_BRIGHT_RED", color.FgHiRed)...).FprintfFunc()
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:49:44 +01:00
|
|
|
func envColor(env string, defaultColor color.Attribute) []color.Attribute {
|
2023-02-17 03:12:44 +03:00
|
|
|
if os.Getenv("FORCE_COLOR") != "" {
|
|
|
|
color.NoColor = false
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:49:44 +01:00
|
|
|
// Fetch the environment variable
|
|
|
|
override := os.Getenv(env)
|
|
|
|
|
|
|
|
// First, try splitting the string by commas (RGB shortcut syntax) and if it
|
|
|
|
// matches, then prepend the 256-color foreground escape sequence.
|
|
|
|
// Otherwise, split by semicolons (ANSI color codes) and use them as is.
|
|
|
|
attributeStrs := strings.Split(override, ",")
|
|
|
|
if len(attributeStrs) == 3 {
|
|
|
|
attributeStrs = append([]string{"38", "2"}, attributeStrs...)
|
|
|
|
} else {
|
|
|
|
attributeStrs = strings.Split(override, ";")
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2024-04-09 01:49:44 +01:00
|
|
|
|
|
|
|
// Loop over the attributes and convert them to integers
|
|
|
|
attributes := make([]color.Attribute, len(attributeStrs))
|
|
|
|
for i, attributeStr := range attributeStrs {
|
|
|
|
attribute, err := strconv.Atoi(attributeStr)
|
|
|
|
if err != nil {
|
|
|
|
return []color.Attribute{defaultColor}
|
|
|
|
}
|
|
|
|
attributes[i] = color.Attribute(attribute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes
|
2022-06-27 21:47:56 -04:00
|
|
|
}
|
2018-02-17 16:12:41 -02:00
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
|
|
|
|
// with optional color.
|
2018-02-17 16:12:41 -02:00
|
|
|
type Logger struct {
|
2023-10-07 16:55:43 -05:00
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
Verbose bool
|
|
|
|
Color bool
|
|
|
|
AssumeYes bool
|
|
|
|
AssumeTerm bool // Used for testing
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Outf prints stuff to STDOUT.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) Outf(color Color, s string, args ...any) {
|
2023-04-27 01:20:06 +01:00
|
|
|
l.FOutf(l.Stdout, color, s, args...)
|
2022-09-29 17:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FOutf prints stuff to the given writer.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) FOutf(w io.Writer, color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if len(args) == 0 {
|
2023-03-30 20:03:59 +00:00
|
|
|
s, args = "%s", []any{s}
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2019-07-07 14:13:53 -03:00
|
|
|
if !l.Color {
|
2021-05-30 22:48:48 -03:00
|
|
|
color = Default
|
2019-07-07 14:13:53 -03:00
|
|
|
}
|
2021-05-30 22:48:48 -03:00
|
|
|
print := color()
|
2022-09-29 17:13:12 +00:00
|
|
|
print(w, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) VerboseOutf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if l.Verbose {
|
2021-05-30 22:48:48 -03:00
|
|
|
l.Outf(color, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// Errf prints stuff to STDERR.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) Errf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if len(args) == 0 {
|
2023-03-30 20:03:59 +00:00
|
|
|
s, args = "%s", []any{s}
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
2019-07-07 14:13:53 -03:00
|
|
|
if !l.Color {
|
2021-05-30 22:48:48 -03:00
|
|
|
color = Default
|
2019-07-07 14:13:53 -03:00
|
|
|
}
|
2021-05-30 22:48:48 -03:00
|
|
|
print := color()
|
2023-04-27 01:20:06 +01:00
|
|
|
print(l.Stderr, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:36:39 -03:00
|
|
|
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
|
2023-03-30 20:03:59 +00:00
|
|
|
func (l *Logger) VerboseErrf(color Color, s string, args ...any) {
|
2018-02-17 16:12:41 -02:00
|
|
|
if l.Verbose {
|
2021-05-30 22:48:48 -03:00
|
|
|
l.Errf(color, s, args...)
|
2018-02-17 16:12:41 -02:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 16:42:54 -05:00
|
|
|
|
2024-04-24 21:40:52 +01:00
|
|
|
func (l *Logger) Warnf(message string, args ...any) {
|
|
|
|
l.Errf(Yellow, message, args...)
|
|
|
|
}
|
|
|
|
|
2023-10-07 16:55:43 -05:00
|
|
|
func (l *Logger) Prompt(color Color, prompt string, defaultValue string, continueValues ...string) error {
|
|
|
|
if l.AssumeYes {
|
|
|
|
l.Outf(color, "%s [assuming yes]\n", prompt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !l.AssumeTerm && !term.IsTerminal() {
|
|
|
|
return ErrNoTerminal
|
|
|
|
}
|
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
if len(continueValues) == 0 {
|
2023-10-07 16:55:43 -05:00
|
|
|
return errors.New("no continue values provided")
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
2023-10-07 16:55:43 -05:00
|
|
|
|
2024-05-13 13:38:32 +00:00
|
|
|
l.Outf(color, "%s [%s/%s]: ", prompt, strings.ToLower(continueValues[0]), strings.ToUpper(defaultValue))
|
2023-10-07 16:55:43 -05:00
|
|
|
|
|
|
|
reader := bufio.NewReader(l.Stdin)
|
2023-09-12 16:42:54 -05:00
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2023-10-07 16:55:43 -05:00
|
|
|
return err
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|
2023-10-07 16:55:43 -05:00
|
|
|
|
2023-09-12 16:42:54 -05:00
|
|
|
input = strings.TrimSpace(strings.ToLower(input))
|
2023-10-07 16:55:43 -05:00
|
|
|
if !slices.Contains(continueValues, input) {
|
|
|
|
return ErrPromptCancelled
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-09-12 16:42:54 -05:00
|
|
|
}
|