pretty.go 2.8 KB

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