|
1 | 1 | package color |
2 | 2 |
|
| 3 | +import "fmt" |
| 4 | + |
| 5 | +/************************************************************* |
| 6 | + * region Message Printer |
| 7 | + *************************************************************/ |
| 8 | + |
| 9 | +// PrinterFace interface |
| 10 | +type PrinterFace interface { |
| 11 | + fmt.Stringer |
| 12 | + Sprint(a ...any) string |
| 13 | + Sprintf(format string, a ...any) string |
| 14 | + Print(a ...any) |
| 15 | + Printf(format string, a ...any) |
| 16 | + Println(a ...any) |
| 17 | +} |
| 18 | + |
| 19 | +// Printer a generic color message printer. |
| 20 | +// |
| 21 | +// Usage: |
| 22 | +// |
| 23 | +// p := &Printer{Code: "32;45;3"} |
| 24 | +// p.Print("message") |
| 25 | +type Printer struct { |
| 26 | + // NoColor disable color. |
| 27 | + NoColor bool |
| 28 | + // Code color code string. eg "32;45;3" |
| 29 | + Code string |
| 30 | +} |
| 31 | + |
| 32 | +// NewPrinter instance |
| 33 | +func NewPrinter(colorCode string) *Printer { |
| 34 | + return &Printer{Code: colorCode} |
| 35 | +} |
| 36 | + |
| 37 | +// String returns color code string. eg: "32;45;3" |
| 38 | +func (p *Printer) String() string { |
| 39 | + // panic("implement me") |
| 40 | + return p.Code |
| 41 | +} |
| 42 | + |
| 43 | +// Sprint returns rendering colored messages |
| 44 | +func (p *Printer) Sprint(a ...any) string { |
| 45 | + return RenderCode(p.String(), a...) |
| 46 | +} |
| 47 | + |
| 48 | +// Sprintf returns format and rendering colored messages |
| 49 | +func (p *Printer) Sprintf(format string, a ...any) string { |
| 50 | + return RenderString(p.String(), fmt.Sprintf(format, a...)) |
| 51 | +} |
| 52 | + |
| 53 | +// Print rendering colored messages |
| 54 | +func (p *Printer) Print(a ...any) { |
| 55 | + doPrintV2(p.String(), fmt.Sprint(a...)) |
| 56 | +} |
| 57 | + |
| 58 | +// Printf format and rendering colored messages |
| 59 | +func (p *Printer) Printf(format string, a ...any) { |
| 60 | + doPrintV2(p.String(), fmt.Sprintf(format, a...)) |
| 61 | +} |
| 62 | + |
| 63 | +// Println rendering colored messages with newline |
| 64 | +func (p *Printer) Println(a ...any) { |
| 65 | + doPrintlnV2(p.Code, a) |
| 66 | +} |
| 67 | + |
| 68 | +// IsEmpty color code |
| 69 | +func (p *Printer) IsEmpty() bool { |
| 70 | + return p.Code == "" |
| 71 | +} |
| 72 | + |
| 73 | +/************************************************************* |
| 74 | + * region SimplePrinter |
| 75 | + *************************************************************/ |
| 76 | + |
| 77 | +// SimplePrinter use for quick use color print on inject to struct |
| 78 | +type SimplePrinter struct{} |
| 79 | + |
| 80 | +// Print message |
| 81 | +func (s *SimplePrinter) Print(v ...any) { Print(v...) } |
| 82 | + |
| 83 | +// Printf message |
| 84 | +func (s *SimplePrinter) Printf(format string, v ...any) { Printf(format, v...) } |
| 85 | + |
| 86 | +// Println message |
| 87 | +func (s *SimplePrinter) Println(v ...any) { |
| 88 | + Println(v...) |
| 89 | +} |
| 90 | + |
| 91 | +// Successf message |
| 92 | +func (s *SimplePrinter) Successf(format string, a ...any) { Success.Printf(format, a...) } |
| 93 | + |
| 94 | +// Successln message |
| 95 | +func (s *SimplePrinter) Successln(a ...any) { Success.Println(a...) } |
| 96 | + |
| 97 | +// Infof message |
| 98 | +func (s *SimplePrinter) Infof(format string, a ...any) { |
| 99 | + Info.Printf(format, a...) |
| 100 | +} |
| 101 | + |
| 102 | +// Infoln message |
| 103 | +func (s *SimplePrinter) Infoln(a ...any) { Info.Println(a...) } |
| 104 | + |
| 105 | +// Warnf message |
| 106 | +func (s *SimplePrinter) Warnf(format string, a ...any) { |
| 107 | + Warn.Printf(format, a...) |
| 108 | +} |
| 109 | + |
| 110 | +// Warnln message |
| 111 | +func (s *SimplePrinter) Warnln(a ...any) { Warn.Println(a...) } |
| 112 | + |
| 113 | +// Errorf message |
| 114 | +func (s *SimplePrinter) Errorf(format string, a ...any) { |
| 115 | + Error.Printf(format, a...) |
| 116 | +} |
| 117 | + |
| 118 | +// Errorln message |
| 119 | +func (s *SimplePrinter) Errorln(a ...any) { Error.Println(a...) } |
| 120 | + |
3 | 121 | /************************************************************* |
4 | | - * quick use color print message |
| 122 | + * region quick start |
5 | 123 | *************************************************************/ |
6 | 124 |
|
7 | 125 | // Redp print message with Red color |
|
0 commit comments