gopkg.in/tools/godep.v56@v56.0.0-20160226230103-b32db8cfcaad/Godeps/_workspace/src/github.com/kr/pretty/pretty.go (about)

     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.
     8  package pretty
     9  
    10  import (
    11  	"fmt"
    12  	"io"
    13  	"log"
    14  )
    15  
    16  // Errorf is a convenience wrapper for fmt.Errorf.
    17  //
    18  // Calling Errorf(f, x, y) is equivalent to
    19  // fmt.Errorf(f, Formatter(x), Formatter(y)).
    20  func Errorf(format string, a ...interface{}) error {
    21  	return fmt.Errorf(format, wrap(a, false)...)
    22  }
    23  
    24  // Fprintf is a convenience wrapper for fmt.Fprintf.
    25  //
    26  // Calling Fprintf(w, f, x, y) is equivalent to
    27  // fmt.Fprintf(w, f, Formatter(x), Formatter(y)).
    28  func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) {
    29  	return fmt.Fprintf(w, format, wrap(a, false)...)
    30  }
    31  
    32  // Log is a convenience wrapper for log.Printf.
    33  //
    34  // Calling Log(x, y) is equivalent to
    35  // log.Print(Formatter(x), Formatter(y)), but each operand is
    36  // formatted with "%# v".
    37  func Log(a ...interface{}) {
    38  	log.Print(wrap(a, true)...)
    39  }
    40  
    41  // Logf is a convenience wrapper for log.Printf.
    42  //
    43  // Calling Logf(f, x, y) is equivalent to
    44  // log.Printf(f, Formatter(x), Formatter(y)).
    45  func Logf(format string, a ...interface{}) {
    46  	log.Printf(format, wrap(a, false)...)
    47  }
    48  
    49  // Logln is a convenience wrapper for log.Printf.
    50  //
    51  // Calling Logln(x, y) is equivalent to
    52  // log.Println(Formatter(x), Formatter(y)), but each operand is
    53  // formatted with "%# v".
    54  func Logln(a ...interface{}) {
    55  	log.Println(wrap(a, true)...)
    56  }
    57  
    58  // Print pretty-prints its operands and writes to standard output.
    59  //
    60  // Calling Print(x, y) is equivalent to
    61  // fmt.Print(Formatter(x), Formatter(y)), but each operand is
    62  // formatted with "%# v".
    63  func Print(a ...interface{}) (n int, errno error) {
    64  	return fmt.Print(wrap(a, true)...)
    65  }
    66  
    67  // Printf is a convenience wrapper for fmt.Printf.
    68  //
    69  // Calling Printf(f, x, y) is equivalent to
    70  // fmt.Printf(f, Formatter(x), Formatter(y)).
    71  func Printf(format string, a ...interface{}) (n int, errno error) {
    72  	return fmt.Printf(format, wrap(a, false)...)
    73  }
    74  
    75  // Println pretty-prints its operands and writes to standard output.
    76  //
    77  // Calling Print(x, y) is equivalent to
    78  // fmt.Println(Formatter(x), Formatter(y)), but each operand is
    79  // formatted with "%# v".
    80  func Println(a ...interface{}) (n int, errno error) {
    81  	return fmt.Println(wrap(a, true)...)
    82  }
    83  
    84  // Sprintf is a convenience wrapper for fmt.Sprintf.
    85  //
    86  // Calling Sprintf(f, x, y) is equivalent to
    87  // fmt.Sprintf(f, Formatter(x), Formatter(y)).
    88  func Sprintf(format string, a ...interface{}) string {
    89  	return fmt.Sprintf(format, wrap(a, false)...)
    90  }
    91  
    92  func wrap(a []interface{}, force bool) []interface{} {
    93  	w := make([]interface{}, len(a))
    94  	for i, x := range a {
    95  		w[i] = formatter{x: x, force: force}
    96  	}
    97  	return w
    98  }