adapter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //
  27. // if IsNop(output) {
  28. // // conversation failed, do something or panic.
  29. // }
  30. func Wrap(printFn interface{}) io.Writer {
  31. switch printFn.(type) {
  32. case io.Writer:
  33. return printFn.(io.Writer)
  34. case writerFunc:
  35. return printFn.(io.Writer)
  36. case printFunc:
  37. return OutputFrom.Print(printFn.(printFunc))
  38. case printVariadicFunc:
  39. return OutputFrom.PrintVardiadic(printFn.(printVariadicFunc))
  40. case printfFunc:
  41. return OutputFrom.Printf(printFn.(printfFunc))
  42. case printlnFunc:
  43. return OutputFrom.Println(printFn.(printlnFunc), false)
  44. }
  45. return NopOutput()
  46. }
  47. // OutputFrom is a variable
  48. // which contains some helpers that can
  49. // convert some forms of output to compatible `io.Writer`
  50. // in order to be passed to the `NewPrinter` or `Register` functions.
  51. var OutputFrom = OutputAdapters{}
  52. // OutputAdapters is a struct
  53. // which contains some forms of output
  54. // and convert them to a compatible `io.Writer`
  55. // in order to be passed to the `NewPrinter` or `Register` functions.
  56. type OutputAdapters struct{}
  57. // Print converts a func(v interface{}) to a compatible `io.Writer`.
  58. func (a *OutputAdapters) Print(print func(v interface{})) io.Writer {
  59. return &printAdapter{
  60. print: print,
  61. }
  62. }
  63. // PrintVardiadic converts a func(v ...interface{}) to a compatible `io.Writer`.
  64. func (a *OutputAdapters) PrintVardiadic(print func(v ...interface{})) io.Writer {
  65. return &printVariadicAdapter{
  66. printVariadic: print,
  67. }
  68. }
  69. // Printf converts a func(string, ...interface{}) to a compatible `io.Writer`.
  70. func (a *OutputAdapters) Printf(printf func(format string, args ...interface{})) io.Writer {
  71. return &printfAdapter{
  72. printf: printf,
  73. }
  74. }
  75. // Println converts a func(string) to a compatible `io.Writer`.
  76. // if "newLine" is true then "\n" will be appended to the "s".
  77. func (a *OutputAdapters) Println(println func(s string), newLine bool) io.Writer {
  78. return &printlnAdapter{
  79. println: println,
  80. newLine: newLine,
  81. }
  82. }
  83. type (
  84. printAdapter struct {
  85. print printFunc
  86. }
  87. printVariadicAdapter struct {
  88. printVariadic printVariadicFunc
  89. }
  90. printfAdapter struct {
  91. printf printfFunc
  92. }
  93. printlnAdapter struct {
  94. println printlnFunc
  95. newLine bool
  96. }
  97. )
  98. func (p *printAdapter) Write(b []byte) (int, error) {
  99. p.print(string(b))
  100. return len(b), nil
  101. }
  102. func (p *printVariadicAdapter) Write(b []byte) (int, error) {
  103. p.printVariadic(string(b))
  104. return len(b), nil
  105. }
  106. func (p *printfAdapter) Write(b []byte) (int, error) {
  107. p.printf(string(b))
  108. return len(b), nil
  109. }
  110. func (p *printlnAdapter) Write(b []byte) (int, error) {
  111. if p.newLine {
  112. b = append(b, NewLine...)
  113. }
  114. p.println(string(b))
  115. return len(b), nil
  116. }