|
| 1 | +package notice |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + |
| 11 | + "github.com/mittwald/cli-kit/term" |
| 12 | +) |
| 13 | + |
| 14 | +// Background is the background of the terminal a notice goes to, which |
| 15 | +// picks between a theme's Dark and Light colors. |
| 16 | +type Background int |
| 17 | + |
| 18 | +// The backgrounds. |
| 19 | +const ( |
| 20 | + // BackgroundAuto reads COLORFGBG, which some terminals set, and takes |
| 21 | + // the dark colors when it does not tell. |
| 22 | + BackgroundAuto Background = iota |
| 23 | + // BackgroundDark takes the dark colors. |
| 24 | + BackgroundDark |
| 25 | + // BackgroundLight takes the light colors. |
| 26 | + BackgroundLight |
| 27 | +) |
| 28 | + |
| 29 | +// Options configure a [Printer]. The zero Options print the [Line] theme to |
| 30 | +// os.Stderr. |
| 31 | +type Options struct { |
| 32 | + // Theme is how notices look. The zero Theme is [Line]; a Theme without |
| 33 | + // dark colors of its own takes [Default]. |
| 34 | + Theme Theme |
| 35 | + // Background picks the theme's dark or light colors. |
| 36 | + Background Background |
| 37 | + // Colors are set over the theme's colors, one color at a time: a color |
| 38 | + // they leave empty keeps the theme's. As in a theme, a color Light |
| 39 | + // leaves empty is taken from Dark. |
| 40 | + Colors Colors |
| 41 | + // Writer is where notices go; os.Stderr when nil. Whether they are |
| 42 | + // painted is decided for it by term.Colored. |
| 43 | + Writer io.Writer |
| 44 | + // Debug turns on the debug notices, usually with a --verbose flag. |
| 45 | + Debug bool |
| 46 | + // Around, when set, writes each notice: it gets a function that writes |
| 47 | + // it and calls that function, so a progress bar or a live display can |
| 48 | + // step aside first, as progress.Bar.WithCleared does. |
| 49 | + Around func(write func()) |
| 50 | + // KeepEscapes leaves escape sequences and control characters in the |
| 51 | + // text of a message and its arguments instead of making it plain with |
| 52 | + // term.Plain. |
| 53 | + // |
| 54 | + // Use it only for text the program wrote itself. Text from a server, a |
| 55 | + // file or an error can then move the cursor, rewrite earlier lines or |
| 56 | + // change the terminal's title. |
| 57 | + KeepEscapes bool |
| 58 | + // NoRedact leaves recognizable credentials, such as password=… or a |
| 59 | + // Bearer token, in the text instead of replacing them with |
| 60 | + // redact.Patterns. |
| 61 | + // |
| 62 | + // Use it only for text that cannot carry a credential; otherwise a |
| 63 | + // notice can put a secret on the screen and into a terminal's |
| 64 | + // scrollback or a CI log. |
| 65 | + NoRedact bool |
| 66 | +} |
| 67 | + |
| 68 | +// Printer writes notices. It is safe for concurrent use; notices from |
| 69 | +// several goroutines never interleave. |
| 70 | +type Printer struct { |
| 71 | + options Options |
| 72 | + mu sync.Mutex |
| 73 | +} |
| 74 | + |
| 75 | +// New returns a Printer with options. |
| 76 | +func New(options Options) *Printer { |
| 77 | + if options.Writer == nil { |
| 78 | + options.Writer = os.Stderr |
| 79 | + } |
| 80 | + switch t := options.Theme; { |
| 81 | + case t.Name == "" && t.Layout == LayoutLine && t.Colors.Dark.Levels == nil && t.Colors.Light.Levels == nil && |
| 82 | + t.Padding == (Padding{}) && !t.NoFill && len(t.Patterns) == 0: |
| 83 | + options.Theme = Line // the zero Theme |
| 84 | + case t.Colors.Dark.Levels == nil: |
| 85 | + options.Theme.Colors = Default // a theme that sets no colors of its own |
| 86 | + } |
| 87 | + return &Printer{options: options} |
| 88 | +} |
| 89 | + |
| 90 | +var active atomic.Pointer[Printer] |
| 91 | + |
| 92 | +// Use installs p as the Printer of the package-level functions, [Errorf] |
| 93 | +// and the rest, for the whole process. Call it once at start-up. |
| 94 | +func Use(p *Printer) { active.Store(p) } |
| 95 | + |
| 96 | +// Active returns the Printer [Use] installed, or one with the zero |
| 97 | +// [Options]. |
| 98 | +func Active() *Printer { |
| 99 | + if p := active.Load(); p != nil { |
| 100 | + return p |
| 101 | + } |
| 102 | + p := New(Options{}) |
| 103 | + if active.CompareAndSwap(nil, p) { |
| 104 | + return p |
| 105 | + } |
| 106 | + return active.Load() |
| 107 | +} |
| 108 | + |
| 109 | +// Errorf writes an error notice. Arguments are formatted as fmt.Sprintf |
| 110 | +// formats them; mark the values the message carries with [Value] or its |
| 111 | +// shorthands, such as [Date] and [Path], to color them. |
| 112 | +func (p *Printer) Errorf(format string, args ...any) { p.Notify(LevelError, format, args...) } |
| 113 | + |
| 114 | +// Warnf writes a warning notice; see [Printer.Errorf]. |
| 115 | +func (p *Printer) Warnf(format string, args ...any) { p.Notify(LevelWarn, format, args...) } |
| 116 | + |
| 117 | +// Infof writes an informational notice; see [Printer.Errorf]. |
| 118 | +func (p *Printer) Infof(format string, args ...any) { p.Notify(LevelInfo, format, args...) } |
| 119 | + |
| 120 | +// Successf writes an OK notice; see [Printer.Errorf]. |
| 121 | +func (p *Printer) Successf(format string, args ...any) { p.Notify(LevelOK, format, args...) } |
| 122 | + |
| 123 | +// Debugf writes a debug notice when [Options] Debug is set; see |
| 124 | +// [Printer.Errorf]. |
| 125 | +func (p *Printer) Debugf(format string, args ...any) { p.Notify(LevelDebug, format, args...) } |
| 126 | + |
| 127 | +// Error writes err as an error notice. A nil err writes nothing. |
| 128 | +func (p *Printer) Error(err error) { |
| 129 | + if err != nil { |
| 130 | + p.Notify(LevelError, "%v", err) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Warn writes err as a warning notice. A nil err writes nothing. |
| 135 | +func (p *Printer) Warn(err error) { |
| 136 | + if err != nil { |
| 137 | + p.Notify(LevelWarn, "%v", err) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// Notify writes a notice of level; see [Printer.Errorf]. |
| 142 | +func (p *Printer) Notify(level Level, format string, args ...any) { |
| 143 | + if level == LevelDebug && !p.options.Debug { |
| 144 | + return |
| 145 | + } |
| 146 | + colored := term.Colored(p.options.Writer) |
| 147 | + text := p.render(level, format, args, colored) |
| 148 | + write := func() { |
| 149 | + p.mu.Lock() |
| 150 | + defer p.mu.Unlock() |
| 151 | + _, _ = io.WriteString(p.options.Writer, text) |
| 152 | + } |
| 153 | + if p.options.Around != nil { |
| 154 | + p.options.Around(write) |
| 155 | + return |
| 156 | + } |
| 157 | + write() |
| 158 | +} |
| 159 | + |
| 160 | +// Errorf writes an error notice with the [Active] Printer. |
| 161 | +func Errorf(format string, args ...any) { Active().Errorf(format, args...) } |
| 162 | + |
| 163 | +// Warnf writes a warning notice with the [Active] Printer. |
| 164 | +func Warnf(format string, args ...any) { Active().Warnf(format, args...) } |
| 165 | + |
| 166 | +// Infof writes an informational notice with the [Active] Printer. |
| 167 | +func Infof(format string, args ...any) { Active().Infof(format, args...) } |
| 168 | + |
| 169 | +// Successf writes an OK notice with the [Active] Printer. |
| 170 | +func Successf(format string, args ...any) { Active().Successf(format, args...) } |
| 171 | + |
| 172 | +// Debugf writes a debug notice with the [Active] Printer. |
| 173 | +func Debugf(format string, args ...any) { Active().Debugf(format, args...) } |
| 174 | + |
| 175 | +// Error writes err as an error notice with the [Active] Printer. |
| 176 | +func Error(err error) { Active().Error(err) } |
| 177 | + |
| 178 | +// Warn writes err as a warning notice with the [Active] Printer. |
| 179 | +func Warn(err error) { Active().Warn(err) } |
| 180 | + |
| 181 | +// lightBackground reports what COLORFGBG says of the background: its last |
| 182 | +// field is the background's color number, and 7 and 15 (white) are the |
| 183 | +// light ones, as the terminals that set it use them. |
| 184 | +func lightBackground(colorfgbg string) bool { |
| 185 | + fields := strings.Split(colorfgbg, ";") |
| 186 | + n, err := strconv.Atoi(strings.TrimSpace(fields[len(fields)-1])) |
| 187 | + return err == nil && (n == 7 || n == 15) |
| 188 | +} |
| 189 | + |
| 190 | +// light reports whether the light colors apply. |
| 191 | +func (p *Printer) light() bool { |
| 192 | + switch p.options.Background { |
| 193 | + case BackgroundLight: |
| 194 | + return true |
| 195 | + case BackgroundDark: |
| 196 | + return false |
| 197 | + } |
| 198 | + return lightBackground(os.Getenv("COLORFGBG")) |
| 199 | +} |
0 commit comments