Documentation ¶
Overview ¶
Package color provides function to detect color support and also describe how the application should behave.
Please see DetectSupportForWriter(...) for details how a detection usually works.
Index ¶
- Variables
- func CanSupportBeAssumed() (bool, error)
- func SupportAssumptionDetectionGithubActions() (bool, error)
- func SupportAssumptionDetectionGitlabCi() (bool, error)
- func SupportAssumptionDetectionIntellij() (bool, error)
- type Mode
- type ModeAware
- type ModeMutableAware
- type Modes
- type SupportAssumptionDetection
- type Supported
- type Supports
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIllegalMode = errors.New("illegal color-mode")
ErrIllegalMode will be returned in situations where illegal values or representations if a Mode are provided.
var ErrIllegalSupport = errors.New("illegal color-support")
ErrIllegalSupport will be returned in situations where illegal values or representations if a Supported are provided.
var SupportAssumptionDetections = []SupportAssumptionDetection{ SupportAssumptionDetectionIntellij, SupportAssumptionDetectionGitlabCi, SupportAssumptionDetectionGithubActions, }
SupportAssumptionDetections holds all global registered SupportAssumptionDetection variants that should be used to discover color the support.
Functions ¶
func CanSupportBeAssumed ¶ added in v0.9.0
CanSupportBeAssumed returns true if the support of color can be assumed.
func SupportAssumptionDetectionGithubActions ¶ added in v0.9.0
SupportAssumptionDetectionGithubActions returns true if this application is executed in the context of a GitHub Actions run (https://docs.github.com/en/free-pro-team@latest/actions).
See: https://docs.gitlab.com/ee/ci/variables/#list-all-environment-variables
func SupportAssumptionDetectionGitlabCi ¶
SupportAssumptionDetectionGitlabCi returns true if this application is executed in the context of a GitLabCI run (https://docs.gitlab.com/ee/ci/).
See: https://docs.gitlab.com/ee/ci/variables/#list-all-environment-variables
func SupportAssumptionDetectionIntellij ¶
SupportAssumptionDetectionIntellij returns true if this application is executed in the context of the IntelliJ IDEA framework (https://jetbrains.com/idea) or derivatives.
See: https://stackoverflow.com/questions/61920425/intellij-terminal-environment-variable-set-global
Types ¶
type Mode ¶
type Mode uint8
Mode defines how colors should be used.
const ( // ModeAuto will result in that the application tries to detect // automatically if it is possible and meaningful to use colors in the // a given terminal. ModeAuto Mode = 0 // ModeAlways tells the application to always use colorful output (if // supported). ModeAlways Mode = 1 // ModeNever tells the application to never use colorful output. ModeNever Mode = 2 )
func (Mode) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (Mode) ShouldColorize ¶ added in v0.9.0
ShouldColorize will check for the given combination of this instance and the given Support value if the output should be colorized.
func (*Mode) UnmarshalText ¶
UnmarshalText implements encoding.TextMarshaler
type ModeAware ¶
type ModeAware interface { // GetColorMode returns the current Mode. GetColorMode() Mode }
ModeAware describes an object that is aware of a Mode and exports its current state.
type ModeMutableAware ¶ added in v1.4.0
type ModeMutableAware interface { ModeAware // SetColorMode modifies the current Mode to the given one. SetColorMode(Mode) }
ModeMutableAware is similar to ModeAware but additionally is able to modify the Mode by calling SetColorMode(Mode).
type Modes ¶ added in v0.9.0
type Modes []Mode
Modes is a multiple version of Mode.
type SupportAssumptionDetection ¶
SupportAssumptionDetection is a function that detects for the current environment if color support can be assumed. This can for example be done in if this application runs in the context of a GitLabCi run, inside an IDE, ...
type Supported ¶ added in v0.9.0
type Supported uint8
Supported expresses if color is supported or not in the current context.
const ( // SupportedNone clearly says that color is not supported and cannot be // used. SupportedNone Supported = 0 // SupportedNative expresses that color is natively supported and most // likely guaranteed to be working. SupportedNative Supported = 1 // SupportedAssumed expresses that color is assumed to work, but there is no // guarantee that this will really work. Most likely this is discovered // based on environment variables that expresses the existence of some // assumed environment, ... SupportedAssumed Supported = 2 )
func DetectSupportForWriter ¶
DetectSupportForWriter detects for the given io.Writer if color is supported or not. Additionally it prepares the given logger with the color mode and will return a modified instance of it. If color is not supported the original io.Writer is still returned. Errors are only returned in cases where something bad happens while detecting or preparing for color.
See SupportAssumptionDetections for assumed detections.
Example (Detection) ¶
package main import ( "os" "github.com/echocat/slf4g/native/color" ) func main() { prepared, supported, err := color.DetectSupportForWriter(os.Stderr) if err != nil { panic(err) } msg := []byte("Hello, world!") if supported.IsSupported() { msg = colorize(msg) } _, _ = prepared.Write(msg) } //goland:noinspection GoTestName func Examplecolorize() {} func colorize(_ []byte) []byte { panic("should never be called.") }
Output:
func (Supported) IsSupported ¶ added in v0.9.0
IsSupported returns true when color should most likely work.
func (Supported) MarshalText ¶ added in v0.9.0
MarshalText implements encoding.TextMarshaler
func (*Supported) Set ¶ added in v0.9.0
Set will set this instance to the given plain value or errors.
func (Supported) String ¶ added in v0.9.0
String prints out a meaningful representation of this instance.
func (*Supported) UnmarshalText ¶ added in v0.9.0
UnmarshalText implements encoding.TextMarshaler
type Supports ¶ added in v0.9.0
type Supports []Supported
Supports is a multiple version of Supported.
func AllSupports ¶ added in v0.9.0
func AllSupports() Supports
AllSupports returns all possible values of Supported.