github.com/dvln/pretty@v0.0.0-20161024040402-00a5f9316993/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  	"github.com/dvln/text"
    17  )
    18  
    19  // Errorf is a convenience wrapper for fmt.Errorf.
    20  //
    21  // Calling Errorf(f, x, y) is equivalent to
    22  // fmt.Errorf(f, Formatter(x), Formatter(y)).
    23  func Errorf(format string, a ...interface{}) error {
    24  	str := text.Indent(fmt.Sprintf(format, wrap(a, false)...), outputPrefixStr)
    25  	return fmt.Errorf("%s", str)
    26  }
    27  
    28  // Fprintf is a convenience wrapper for fmt.Fprintf.
    29  //
    30  // Calling Fprintf(w, f, x, y) is equivalent to
    31  // fmt.Fprintf(w, f, Formatter(x), Formatter(y)).
    32  func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) {
    33  	str := text.Indent(fmt.Sprintf(format, wrap(a, false)...), outputPrefixStr)
    34  	return fmt.Fprint(w, str)
    35  }
    36  
    37  // Log is a convenience wrapper for log.Printf.
    38  //
    39  // Calling Log(x, y) is equivalent to
    40  // log.Print(Formatter(x), Formatter(y)), but each operand is
    41  // formatted with "%# v".
    42  func Log(a ...interface{}) {
    43  	str := text.Indent(fmt.Sprint(wrap(a, true)...), outputPrefixStr)
    44  	log.Print(str)
    45  }
    46  
    47  // Logf is a convenience wrapper for log.Printf.
    48  //
    49  // Calling Logf(f, x, y) is equivalent to
    50  // log.Printf(f, Formatter(x), Formatter(y)).
    51  func Logf(format string, a ...interface{}) {
    52  	str := text.Indent(fmt.Sprintf(format, wrap(a, false)...), outputPrefixStr)
    53  	log.Print(str)
    54  }
    55  
    56  // Logln is a convenience wrapper for log.Printf.
    57  //
    58  // Calling Logln(x, y) is equivalent to
    59  // log.Println(Formatter(x), Formatter(y)), but each operand is
    60  // formatted with "%# v".
    61  func Logln(a ...interface{}) {
    62  	str := text.Indent(fmt.Sprintln(wrap(a, true)...), outputPrefixStr)
    63  	log.Print(str)
    64  }
    65  
    66  // Print pretty-prints its operands and writes to standard output.
    67  //
    68  // Calling Print(x, y) is equivalent to
    69  // fmt.Print(Formatter(x), Formatter(y)), but each operand is
    70  // formatted with "%# v".
    71  func Print(a ...interface{}) (n int, errno error) {
    72  	str := text.Indent(fmt.Sprint(wrap(a, true)...), outputPrefixStr)
    73  	return fmt.Print(str)
    74  }
    75  
    76  // Printf is a convenience wrapper for fmt.Printf.
    77  //
    78  // Calling Printf(f, x, y) is equivalent to
    79  // fmt.Printf(f, Formatter(x), Formatter(y)).
    80  func Printf(format string, a ...interface{}) (n int, errno error) {
    81  	str := text.Indent(fmt.Sprintf(format, wrap(a, false)...), outputPrefixStr)
    82  	return fmt.Print(str)
    83  }
    84  
    85  // Println pretty-prints its operands and writes to standard output.
    86  //
    87  // Calling Print(x, y) is equivalent to
    88  // fmt.Println(Formatter(x), Formatter(y)), but each operand is
    89  // formatted with "%# v".
    90  func Println(a ...interface{}) (n int, errno error) {
    91  	str := text.Indent(fmt.Sprintln(wrap(a, true)...), outputPrefixStr)
    92  	return fmt.Println(str)
    93  }
    94  
    95  // Sprint is a convenience wrapper for fmt.Sprintf.
    96  //
    97  // Calling Sprint(x, y) is equivalent to
    98  // fmt.Sprint(Formatter(x), Formatter(y)), but each operand is
    99  // formatted with "%# v".
   100  func Sprint(a ...interface{}) string {
   101  	return fmt.Sprint(wrap(a, true)...)
   102  }
   103  
   104  // Sprintf is a convenience wrapper for fmt.Sprintf.
   105  //
   106  // Calling Sprintf(f, x, y) is equivalent to
   107  // fmt.Sprintf(f, Formatter(x), Formatter(y)).
   108  func Sprintf(format string, a ...interface{}) string {
   109  	return text.Indent(fmt.Sprintf(format, wrap(a, false)...), outputPrefixStr)
   110  }
   111  
   112  // Sprintln is a convenience wrapper for fmt.Sprintln.
   113  //
   114  // Calling Sprintln(x, y) is equivalent to
   115  // fmt.Sprintln(Formatter(x), Formatter(y)).
   116  func Sprintln(a ...interface{}) string {
   117  	return text.Indent(fmt.Sprintln(wrap(a, false)...), outputPrefixStr)
   118  }
   119  
   120  func wrap(a []interface{}, force bool) []interface{} {
   121  	w := make([]interface{}, len(a))
   122  	for i, x := range a {
   123  		w[i] = formatter{v: reflect.ValueOf(x), force: force}
   124  	}
   125  	return w
   126  }