clistyle

package
v1.5.18 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 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 clistyle is the single place for terminal color, semantic styles and the ASCII logo. It only writes to the io.Writer it is given.

Index

Constants

View Source
const DefaultThemeName = "default"

DefaultThemeName is the palette used when nothing else is chosen.

View Source
const EnvTheme = "LEVELRAIL_THEME"

EnvTheme is the environment variable that picks a theme.

Variables

This section is empty.

Functions

func BannerString

func BannerString(mode Mode, light, unicode bool, info []string) string

BannerString draws the medium mark with up to six text lines beside it, vertically centered on the mark: line 0 is the product name in the brand color, the rest are muted. The mark is skipped without unicode, so the text stands alone. Escape-free when mode is ModeNone.

func LogoString

func LogoString(size LogoSize, mode Mode, light, unicode bool) string

LogoString renders the logo. Without unicode every size is the ASCII wordmark.

func RenderLogo(w io.Writer, size LogoSize, mode Mode, light, unicode bool) error

RenderLogo writes the logo to w.

func RoleName

func RoleName(r Role) string

RoleName is the stable lowercase name of r.

func ThemeNames

func ThemeNames() []string

ThemeNames returns the sorted theme names.

func UnicodeOK

func UnicodeOK(lookup Lookup) bool

UnicodeOK is false for consoles that cannot draw the block characters.

Types

type LogoContext

type LogoContext int

LogoContext is where the caller is about to print.

const (
	CtxOther      LogoContext = iota // any other command, never shows
	CtxFirstRun                      // setup or the first command ever run
	CtxVersion                       // the version verb
	CtxVersionFlg                    // --version, never shows
	CtxUpdateDone                    // after a successful update
)

Print contexts the caller can report.

type LogoSize

type LogoSize int

LogoSize picks one of the three renderings.

const (
	LogoCompact LogoSize = iota
	LogoMedium
	LogoWordmark
)

The three logo renderings.

func ShowLogo(s LogoState) (LogoSize, bool)

ShowLogo reports whether to print a logo and which size.

type LogoState

type LogoState struct {
	Context        LogoContext
	JSON           bool
	CI             bool
	IsTTY          bool
	NoBanner       bool   // --no-banner or LEVELRAIL_NO_BANNER
	FirstRunDone   bool   // a banner or setup already ran on this machine
	LastLogoShown  string // version the post update logo was last shown for
	CurrentVersion string
}

LogoState is everything the decision needs. The caller reads files and env.

type Lookup

type Lookup func(string) (string, bool)

Lookup reads one environment variable, like os.LookupEnv.

type Mode

type Mode int

Mode is the color depth a stream can show.

const (
	ModeNone Mode = iota
	Mode16
	Mode256
	ModeTrueColor
)

Color depths from none to 24 bit.

func DetectMode

func DetectMode(lookup Lookup, w io.Writer, opts Options) Mode

DetectMode decides the color depth for w. Only a *os.File can be a terminal.

func DetectModeTTY

func DetectModeTTY(lookup Lookup, isTTY bool, opts Options) Mode

DetectModeTTY is the pure core of DetectMode.

func (Mode) String

func (m Mode) String() string

type Options

type Options struct {
	NoColor bool // --no-color
	JSON    bool // --json always means no color
	CI      bool // resolved CI mode
	// GOOS is runtime.GOOS when empty. Tests set it.
	GOOS string
}

Options are the caller side switches (flags and resolved CI mode).

type Role

type Role int

Role is a semantic style. Callers always pair it with a word or symbol.

const (
	RoleOK Role = iota
	RoleWarn
	RoleFail
	RoleHint
	RoleMuted
	RoleBrand
	RoleAccent
)

Semantic roles.

func Roles

func Roles() []Role

Roles lists every role in display order.

type RoleStyle

type RoleStyle struct {
	RGB    string // optional "rrggbb" for truecolor, else the 256 index is used
	C256   int
	ANSI16 int // full SGR parameter, for example 32 or 90
	Bold   bool
	Dim    bool
}

RoleStyle is one role in a theme. A zero C256 or ANSI16 means "no foreground color at that depth", so a role can be only bold or only dim.

type Styler

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

Styler paints text for one stream. The zero value paints nothing.

func New

func New(mode Mode, light bool) Styler

New returns a Styler. light picks the light background brand shade, the default (dark) is used when the background is unknown.

func (Styler) Accent

func (s Styler) Accent(t string) string

Accent paints t with the accent role.

func (Styler) Brand

func (s Styler) Brand(t string) string

Brand paints t with the brand role.

func (Styler) Fail

func (s Styler) Fail(t string) string

Fail paints t with the fail role.

func (Styler) Hint

func (s Styler) Hint(t string) string

Hint paints t with the hint role.

func (Styler) Mode

func (s Styler) Mode() Mode

Mode is the depth this Styler paints at.

func (Styler) Muted

func (s Styler) Muted(t string) string

Muted paints t with the muted role.

func (Styler) OK

func (s Styler) OK(t string) string

OK paints t with the ok role.

func (Styler) ThemeName

func (s Styler) ThemeName() string

ThemeName is the active theme name.

func (Styler) Warn

func (s Styler) Warn(t string) string

Warn paints t with the warn role.

func (Styler) WithTheme

func (s Styler) WithTheme(t Theme) Styler

WithTheme returns a copy that paints with t. It never changes the color mode, so NO_COLOR, non-TTY, --json and CI stay off whatever the theme.

type Theme

type Theme struct {
	Name        string
	Description string
	Roles       map[Role]RoleStyle
	// BrandLight replaces the brand role on a light background.
	BrandLight RoleStyle
}

Theme is a named palette over the semantic roles.

func LookupTheme

func LookupTheme(name string) (Theme, bool)

LookupTheme finds a theme by case-insensitive name.

func Themes

func Themes() []Theme

Themes returns the built-in themes in registry order.

func (Theme) Role

func (t Theme) Role(r Role, light bool) RoleStyle

Role returns the style for r, with the light background brand shade when light.

type ThemeChoice

type ThemeChoice struct {
	Theme  Theme
	Source string // flag, env, account, saved or default
	// Unknown is the rejected name when a source named a theme that does not exist.
	Unknown string
}

ThemeChoice is a resolved theme and where it came from.

func ResolveTheme

func ResolveTheme(flag string, lookup Lookup, account, saved string) ThemeChoice

ResolveTheme picks the first non-empty of flag, LEVELRAIL_THEME, the cached account theme and the locally saved theme. An unknown name falls back to default and is reported in Unknown. Theme choice never changes the color mode, see Styler.

Jump to

Keyboard shortcuts

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