pio.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package pio
  2. import (
  3. "io"
  4. )
  5. // NewLine is a slice of bytes which controls the
  6. // how a new line should be presented.
  7. //
  8. // Defaults to \n.
  9. var NewLine = []byte("\n")
  10. // Default returns the default, package-level registry instance.
  11. var Default = NewRegistry()
  12. // RegisterPrinter registers an already-created printer to the
  13. // registry.
  14. //
  15. // If a printer with the same `Name` is already
  16. // registered then it will be overridden by
  17. // this new "printer".
  18. //
  19. // Returns the Registry, therefore it can be used as builder.
  20. func RegisterPrinter(p *Printer) *Registry {
  21. return Default.RegisterPrinter(p)
  22. }
  23. // Register creates and registers a new Printer
  24. // based on a name(string) and an "output"(io.Writer).
  25. //
  26. // If a printer with the same `Name` is already
  27. // registered then it will be overridden by
  28. // this new "printer".
  29. //
  30. // Look `OutputFrom` too.
  31. //
  32. // Returns the just created Printer.
  33. func Register(printerName string, output io.Writer) *Printer {
  34. return Default.Register(printerName, output)
  35. }
  36. // Get returns a Printer based on the "printerName".
  37. // If printer with this name can't be found then
  38. // this function will return nil, so a check for
  39. // nil is always a good practice.
  40. func Get(printerName string) *Printer {
  41. return Default.Get(printerName)
  42. }
  43. // Remove deletes a printer item from the printers collection
  44. // by its name.
  45. func Remove(printerName string) {
  46. Default.Remove(printerName)
  47. }
  48. // Print accepts a value of "v",
  49. // tries to marshal its contents and flushes the result
  50. // to all available printers.
  51. func Print(v interface{}) (int, error) {
  52. return Default.Print(v)
  53. }
  54. // Println accepts a value of "v",
  55. // tries to marshal its contents and flushes the result
  56. // to all available printers, it adds a new line at the ending,
  57. // the result doesn't contain this new line, therefore result's contnets kept as expected.
  58. func Println(v interface{}) (int, error) {
  59. return Default.Println(v)
  60. }
  61. // Scan scans everything from "r" and prints
  62. // its new contents to the printers,
  63. // forever or until the returning "cancel" is fired, once.
  64. func Scan(r io.Reader, addNewLine bool) (cancel func()) {
  65. return Default.Scan(r, addNewLine)
  66. }