github.com/gozelle/pretty@v0.3.1/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  	"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)).
    21  func 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)).
    29  func 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".
    38  func 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)).
    46  func 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".
    55  func 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".
    64  func 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)).
    72  func 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 Println(x, y) is equivalent to
    79  // fmt.Println(Formatter(x), Formatter(y)), but each operand is
    80  // formatted with "%# v".
    81  func 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".
    90  func 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)).
    98  func Sprintf(format string, a ...interface{}) string {
    99  	return fmt.Sprintf(format, wrap(a, false)...)
   100  }
   101  
   102  func 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  }