pio.go 2.2 KB

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