notice

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notice writes the error, warning, info, OK and debug lines of a command-line tool: one line per notice on the error stream, labeled ERROR, WARN, INFO, OK or DEBUG.

A Theme sets how a notice looks. Line writes the label in its level's color and a colon ("WARN: …"); Badge writes the label on its level's background, padded so the messages line up; Bar adds a tint of the level's color behind the message, and Block puts the whole line on the level's background. Bar and Block fill the line to the right edge of the terminal unless the theme says otherwise.

The colors of a theme are Colors: a Palette for dark terminals and one for light ones, picked by Options Background, which reads COLORFGBG by default. Default, Muted, Deep and Pastel are the built-in colors; Theme.WithColors puts any of them on any theme, and Options Colors change single colors over them.

Values in a message are marked with a Role, so they stand out in a color of their own: Date, Time, Detail, Ref and Path mark the built-in roles, and NewRole makes one of a tool's own, with colors for both backgrounds. A Pattern gives a role to whatever a regular expression matches in the message's own text, such as a merge request reference.

Every notice is made safe before it is written: escape sequences and control characters are taken out (term.Plain) and recognizable credentials replaced (redact.Patterns), in the message and in its values. Credentials are looked for in the message as it reads, values in place; a notice in which one is found is written without colored values. Where the writer takes no color, as term.Colored decides, a notice is plain text in every theme: "WARN: …".

A Printer writes notices with its Options. The package-level functions, such as Warnf, use the Printer that Use installed for the whole process, or a Line Printer on os.Stderr.

Example
package main

import (
	"errors"
	"os"

	"github.com/mittwald/cli-kit/notice"
)

func main() {
	// os.Stdout is no terminal in a test, so the notices come out plain.
	p := notice.New(notice.Options{Theme: notice.Badge, Writer: os.Stdout})
	p.Warnf("Booking day %s was never left: the start at %s has no Gehen", notice.Date("19.11.2026"), notice.Time("08:40"))
	p.Error(errors.New("request timed out: password=hunter2"))
	p.Debugf("not shown without Options.Debug")
}
Output:
WARN: Booking day 19.11.2026 was never left: the start at 08:40 has no Gehen
ERROR: request timed out: password=[REDACTED]

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// RoleDate is a date, such as "19.11.2026".
	RoleDate = Role{&roleSpec{name: "date"}}
	// RoleTime is a time of day, such as "08:40".
	RoleTime = Role{&roleSpec{name: "time"}}
	// RoleDetail is any other value: an error's text, a count, a name.
	RoleDetail = Role{&roleSpec{name: "detail"}}
	// RoleRef names something: a merge request, a review, a session, a
	// branch.
	RoleRef = Role{&roleSpec{name: "ref"}}
	// RolePath is a file or a URL.
	RolePath = Role{&roleSpec{name: "path"}}
)

The built-in roles.

View Source
var AllColors = map[string]Colors{"default": Default, "muted": Muted, "deep": Deep, "pastel": Pastel}

AllColors lists the built-in colors by name.

View Source
var Badge = Theme{Name: "badge", Layout: LayoutBadge, Colors: Default, Padding: Padding{1, 1}}

Badge is LayoutBadge in the Default colors.

View Source
var Bar = Theme{Name: "bar", Layout: LayoutBar, Colors: Default, Padding: Padding{1, 1}}

Bar is LayoutBar in the Default colors.

View Source
var Block = Theme{Name: "block", Layout: LayoutBlock, Colors: Default, Padding: Padding{1, 1}}

Block is LayoutBlock in the Default colors.

View Source
var Deep = Colors{
	Dark: palette(
		[5]string{"#8FD0F2", "#F2BD55", "#FFFFFF", "#F2A6D0", "#B7A6FF"}, "#6B7484",
		levelSpec{LevelError, "#E06C6C", "#6E1C1C", "#F4DADA", "#3E1010", "#F4DADA", deepValues},
		levelSpec{LevelWarn, "#F2BD55", "#5E4512", "#F6EBD3", "#3A2A0A", "#F6EBD3", [5]string{"#7FC4EA", "#FFD36B", "#FFFFFF", "#F5B8DA", "#C7B8FF"}},
		levelSpec{LevelInfo, "#8FD0F2", "#1D3B53", "#DCEAF4", "#102434", "#DCEAF4", [5]string{"#8FD0F2", "#F2BD55", "#FFFFFF", "#F5B8DA", "#C7B8FF"}},
		levelSpec{LevelOK, "#77C26E", "#1C4A29", "#DDF0E2", "#102A18", "#DDF0E2", [5]string{"#8FD0F2", "#F2BD55", "#FFFFFF", "#F5B8DA", "#C7B8FF"}},
		levelSpec{LevelDebug, "#6B7484", "#2E333C", "#C9CED6", "#1E2228", "#C9CED6", deepValues},
	),
	Light: lightPalette(
		[5]string{"#1D4F7A", "#7A4A00", "#202020", "#7A1F55", "#4A2F8F"}, "#6B7484",
		lightSpec{LevelError, "#6E1C1C", "#EAD5D5", "#4A1010"},
		lightSpec{LevelWarn, "#5E4512", "#E8DFC8", "#3A2A0A"},
		lightSpec{LevelInfo, "#1D3B53", "#D3DEE8", "#102434"},
		lightSpec{LevelOK, "#1C4A29", "#D3E3D7", "#102A18"},
		lightSpec{LevelDebug, "#2E333C", "#DADDE2", "#1E2228"},
	),
}

Deep are dark backgrounds with the table tones of a timesheet as values.

View Source
var Default = Colors{
	Dark: palette(
		[5]string{"#5FAFD7", "#ECB44A", "256:252", "256:218", "256:105"}, "256:244",
		levelSpec{LevelError, "256:203", "41", "97", "#5A1818", "97", [5]string{"#FFE28C", "#FFE28C", "#FFE28C", "#FFD6EC", "#D9D2FF"}},
		levelSpec{LevelWarn, "256:221", "#ECB44A", "30", "#54421C", "97", [5]string{"#14326E", "#781E00", "#14326E", "#6A0E3E", "#3A1060"}},
		levelSpec{LevelInfo, "#ADB4C0", "#5FAFD7", "30", "#1E3A4C", "97", [5]string{"#14326E", "#781E00", "#50145A", "#6A0E3E", "#3A1060"}},
		levelSpec{LevelOK, "256:118", "102", "30", "#1C4622", "97", [5]string{"#14326E", "#781E00", "#14326E", "#6A0E3E", "#3A1060"}},
		levelSpec{LevelDebug, "256:242", "#555D6B", "97", "#30343C", "97", [5]string{"#FFE28C", "#FFE28C", "#FFE28C", "#FFD6EC", "#D9D2FF"}},
	),
	Light: lightPalette(
		[5]string{"#1F5F99", "#9A5B00", "#303030", "#A0306E", "#5B3FB5"}, "#8A8F98",
		lightSpec{LevelError, "#C0392B", "#FBE0E0", "#5A1010"},
		lightSpec{LevelWarn, "#A66B00", "#FBEBC8", "#4A3300"},
		lightSpec{LevelInfo, "#4A5868", "#DCEEF8", "#0F3550"},
		lightSpec{LevelOK, "#2E7D32", "#DDF2DD", "#14451A"},
		lightSpec{LevelDebug, "#8A8F98", "#ECEEF1", "#2E333C"},
	),
}

Default are the default colors. ERROR and OK badges take the terminal's own red and bright green, so they follow its color scheme; everything else is fixed.

Levels lists every level, most serious first.

View Source
var Line = Theme{Name: "line", Layout: LayoutLine, Colors: Default, Padding: Padding{1, 1}}

Line is LayoutLine in the Default colors.

View Source
var Muted = Colors{
	Dark: palette(
		[5]string{"#7FC4EA", "#F0C36A", "#E4E7EC", "#E6A6C8", "#A99BE0"}, "#7A8290",
		levelSpec{LevelError, "#E07A7A", "#B83A3A", "#FBEAEA", "#4A1C1C", "#FBEAEA", [5]string{"#BFE3FF", "#FFD27A", "#FFFFFF", "#FFD6EC", "#E2DAFF"}},
		levelSpec{LevelWarn, "#E6B85C", "#D9A43F", "#1F1608", "#4A3814", "#F6EBD3", [5]string{"#0F3A73", "#7A1F00", "#3B1A55", "#6A0E3E", "#4A1A6A"}},
		levelSpec{LevelInfo, "#7FB2D6", "#4C8DB8", "#F2F8FC", "#1C3448", "#F2F8FC", [5]string{"#FFE7A8", "#FFC37A", "#FFFFFF", "#FFE0F0", "#E9DDFF"}},
		levelSpec{LevelOK, "#6CBF83", "#3F9A57", "#F1FAF3", "#1A3E24", "#F1FAF3", [5]string{"#D9F0FF", "#FFE08A", "#FFFFFF", "#FFE0F0", "#E9DDFF"}},
		levelSpec{LevelDebug, "#7A8290", "#4A5160", "#E4E7EC", "#2C3038", "#E4E7EC", [5]string{"#9FD3F0", "#F0C36A", "#FFFFFF", "#FFD6EC", "#D9D2FF"}},
	),
	Light: lightPalette(
		[5]string{"#2A6496", "#8C5A00", "#333333", "#9B3A72", "#5B4BA8"}, "#8A9099",
		lightSpec{LevelError, "#A33A3A", "#F6E1E1", "#4A1414"},
		lightSpec{LevelWarn, "#9A6E14", "#F7ECD4", "#3F2E08"},
		lightSpec{LevelInfo, "#3B6E91", "#DFEBF4", "#13304A"},
		lightSpec{LevelOK, "#2F7A45", "#DFF0E4", "#123A1E"},
		lightSpec{LevelDebug, "#7A8290", "#E9EBEF", "#2C3038"},
	),
}

Muted are softer colors than Default.

View Source
var Pastel = Colors{
	Dark: palette(
		[5]string{"#8FC1F0", "#F0B27A", "#E0E0E0", "#E7A3CB", "#BBA8F0"}, "#8A8F98",
		levelSpec{LevelError, "#F4B4B4", "#F4B4B4", "#3A0D0D", "#4A2E2E", "#F4DADA", [5]string{"#0B3C82", "#8A2A00", "#5A0E0E", "#7A1450", "#4A1A6A"}},
		levelSpec{LevelWarn, "#F6D58E", "#F6D58E", "#3A2A05", "#4A4028", "#F6EBD3", [5]string{"#0B3C82", "#8A2A00", "#4A1A6A", "#7A1450", "#4A1A6A"}},
		levelSpec{LevelInfo, "#B4D8F0", "#B4D8F0", "#0D2638", "#2A3A48", "#DCEAF4", [5]string{"#6A1B78", "#8A2A00", "#0B3C82", "#7A1450", "#4A1A6A"}},
		levelSpec{LevelOK, "#B8E6C2", "#B8E6C2", "#0D2E16", "#2A4232", "#DDF0E2", [5]string{"#0B3C82", "#8A2A00", "#5A1E6A", "#7A1450", "#4A1A6A"}},
		levelSpec{LevelDebug, "#9AA0AA", "#D3D8E0", "#22262E", "#3A3E46", "#D3D8E0", [5]string{"#0B3C82", "#8A2A00", "#3A3F4A", "#7A1450", "#4A1A6A"}},
	),
	Light: lightPalette(
		[5]string{"#0B3C82", "#8A2A00", "#22262E", "#8A1E5A", "#5B2A9A"}, "#6A7080",
		lightSpec{LevelError, "#B03030", "#FBE3E3", "#3A0D0D"},
		lightSpec{LevelWarn, "#9A6A00", "#FCEFD2", "#3A2A05"},
		lightSpec{LevelInfo, "#1F6FA8", "#E3F0F9", "#0D2638"},
		lightSpec{LevelOK, "#2E7D3E", "#E5F5E9", "#0D2E16"},
		lightSpec{LevelDebug, "#6A7080", "#ECEFF3", "#22262E"},
	),
}

Pastel are light backgrounds with dark ink, made for light terminals; on a dark terminal the labels, values and tints are brightened.

Roles lists the built-in roles.

View Source
var Themes = []Theme{Line, Badge, Bar, Block}

Themes lists the built-in themes.

Functions

func Date

func Date(v any) any

Date marks v as a date: Value with RoleDate. Time, Detail, Ref and Path do the same for the other built-in roles.

func Debugf

func Debugf(format string, args ...any)

Debugf writes a debug notice with the Active Printer.

func Detail

func Detail(v any) any

Detail marks v as a detail, such as an error's text; see Date.

func Error

func Error(err error)

Error writes err as an error notice with the Active Printer.

func Errorf

func Errorf(format string, args ...any)

Errorf writes an error notice with the Active Printer.

func Infof

func Infof(format string, args ...any)

Infof writes an informational notice with the Active Printer.

func Path

func Path(v any) any

Path marks v as a file or a URL; see Date.

func Ref

func Ref(v any) any

Ref marks v as a reference, such as a merge request; see Date.

func Successf

func Successf(format string, args ...any)

Successf writes an OK notice with the Active Printer.

func Time

func Time(v any) any

Time marks v as a time of day; see Date.

func Use

func Use(p *Printer)

Use installs p as the Printer of the package-level functions, Errorf and the rest, for the whole process. Call it once at start-up.

func Value

func Value(r Role, v any) any

Value marks v as a value of role r in a message: it is shown in the role's color and is never searched by a Pattern. Like every other argument, its text is made plain (term.Plain) and has credentials taken out (redact.Patterns) unless the Options say otherwise.

func Warn

func Warn(err error)

Warn writes err as a warning notice with the Active Printer.

func Warnf

func Warnf(format string, args ...any)

Warnf writes a warning notice with the Active Printer.

Types

type Background

type Background int

Background is the background of the terminal a notice goes to, which picks between a theme's Dark and Light colors.

const (
	// BackgroundAuto reads COLORFGBG, which some terminals set, and takes
	// the dark colors when it does not tell.
	BackgroundAuto Background = iota
	// BackgroundDark takes the dark colors.
	BackgroundDark
	// BackgroundLight takes the light colors.
	BackgroundLight
)

The backgrounds.

type Colors

type Colors struct {
	Dark  Palette
	Light Palette
}

Colors are the colors of a theme: Dark for terminals with a dark background, Light for terminals with a light one. A color Light leaves empty is taken from Dark, so Light needs only what a light background changes.

type Layout

type Layout int

Layout is how a notice is laid out.

const (
	// LayoutLine writes the label in its level's color, a colon and the
	// message: "WARN: …". A debug notice is a muted "+ …" line.
	LayoutLine Layout = iota
	// LayoutBadge writes the label as a badge, on its level's background,
	// padded to one width so the messages line up, and the message after it.
	LayoutBadge
	// LayoutBar is LayoutBadge with the message on a tint of the level's
	// color, filled to the right edge of the terminal.
	LayoutBar
	// LayoutBlock writes badge and message on the level's background, filled
	// to the right edge of the terminal; only the label is bold.
	LayoutBlock
)

The layouts.

type Level

type Level int

Level is how serious a notice is. It picks the label and the colors.

const (
	LevelError Level = iota
	LevelWarn
	LevelInfo
	LevelOK
	LevelDebug
)

The levels, most serious first.

func (Level) String

func (l Level) String() string

String returns the label a notice of the level carries: ERROR, WARN, INFO, OK or DEBUG.

type LevelColors

type LevelColors struct {
	// Label colors the label of LayoutLine, and a debug line as a whole.
	Label theme.Style
	// Accent is the background of the badge, and of the whole line in
	// LayoutBlock.
	Accent theme.Style
	// Text colors text on Accent.
	Text theme.Style
	// Tint is the background of the message in LayoutBar.
	Tint theme.Style
	// TintText colors text on Tint.
	TintText theme.Style
	// Values color a [Value] on Accent, in LayoutBlock. Such a value is bold
	// as well.
	Values map[Role]theme.Style
}

LevelColors are the colors of one level.

type Options

type Options struct {
	// Theme is how notices look. The zero Theme is [Line]; a Theme without
	// dark colors of its own takes [Default].
	Theme Theme
	// Background picks the theme's dark or light colors.
	Background Background
	// Colors are set over the theme's colors, one color at a time: a color
	// they leave empty keeps the theme's. As in a theme, a color Light
	// leaves empty is taken from Dark.
	Colors Colors
	// Writer is where notices go; os.Stderr when nil. Whether they are
	// painted is decided for it by term.Colored.
	Writer io.Writer
	// Debug turns on the debug notices, usually with a --verbose flag.
	Debug bool
	// Around, when set, writes each notice: it gets a function that writes
	// it and calls that function, so a progress bar or a live display can
	// step aside first, as progress.Bar.WithCleared does.
	Around func(write func())
	// KeepEscapes leaves escape sequences and control characters in the
	// text of a message and its arguments instead of making it plain with
	// term.Plain.
	//
	// Use it only for text the program wrote itself. Text from a server, a
	// file or an error can then move the cursor, rewrite earlier lines or
	// change the terminal's title.
	KeepEscapes bool
	// NoRedact leaves recognizable credentials, such as password=… or a
	// Bearer token, in the text instead of replacing them with
	// redact.Patterns.
	//
	// Use it only for text that cannot carry a credential; otherwise a
	// notice can put a secret on the screen and into a terminal's
	// scrollback or a CI log.
	NoRedact bool
}

Options configure a Printer. The zero Options print the Line theme to os.Stderr.

type Padding

type Padding struct{ Left, Right int }

Padding is the space a badge has left and right of its label, in columns.

type Palette

type Palette struct {
	// Levels are the colors of each level.
	Levels map[Level]LevelColors
	// Values color a [Value] on the terminal's own background, as LayoutLine
	// and LayoutBadge show the message, and on the tint of LayoutBar.
	Values map[Role]theme.Style
	// Separator colors the colon after a LayoutLine label.
	Separator theme.Style
}

Palette is one set of colors.

type Pattern

type Pattern struct {
	Match *regexp.Regexp
	Role  Role
}

Pattern highlights what Match finds in a message in the colors of Role. Matches are searched in the message's own text only: never inside a Value, whose role already colors it.

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer writes notices. It is safe for concurrent use; notices from several goroutines never interleave.

func Active

func Active() *Printer

Active returns the Printer Use installed, or one with the zero Options.

func New

func New(options Options) *Printer

New returns a Printer with options.

func (*Printer) Debugf

func (p *Printer) Debugf(format string, args ...any)

Debugf writes a debug notice when Options Debug is set; see Printer.Errorf.

func (*Printer) Error

func (p *Printer) Error(err error)

Error writes err as an error notice. A nil err writes nothing.

func (*Printer) Errorf

func (p *Printer) Errorf(format string, args ...any)

Errorf writes an error notice. Arguments are formatted as fmt.Sprintf formats them; mark the values the message carries with Value or its shorthands, such as Date and Path, to color them.

func (*Printer) Infof

func (p *Printer) Infof(format string, args ...any)

Infof writes an informational notice; see Printer.Errorf.

func (*Printer) Notify

func (p *Printer) Notify(level Level, format string, args ...any)

Notify writes a notice of level; see Printer.Errorf.

func (*Printer) Successf

func (p *Printer) Successf(format string, args ...any)

Successf writes an OK notice; see Printer.Errorf.

func (*Printer) Warn

func (p *Printer) Warn(err error)

Warn writes err as a warning notice. A nil err writes nothing.

func (*Printer) Warnf

func (p *Printer) Warnf(format string, args ...any)

Warnf writes a warning notice; see Printer.Errorf.

type Role

type Role struct {
	// contains filtered or unexported fields
}

Role is what a value in a message is, such as a date or a path. Each role has a color of its own in every palette, for dark and light terminals and on the background of each level. The built-in roles are colored by the built-in palettes; NewRole makes a role a tool needs beyond them.

Roles are compared by identity: two roles with the same name are still two roles.

func NewRole

func NewRole(name string, colors RoleColors) Role

NewRole makes a role of a tool's own, colored by colors wherever a palette does not color it. Dark and Light must both be set, so the role reads on either terminal; NewRole panics otherwise, at start-up rather than on the first light terminal.

func (Role) Name

func (r Role) Name() string

Name returns the name the role was made with.

type RoleColors

type RoleColors struct {
	// Dark colors the role on a dark terminal.
	Dark theme.Style
	// Light colors the role on a light terminal.
	Light theme.Style
	// OnAccent colors the role on the background of a level, as
	// LayoutBlock shows it. A level it leaves out shows the role in the
	// level's own text color, bold, which reads on any background.
	OnAccent map[Level]theme.Style
}

RoleColors are the colors a role brings along for palettes that do not color it themselves.

type Theme

type Theme struct {
	// Name says what the theme is, for messages and tests.
	Name string
	// Layout is how a notice is laid out.
	Layout Layout
	// Colors are the colors for dark and light terminals.
	Colors Colors
	// Padding is the space around a badge's label. The built-in themes use
	// one column on each side; a Theme built from scratch has none unless it
	// sets one.
	Padding Padding
	// NoFill stops the background of LayoutBar and LayoutBlock at the end of
	// the text instead of filling the line to the right edge.
	NoFill bool
	// Patterns are highlighted in the message's own text, the text of the
	// format and of arguments that are not a [Value].
	Patterns []Pattern
}

Theme is everything about how a notice looks: the layout, the colors for dark and light terminals, the badge padding, whether the background reaches the edge of the terminal, and the patterns highlighted in a message.

The built-in themes are Line, Badge, Bar and Block, all in the Default colors. A Theme is a value: the With methods return a changed copy and leave the theme they are called on as it was, so a built-in theme cannot be changed by accident.

func (Theme) WithColors

func (t Theme) WithColors(c Colors) Theme

WithColors returns t with other colors, such as Muted, Deep or Pastel.

func (Theme) WithPadding

func (t Theme) WithPadding(left, right int) Theme

WithPadding returns t with left and right columns of space around a badge's label.

func (Theme) WithPatterns

func (t Theme) WithPatterns(patterns ...Pattern) Theme

WithPatterns returns t with patterns added to the ones it has.

func (Theme) WithoutFill

func (t Theme) WithoutFill() Theme

WithoutFill returns t with the background of LayoutBar and LayoutBlock ending with the text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL