Documentation
¶
Overview ¶
termcolor defines types and functions to output colorized console output.
termcolor provides a Printer that wraps an io.Writer to print colorized messages. It supports a guard that suppresses all color information if the Printer's underlying io.Writer is no TTY. Thus, applications can use a single, colorized output API but have standard text being printed if outout is redirected.
Example ¶
package main
import (
"github.com/halimath/termcolor"
)
func main() {
p := termcolor.Stdout()
p.Printf("Welcome to %s output!", p.Styled("colored", termcolor.ForegroundCyan), termcolor.Bold)
}
Output: Welcome to colored output!
Index ¶
Examples ¶
Constants ¶
const ( // Reset rests all styles to their default. Reset style = "0" // Bold renders text in bold (somewhat brighter). Bold style = "1" // Default applies the default styles. Default style = "22" // Foreground color black ForegroundBlack style = "30" // Foreground color red ForegroundRed style = "31" // Foreground color green ForegroundGreen style = "32" // Foreground color yellow ForegroundYellow style = "33" // Foreground color blue ForegroundBlue style = "34" // Foreground color magenta ForegroundMagenta style = "35" // Foreground color cyan ForegroundCyan style = "36" // Foreground color white ForegroundWhite style = "37" // Background color back BackgroundBlack style = "40" // Background color red BackgroundRed style = "41" // Background color green BackgroundGreen style = "42" // Background color yellow BackgroundYellow style = "43" // Background color blue BackgroundBlue style = "44" // Background color magenta BackgroundMagenta style = "45" // Background color cyan BackgroundCyan style = "46" // Background color white BackgroundWhite style = "47" )
Variables ¶
This section is empty.
Functions ¶
func Activate ¶
func Activate(styles ...style) string
Activate returns a string that activates styles.
func ApplyStyles ¶
ApplyStyles applies all styles to s and returns the resulting string.
Types ¶
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer defines a type that provides methods to produce colorized output. Output is written to w if isTTY is set to true, otherwise output is written unstyled.
func NewFromFile ¶
NewFromFile creates a new Printer writing to f and applying colors if f is character device.
func Stderr ¶
func Stderr() *Printer
Stderr creates a Printer for os.Stderr applying colors if stout is a console device.
func Stdout ¶
func Stdout() *Printer
Stdout creates a Printer for os.Stdout applying colors if stout is a console device.
func (*Printer) Print ¶
Print prints msg with all styles applied. It returns any error returned from writing to p's io.Writer.
func (*Printer) Printf ¶
Printf prints formatted and styled output to p's underlying io.Writer. It works almost the same as fmt.Printf with the exception that any value given in argsAndStyles that is of type style will be excluded from formatting and will be used to style the overall string in stead. So
p.Printf("hello, %s", "world", termcolor.ForegroundRed)
will print "hello, world" in red color, if p is a TTY.
It returns any error returned from writing to p's io.Writer.