Documentation ¶
Overview ¶
Package color extends fmt.Printf with verbs for producing colored output.
Printing ¶
Verbs:
%h[attr...] replaced with a SGR code that sets all of the attributes in [] multiple attributes are + separated %r an abbreviation for %h[reset]
Preparing Strings:
While this package is heavily optimized, processing the highlighting verbs is still very expensive. Thus, it makes more sense to process the verbs once and then store the results into a Format structure. The format structure, holds two strings, one for when colored output is enabled and the other for when it is disabled.
Use the Prepare function to create Format structures. Then, use the Printfp like functions to use them as the base format strings, or send them as part of the variadic arguments to any Print function and they will be expanded to their appropriate strings. See Prepare below for an example.
Errors:
If an error occurs, the generated string will contain a description of the problem, as in these examples.
Invalid character in the highlight verb: Printf("%h(fgRed)%s", "hi"): %!h(INVALID) No attributes in the highlight verb: Printf("%h[]%s", "hi"): %!h(MISSING) Unknown attribute in the highlight verb: Printf("%h[fgGdsds]%s", "hi"): %!h(BADATTR) String ended before the verb: Printf("%h[fg", "hi"): %!h(SHORT)
Everything else is handled by the fmt package. You should read its documentation.
Attributes Reference ¶
Named Colors:
%h[xgBlack] %h[xgRed] %h[xgGreen] %h[xgYellow] %h[xgBlue] %h[xgMagenta] %h[xgCyan] %h[xgWhite] %h[xgBrightBlack] %h[xgBrightRed] %h[xgBrightGreen] %h[xgBrightYellow] %h[xgBrightBlue] %h[xgBrightMagenta] %h[xgBrightCyan] %h[xgBrightWhite] Where 'x' is either 'f' or 'b'.
256 Colors:
%h[fgx] %h[bgx] Where x is any number from 0-255.
Modes:
%h[reset] or the %r verb %h[bold] %h[underline] %h[reverse] %h[blink] %h[dim]
See http://goo.gl/LRLA7o for information on the attributes. Scroll down to the SGR section.
See http://goo.gl/fvtHLs and ISO-8613-3 (according to above document) for more information on 256 colors.
Example (Attributes) ¶
package main import ( "github.com/nhooyr/color" ) func main() { // "panic:" with a red foreground then normal "foo". color.Printf("%h[fgRed]panic:%r %s\n", "foo") // "panic:" with a red background then normal "bar". color.Printf("%h[bgRed]panic:%r %s\n", "bar") // Bold "panic:" then normal "foo". color.Printf("%h[bold]panic:%r %s\n", "foo") // Underlined "panic:" with then normal "bar". color.Printf("%h[underline]panic:%r %s\n", "bar") // "panic:" using color 83 as the foreground then normal "foo". color.Printf("%h[fg83]panic:%r %s\n", "foo") // "panic:" using color 158 as the background then normal "bar". color.Printf("%h[bg158]panic:%r %s\n", "bar") }
Output:
Example (Mixing) ¶
package main import ( "github.com/nhooyr/color" ) func main() { // Bolded "panic:" with a green foreground then normal "foo". color.Printf("%h[fgGreen+bold]panic:%r %s\n", "foo") // Underlined "panic:" with a bright black background then normal "bar". color.Printf("%h[bg8+underline]panic:%r %s\n", "bar") }
Output:
Index ¶
- func ExpandFormats(color bool, a []interface{})
- func Highlight(s string) string
- func IsTerminal(f *os.File) bool
- func Print(a ...interface{}) (n int, err error)
- func Printf(format string, a ...interface{}) (n int, err error)
- func Printfp(f *Format, a ...interface{}) (n int, err error)
- func Println(a ...interface{}) (n int, err error)
- func Run(s string, color bool) string
- func Strip(s string) string
- type Format
- type Printer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpandFormats ¶
func ExpandFormats(color bool, a []interface{})
ExpandFormats replaces each Format in a with its appropriate string according to color.
func Highlight ¶
Highlight replaces the highlight verbs in s with the appropriate control sequences and then returns the resulting string. It is a thin wrapper around Run.
func IsTerminal ¶
IsTerminal returns true if f is a terminal and false otherwise.
Types ¶
type Format ¶
type Format struct {
// contains filtered or unexported fields
}
Format represents a format string with the highlight verbs fully parsed. TODO interface
func Prepare ¶
Prepare returns a Format structure using f as the base string.
Example ¶
package main import ( "github.com/nhooyr/color" ) func main() { // Prepare only processes the highlight verbs in the string, // letting you print it repeatedly with performance. panicFormat := color.Prepare("%h[fgRed+bold]panic:%r %s\n") // Each will print "panic:" and some normal text after, but if standard output // is a terminal, "panic:" will be printed in bold with a red foreground. color.Printfp(panicFormat, "foo") color.Printfp(panicFormat, "bar") color.Printfp(panicFormat, "foo") hello := color.Prepare("%h[fgBlue+bold]HELLO%r") // Each will print "HELLO", but if standard output is a terminal, // "HELLO" will be printed in bold with a blue foreground. color.Println(hello) color.Println(hello) color.Println(hello) }
Output:
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer prints to a writer using highlight verbs.
Example ¶
package main import ( "os" "github.com/nhooyr/color" ) func main() { redFormat := color.Prepare("%h[fgRed]%s%r\n") // If standard error is a terminal, this will print in color. // Otherwise it will print a normal "bar". p := color.New(os.Stderr, color.IsTerminal(os.Stderr)) p.Printfp(redFormat, "bar") // "foo" with red foreground. p = color.New(os.Stderr, true) p.Printfp(redFormat, "foo") // Normal "bar", the highlight verbs are ignored. p = color.New(os.Stderr, false) p.Printfp(redFormat, "bar") }
Output:
func New ¶
New creates a new Printer that writes to out. The color argument dictates whether color output is enabled.
func (*Printer) Print ¶
Print calls fmt.Fprint to print to the underlying writer. It will expand each Format in a to its appropriate string before calling fmt.Fprint.
func (*Printer) Printf ¶
Printf first processes the highlight verbs in format and then calls fmt.Fprintf with the processed format and the other arguments. It will expand each Format in a to its appropriate string before calling fmt.Fprintf. It returns the number of bytes written an any write error encountered.