1// Package pretty provides pretty-printing for Go values. This is 2// useful during debugging, to avoid wrapping long output lines in 3// the terminal. 4// 5// It provides a function, Formatter, that can be used with any 6// function that accepts a format string. It also provides 7// convenience wrappers for functions in packages fmt and log. 8package pretty 9 10import ( 11 "fmt" 12 "io" 13 "log" 14 "reflect" 15) 16 17// Errorf is a convenience wrapper for fmt.Errorf. 18// 19// Calling Errorf(f, x, y) is equivalent to 20// fmt.Errorf(f, Formatter(x), Formatter(y)). 21func Errorf(format string, a ...interface{}) error { 22 return fmt.Errorf(format, wrap(a, false)...) 23} 24 25// Fprintf is a convenience wrapper for fmt.Fprintf. 26// 27// Calling Fprintf(w, f, x, y) is equivalent to 28// fmt.Fprintf(w, f, Formatter(x), Formatter(y)). 29func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) { 30 return fmt.Fprintf(w, format, wrap(a, false)...) 31} 32 33// Log is a convenience wrapper for log.Printf. 34// 35// Calling Log(x, y) is equivalent to 36// log.Print(Formatter(x), Formatter(y)), but each operand is 37// formatted with "%# v". 38func Log(a ...interface{}) { 39 log.Print(wrap(a, true)...) 40} 41 42// Logf is a convenience wrapper for log.Printf. 43// 44// Calling Logf(f, x, y) is equivalent to 45// log.Printf(f, Formatter(x), Formatter(y)). 46func Logf(format string, a ...interface{}) { 47 log.Printf(format, wrap(a, false)...) 48} 49 50// Logln is a convenience wrapper for log.Printf. 51// 52// Calling Logln(x, y) is equivalent to 53// log.Println(Formatter(x), Formatter(y)), but each operand is 54// formatted with "%# v". 55func Logln(a ...interface{}) { 56 log.Println(wrap(a, true)...) 57} 58 59// Print pretty-prints its operands and writes to standard output. 60// 61// Calling Print(x, y) is equivalent to 62// fmt.Print(Formatter(x), Formatter(y)), but each operand is 63// formatted with "%# v". 64func Print(a ...interface{}) (n int, errno error) { 65 return fmt.Print(wrap(a, true)...) 66} 67 68// Printf is a convenience wrapper for fmt.Printf. 69// 70// Calling Printf(f, x, y) is equivalent to 71// fmt.Printf(f, Formatter(x), Formatter(y)). 72func Printf(format string, a ...interface{}) (n int, errno error) { 73 return fmt.Printf(format, wrap(a, false)...) 74} 75 76// Println pretty-prints its operands and writes to standard output. 77// 78// Calling Print(x, y) is equivalent to 79// fmt.Println(Formatter(x), Formatter(y)), but each operand is 80// formatted with "%# v". 81func Println(a ...interface{}) (n int, errno error) { 82 return fmt.Println(wrap(a, true)...) 83} 84 85// Sprint is a convenience wrapper for fmt.Sprintf. 86// 87// Calling Sprint(x, y) is equivalent to 88// fmt.Sprint(Formatter(x), Formatter(y)), but each operand is 89// formatted with "%# v". 90func Sprint(a ...interface{}) string { 91 return fmt.Sprint(wrap(a, true)...) 92} 93 94// Sprintf is a convenience wrapper for fmt.Sprintf. 95// 96// Calling Sprintf(f, x, y) is equivalent to 97// fmt.Sprintf(f, Formatter(x), Formatter(y)). 98func Sprintf(format string, a ...interface{}) string { 99 return fmt.Sprintf(format, wrap(a, false)...) 100} 101 102func wrap(a []interface{}, force bool) []interface{} { 103 w := make([]interface{}, len(a)) 104 for i, x := range a { 105 w[i] = formatter{v: reflect.ValueOf(x), force: force} 106 } 107 return w 108} 109