Exemplo n.º 1
0
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"))

}
Exemplo n.º 2
0
Arquivo: klog.go Projeto: matm/gobuild
func (l *Logger) write(level Level, format string, a ...interface{}) (n int, err error) {
	if level < l.level {
		return
	}
	var levelName string = levels[int(level)]
	var sep = " "
	var prefix, outstr = l.prefix, ""

	if l.flags&Fdatetime != 0 {
		now := time.Now()
		layout := ""
		if l.flags&Fdate != 0 {
			layout += "2006/01/02"
		}
		if l.flags&Ftime != 0 {
			layout += " 15:04:05"
		}
		layout = strings.TrimSpace(layout)
		prefix += now.Format(layout)
	}

	if l.flags&Fshortfile != 0 {
		// Retrieve the stack infos
		_, file, line, ok := runtime.Caller(2)
		if !ok {
			file = "<unknown>"
			line = -1
		} else {
			file = file[strings.LastIndex(file, "/")+1:]
		}
		prefix = fmt.Sprintf("%s %s:%d", prefix, file, line)
	}

	outstr += levelName

	if format == "" {
		for _, i := range a {
			outstr += sep + fmt.Sprintf("%v", i)
		}
	} else {
		outstr = outstr + sep + fmt.Sprintf(format, a...)
	}
	if !strings.HasSuffix(outstr, "\n") {
		outstr += "\n"
	}

	if l.colorEnable && l.flags&Fcolor != 0 {
		brush := color.NewBrush("", colors[int(level)])
		outstr = brush(outstr)
	}

	mu.Lock()
	defer mu.Unlock()
	return l.writer.Write([]byte(prefix + sep + outstr))
}
Exemplo n.º 3
0
func (y Yellow) String() string {
	return color.NewBrush("", color.YellowPaint)(string(y))
}
Exemplo n.º 4
0
func (d DarkBlue) String() string {
	return color.NewBrush("", color.DarkBluePaint)(string(d))
}
Exemplo n.º 5
0
func (p Purple) String() string {
	return color.NewBrush("", color.PurplePaint)(string(p))
}
Exemplo n.º 6
0
func (r Red) String() string {
	return color.NewBrush("", color.RedPaint)(string(r))
}
Exemplo n.º 7
0
func (c Cyan) String() string {
	return color.NewBrush("", color.CyanPaint)(string(c))
}
Exemplo n.º 8
0
func (g Green) String() string {
	return color.NewBrush("", color.GreenPaint)(string(g))
}
Exemplo n.º 9
0
func (l LightGray) String() string {
	return color.NewBrush("", color.LightGrayPaint)(string(l))
}
Exemplo n.º 10
0
func (b Blue) String() string {
	return color.NewBrush("", color.BluePaint)(string(b))
}
Exemplo n.º 11
0
func (w White) String() string {
	return color.NewBrush(color.DarkGrayPaint, color.WhitePaint)(string(w))
}
Exemplo n.º 12
0
func (b Black) String() string {
	return color.NewBrush(color.WhitePaint, color.BlackPaint)(string(b))
}
Exemplo n.º 13
0
func (d DarkYellow) String() string {
	return color.NewBrush("", color.DarkYellowPaint)(string(d))
}
Exemplo n.º 14
0
func (d DarkRed) String() string {
	return color.NewBrush("", color.DarkRedPaint)(string(d))
}
Exemplo n.º 15
0
func (d DarkGreen) String() string {
	return color.NewBrush("", color.DarkGreenPaint)(string(d))
}