github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/cli/replacements.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "strings" 8 ) 9 10 var replacements = map[string]string{ 11 "BOLD": "\x1b[1m", 12 "BOLD_GREY": "\x1b[30;1m", 13 "BOLD_RED": "\x1b[31;1m", 14 "BOLD_GREEN": "\x1b[32;1m", 15 "BOLD_YELLOW": "\x1b[33;1m", 16 "BOLD_BLUE": "\x1b[34;1m", 17 "BOLD_MAGENTA": "\x1b[35;1m", 18 "BOLD_CYAN": "\x1b[36;1m", 19 "BOLD_WHITE": "\x1b[37;1m", 20 "GREY": "\x1b[30m", 21 "RED": "\x1b[31m", 22 "GREEN": "\x1b[32m", 23 "YELLOW": "\x1b[33m", 24 "BLUE": "\x1b[34m", 25 "MAGENTA": "\x1b[35m", 26 "CYAN": "\x1b[36m", 27 "WHITE": "\x1b[37m", 28 "RESET": "\x1b[0m", 29 "RESETLN": "\x1b[1G\x1b[2K", // Resets back to start of line and clears it. 30 } 31 32 // Printf is a convenience wrapper to Fprintf that always writes to stderr. 33 func Printf(msg string, args ...interface{}) { 34 Fprintf(os.Stderr, msg, args...) 35 } 36 37 // Fprintf implements essentially fmt.Fprintf with replacements of 38 // some ANSI sequences, e.g. ${BOLD_RED} -> \x1bwhatever. 39 func Fprintf(w io.Writer, msg string, args ...interface{}) { 40 for k, v := range replacements { 41 if !StdErrIsATerminal || !StdOutIsATerminal { 42 v = "" 43 } 44 msg = strings.Replace(msg, fmt.Sprintf("${%s}", k), v, -1) 45 } 46 fmt.Fprintf(w, msg, args...) 47 }