color

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: BSD-3-Clause Imports: 12 Imported by: 2

Documentation

Overview

Package color provides template functions for adding color to the command line output. It automatically detects whether the terminal supports color, and it contains conventions-based approaches for allowing the user to control whether color is used

Index

Constants

View Source
const (
	// FlagFeature enables the flag --color={auto|never|always} for enabling color.
	// The flag's value is optional, in which case its value is always
	FlagFeature = Feature(1 << iota)

	// NoFlagFeature enables the flag --no-color for disabling color
	NoFlagFeature

	// ModeFeature specifies that the --color flag accepts an optional value
	// that corresponds to the Mode value, either "auto", "always", or "never"
	// indicating how to handle color.
	ModeFeature

	// TemplateFuncs enables the template funcs feature, which provides template funcs
	// for colors and styles
	TemplateFuncs

	// AllFeatures enables all of the features.  This is the default
	AllFeatures = -1
)

Variables

This section is empty.

Functions

func NewTemplateFuncs added in v0.13.0

func NewTemplateFuncs(modeopt ...Mode) map[string]any

NewTemplateFuncs gets the template funcs that support the given mode

func RegisterTemplateFuncs

func RegisterTemplateFuncs(modeopt ...Mode) cli.Action

RegisterTemplateFuncs sets up the template funcs which can be used to activate color and styles. The common use is to pipe to the format function which has the same name as any of the Color and Style constants:

{{ "Text to make bold" | Bold }}

func SetMode

func SetMode(modeopt ...Mode) cli.Action

SetMode returns an action that sets the color mode. If specified on a flag or argument, it provides the action for a Boolean or Mode value that controls whether color is set. The flag or arg must have Value that is either *bool or *Mode. The initializer sets *Mode if it is unset. If the argument modeopt is specified, the value will be used; otherwise, it will be obtained from the context.

func SourceAnnotation

func SourceAnnotation() (string, string)

SourceAnnotation gets the name and value of the annotation added to the Data of all flags that are initialized from this package

func SynopsisColor added in v0.17.0

func SynopsisColor(c cli.Color) cli.Action

SynopsisColor returns an action, typically added to the Uses pipeline of a flag, command, or expression, that causes its name to be rendered in the synopsis using the given foreground color instead of the default Bold style. It sets a private data key that the synopsis package reads.

&cli.Flag{Uses: color.SynopsisColor(cli.Red)}

func SynopsisStyle added in v0.17.0

func SynopsisStyle(s cli.Style) cli.Action

SynopsisStyle returns an action, typically added to the Uses pipeline of a flag, command, or expression, that causes its name to be rendered in the synopsis using the given style instead of the default Bold style. It sets a private data key that the synopsis package reads.

&cli.Flag{Uses: color.SynopsisStyle(cli.Underline)}

Types

type ContextFilter added in v0.12.0

type ContextFilter int

ContextFilter provides the context filters in this package

const (
	// Defines provides a context filter for flags defined by this package
	Defines ContextFilter = 0
)

func (ContextFilter) Describe added in v0.12.0

func (f ContextFilter) Describe() string

Describe produces a representation of the context filter suitable for use in messages

func (ContextFilter) MarshalText added in v0.12.0

func (f ContextFilter) MarshalText() ([]byte, error)

MarshalText provides the textual representation

func (ContextFilter) Matches added in v0.12.0

func (f ContextFilter) Matches(ctx context.Context) bool

func (ContextFilter) String added in v0.12.0

func (f ContextFilter) String() string

String produces a textual representation of the context filter

func (*ContextFilter) UnmarshalText added in v0.12.0

func (f *ContextFilter) UnmarshalText(b []byte) error

UnmarshalText converts the textual representation

type Emoji

type Emoji string

Emoji represents emoji

const (
	Tada           Emoji = "🎉"
	Fire           Emoji = "🔥"
	Sparkles       Emoji = "✨"
	Exclamation    Emoji = "❗"
	Bulb           Emoji = "💡"
	X              Emoji = "❌"
	HeavyCheckMark Emoji = "✔️"
	Warning        Emoji = "⚠️"
	Play           Emoji = "▶"
)

Emoji constants

type Feature

type Feature int

Feature provides a name for each feature in the extension

func (Feature) Pipeline

func (f Feature) Pipeline() cli.Action

Pipeline converts the value into a pipeline

type Mode

type Mode int

Mode enumerates the color mode: on, off, or auto-detect

const (
	// Auto will enable color depending upon whether stdout is detected as TTY.
	Auto Mode = iota

	// Always enable terminal color
	Always

	// Never enable terminal color
	Never
)

func (*Mode) Set

func (m *Mode) Set(arg string) error

func (Mode) String

func (m Mode) String() string

func (*Mode) Synopsis

func (*Mode) Synopsis() string

type Options

type Options struct {
	// Features enumerates which features to use
	Features Feature
}

Options is the configuration for the color extension and provides the initializer for the app initialization pipeline

func (Options) Execute

func (o Options) Execute(c context.Context) error

Execute implements the action interface

func (Options) Pipeline added in v0.14.0

func (o Options) Pipeline() cli.Action

Pipeline converts the value into a pipeline

Jump to

Keyboard shortcuts

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