func installToolchains(langs []toolchainInstaller) error { var notInstalled []string for _, l := range langs { fmt.Println(brush.Cyan(l.name + " " + strings.Repeat("=", 78-len(l.name))).String()) if err := l.fn(); err != nil { if err2, ok := err.(skippedToolchain); ok { fmt.Printf("%s\n", brush.Yellow(err2.Error())) } else { fmt.Printf("%s\n", brush.Red(fmt.Sprintf("failed to install/upgrade %s toolchain: %s", l.name, err))) } notInstalled = append(notInstalled, l.name) // Continue here because we attempt to install // all the toolchains. continue } fmt.Println(brush.Green("OK! Installed/upgraded " + l.name + " toolchain").String()) fmt.Println(brush.Cyan(strings.Repeat("=", 80)).String()) fmt.Println() } if len(notInstalled) != 0 { return errors.New(brush.Red(fmt.Sprintf("The following toolchains were not installed:\n%s", strings.Join(notInstalled, "\n"))).String()) } return nil }
func main() { // Default Brush are available for your convenience. You can invoke // them directly fmt.Printf("This is %s\n", brush.Red("red")) // or you can create new ones! weird := color.NewBrush(color.PurplePaint, color.CyanPaint) fmt.Printf("This color is %s\n", weird("weird")) // Create a Style, which has convenience methods redBg := color.NewStyle(color.RedPaint, color.YellowPaint) // Style.WithForeground or WithBackground returns a new Style, with the applied // Paint. Styles are immutable so the original one is left unchanged greenFg := redBg.WithForeground(color.GreenPaint) // Style.Brush gives you a Brush that you can invoke directly to colorize strings. green := greenFg.Brush() fmt.Printf("This is %s but not really\n", green("kind of green")) // You can use it with all sorts of things sout := log.New(os.Stdout, "["+brush.Green("OK").String()+"]\t", log.LstdFlags) serr := log.New(os.Stderr, "["+brush.Red("OMG").String()+"]\t", log.LstdFlags) sout.Printf("Everything was going %s until...", brush.Cyan("fine")) serr.Printf("%s killed %s !!!", brush.Red("Locke"), brush.Blue("Jacob")) }
func Error(hl, msg string) { if PureMode { errorP(hl, msg) } if len(hl) > 0 { hl = " " + brush.Red(hl).String() } fmt.Printf("gopm %s%s %s\n", brush.Red("ERR!"), hl, msg) }
func (c *ToolchainInstallCmd) Execute(args []string) error { if len(c.Args.Languages) == 0 { return errors.New(brush.Red(fmt.Sprintf("No languages specified. Standard languages include: %s", stdToolchains.listKeys())).String()) } var is []toolchainInstaller for _, l := range c.Args.Languages { i, ok := stdToolchains[l] if !ok { return errors.New(brush.Red(fmt.Sprintf("Language %s unrecognized. Standard languages include: %s", l, stdToolchains.listKeys())).String()) } is = append(is, i) } return installToolchains(is) }
func DisplayComments(comments []gh.Comment) { fmt.Fprintln(os.Stdout, "Comments:") for _, c := range comments { fmt.Fprintf(os.Stdout, "<%s\n@%s %s\n%s\n%s>", strings.Repeat("=", 79), brush.Red(c.User.Login), c.CreatedAt.Format(defaultTimeFormat), strings.Replace(c.Body, "LGTM", fmt.Sprintf("%s", brush.Green("LGTM")), -1), strings.Repeat("=", 79)) fmt.Fprint(os.Stdout, "\n\n") } }
func checkResults(output bytes.Buffer, treeDir, actualDir, expectedDir string) error { treeName := filepath.Base(treeDir) out, err := exec.Command("diff", "-ur", expectedDir, actualDir).CombinedOutput() if err != nil || len(out) > 0 { fmt.Println(brush.Red(treeName + " FAIL").String()) fmt.Printf("Diff failed for %s: %s.", treeName, err) if len(out) > 0 { fmt.Println(brush.Red(treeName + " FAIL")) fmt.Println(output.String()) fmt.Println(string(ColorizeDiff(out))) } return fmt.Errorf("Output for %s differed from expected.", treeName) } else { fmt.Println(brush.Green(treeName + " PASS").String()) } return nil }
func (c *ToolchainTempDirCmd) Execute(args []string) error { dir, err := toolchain.TempDir(c.Args.ToolchainPath) if err != nil { return errors.New(brush.Red(fmt.Sprintf("Failed to get/create temp dir: %s", err)).String()) } fmt.Printf(dir) return nil }
// ColorizeDiff takes a byte slice of lines and returns the same, but with diff // highlighting. That is, lines starting with '+' are green and lines starting // with '-' are red. func ColorizeDiff(diff []byte) []byte { lines := bytes.Split(diff, []byte{'\n'}) for i, line := range lines { if bytes.HasPrefix(line, []byte{'-'}) { lines[i] = []byte(brush.Red(string(line)).String()) } if bytes.HasPrefix(line, []byte{'+'}) { lines[i] = []byte(brush.Green(string(line)).String()) } } return bytes.Join(lines, []byte{'\n'}) }
func DisplayPatch(r io.Reader) error { s := bufio.NewScanner(r) for s.Scan() { if err := s.Err(); err != nil { return err } t := s.Text() switch t[0] { case '-': fmt.Fprintln(os.Stdout, brush.Red(t)) case '+': fmt.Fprintln(os.Stdout, brush.Green(t)) default: fmt.Fprintln(os.Stdout, t) } } return nil }
func makeLogMux(conf *Config) (*logMux, error) { fileVerb, err := getVerbosityLevel(conf.LogFileVerbosity) if err != nil { return nil, fmt.Errorf("log file verbosity, %v", err) } consoleVerb, err := getVerbosityLevel(conf.ConsoleVerbosity) if err != nil { return nil, fmt.Errorf("console verbosity, %v", err) } filename := conf.LogFilename file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0640) if os.IsNotExist(err) { file, err = os.Create(filename) } if err != nil { return nil, fmt.Errorf("opening log file %s, %v", filename, err) } debugPfx := fmt.Sprintf("%s%s%s ", brush.DarkGray("["), brush.Blue("DEBUG"), brush.DarkGray("]")) watchPfx := fmt.Sprintf("%s%s%s ", brush.DarkGray("["), brush.DarkCyan("WATCH"), brush.DarkGray("]")) infoPfx := fmt.Sprintf(" %s%s%s ", brush.DarkGray("["), brush.Green("INFO"), brush.DarkGray("]")) warnPfx := fmt.Sprintf(" %s%s%s ", brush.DarkGray("["), brush.Yellow("WARN"), brush.DarkGray("]")) errPfx := fmt.Sprintf("%s%s%s ", brush.DarkGray("["), brush.Red("ERROR"), brush.DarkGray("]")) return &logMux{ logFile: file, debugFile: log.New(file, "[DEBUG] ", log.LstdFlags), watchFile: log.New(file, "[WATCH] ", log.LstdFlags), infoFile: log.New(file, " [INFO] ", log.LstdFlags), warnFile: log.New(file, " [WARN] ", log.LstdFlags), errorFile: log.New(file, "[ERROR] ", log.LstdFlags), debugConsole: log.New(os.Stdout, debugPfx, log.LstdFlags), watchConsole: log.New(os.Stdout, watchPfx, log.LstdFlags), infoConsole: log.New(os.Stdout, infoPfx, log.LstdFlags), warnConsole: log.New(os.Stdout, warnPfx, log.LstdFlags), errorConsole: log.New(os.Stderr, errPfx, log.LstdFlags), fileVerbose: fileVerb, consoleVerbose: consoleVerb, }, nil }
var stdout_std_logger *log.Logger var stderr_std_logger *log.Logger var prefixes = map[int]string{ DEBUG: "[debug] ", INFO: "[info] ", WARNING: "[warning] ", ERROR: "[error] ", FATAL: "[fatal] ", } var coloredPrefixes = map[int]string{ DEBUG: brush.Cyan("[debug] ").String(), INFO: brush.Green("[info] ").String(), WARNING: brush.Yellow("[warning] ").String(), ERROR: brush.Red("[error] ").String(), FATAL: brush.DarkRed("[fatal] ").String(), } type Stdout struct { MinLevel int Colored bool } func init() { stdout_std_logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) stderr_std_logger = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) } func (s Stdout) std_format_message(level int, message string) (int, string) { if s.Colored {
brog create [name] Creates a blank post in file [name], in the location specified by the config file. brog page [name] Creates a blank page in file [name], in the location specified by the config file. brog help Shows this message. brog version Prints the current version of brog. ` ) var ( errPfx = fmt.Sprintf("%s%s%s ", brush.DarkGray("["), brush.Red("ERROR"), brush.DarkGray("]")) ) func main() { commands := os.Args[1:] for i, arg := range commands { switch arg { case Init: doInit() return case Server: if len(commands) > i+1 { doServer(commands[i+1] == "prod") } else { doServer(false)
func Red(s string) string { if Colorize { return brush.Red(s).String() } return s }