adapter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package pio
  2. import (
  3. "io"
  4. )
  5. // Available sources.
  6. type (
  7. printFunc func(interface{})
  8. printVariadicFunc func(...interface{})
  9. printfFunc func(string, ...interface{})
  10. printlnFunc func(string)
  11. )
  12. type writerFunc func([]byte) (int, error)
  13. func (w writerFunc) Write(p []byte) (n int, err error) {
  14. return w(p)
  15. }
  16. // Wrap returns a new output based on the "printfFn"
  17. // if not a compatible output found then it will
  18. // return a writer which writes nothing.
  19. //
  20. // To check if the wrapping worked
  21. // you can check if the result `io.Writer`
  22. // `IsNop`, i.e:
  23. // std's log.Panicf is not a compatible interface
  24. //
  25. // output := Output(log.Panicf)
  26. // if IsNop(output) {
  27. // // conversation failed, do something or panic.
  28. //}
  29. func Wrap(printFn interface{}) io.Writer {
  30. switch printFn.(type) {
  31. case io.Writer:
  32. return printFn.(io.Writer)
  33. case writerFunc:
  34. return printFn.(io.Writer)
  35. case printFunc:
  36. return OutputFrom.Print(printFn.(printFunc))
  37. case printVariadicFunc:
  38. return OutputFrom.PrintVardiadic(printFn.(printVariadicFunc))
  39. case printfFunc:
  40. return OutputFrom.Printf(printFn.(printfFunc))
  41. case printlnFunc:
  42. return OutputFrom.Println(printFn.(printlnFunc), false)
  43. }
  44. return NopOutput()
  45. }
  46. // OutputFrom is a variable
  47. // which contains some helpers that can
  48. // convert some forms of output to compatible `io.Writer`
  49. // in order to be passed to the `NewPrinter` or `Register` functions.
  50. var OutputFrom = OutputAdapters{}
  51. // OutputAdapters is a struct
  52. // which contains some forms of output
  53. // and convert them to a compatible `io.Writer`
  54. // in order to be passed to the `NewPrinter` or `Register` functions.
  55. type OutputAdapters struct{}
  56. // Print converts a func(v interface{}) to a compatible `io.Writer`.
  57. func (a *OutputAdapters) Print(print func(v interface{})) io.Writer {
  58. return &printAdapter{
  59. print: print,
  60. }
  61. }
  62. // PrintVardiadic converts a func(v ...interface{}) to a compatible `io.Writer`.
  63. func (a *OutputAdapters) PrintVardiadic(print func(v ...interface{})) io.Writer {
  64. return &printVariadicAdapter{
  65. printVariadic: print,
  66. }
  67. }
  68. // Printf converts a func(string, ...interface{}) to a compatible `io.Writer`.
  69. func (a *OutputAdapters) Printf(printf func(format string, args ...interface{})) io.Writer {
  70. return &printfAdapter{
  71. printf: printf,
  72. }
  73. }
  74. // Println converts a func(string) to a compatible `io.Writer`.
  75. // if "newLine" is true then "\n" will be appended to the "s".
  76. func (a *OutputAdapters) Println(println func(s string), newLine bool) io.Writer {
  77. return &printlnAdapter{
  78. println: println,
  79. newLine: newLine,
  80. }
  81. }
  82. type (
  83. printAdapter struct {
  84. print printFunc
  85. }
  86. printVariadicAdapter struct {
  87. printVariadic printVariadicFunc
  88. }
  89. printfAdapter struct {
  90. printf printfFunc
  91. }
  92. printlnAdapter struct {
  93. println printlnFunc
  94. newLine bool
  95. }
  96. )
  97. func (p *printAdapter) Write(b []byte) (int, error) {
  98. p.print(string(b))
  99. return len(b), nil
  100. }
  101. func (p *printVariadicAdapter) Write(b []byte) (int, error) {
  102. p.printVariadic(string(b))
  103. return len(b), nil
  104. }
  105. func (p *printfAdapter) Write(b []byte) (int, error) {
  106. p.printf(string(b))
  107. return len(b), nil
  108. }
  109. func (p *printlnAdapter) Write(b []byte) (int, error) {
  110. if p.newLine {
  111. b = append(b, NewLine...)
  112. }
  113. p.println(string(b))
  114. return len(b), nil
  115. }