covermyass/output/printer.go

59 lines
1.0 KiB
Go
Raw Normal View History

2022-10-31 10:20:17 +00:00
package output
import (
"fmt"
"os"
)
type Printer interface {
Printf(string, ...interface{})
Println(string)
Write([]byte) (int, error)
2022-10-31 10:20:17 +00:00
}
var globalPrinter Printer = &VoidPrinter{}
func GetPrinter() Printer {
return globalPrinter
}
2022-10-31 10:20:17 +00:00
func ChangePrinter(printer Printer) {
globalPrinter = printer
}
func Printf(format string, args ...interface{}) {
globalPrinter.Printf(format, args...)
}
func Println(format string) {
globalPrinter.Printf(format)
}
type ConsolePrinter struct{}
func NewConsolePrinter() Printer {
return &ConsolePrinter{}
}
func (c *ConsolePrinter) Printf(format string, args ...interface{}) {
_, _ = fmt.Fprintf(os.Stdout, format, args...)
}
func (c *ConsolePrinter) Println(format string) {
_, _ = fmt.Fprintln(os.Stdout, format)
}
func (c *ConsolePrinter) Write(b []byte) (int, error) {
return os.Stdout.Write(b)
}
2022-10-31 10:20:17 +00:00
type VoidPrinter struct{}
func (v *VoidPrinter) Printf(_ string, _ ...interface{}) {}
func (v *VoidPrinter) Println(_ string) {}
func (v *VoidPrinter) Write(b []byte) (int, error) {
return 0, nil
}