color

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 6 Imported by: 1

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

Examples

Constants

This section is empty.

Variables

View Source
var ErrIllegalMode = errors.New("illegal color-mode")

ErrIllegalMode will be returned in situations where illegal values or representations if a Mode are provided.

View Source
var ErrIllegalSupport = errors.New("illegal color-support")

ErrIllegalSupport will be returned in situations where illegal values or representations if a Supported are provided.

SupportAssumptionDetections holds all global registered SupportAssumptionDetection variants that should be used to discover color the support.

Functions

func CanSupportBeAssumed added in v0.9.0

func CanSupportBeAssumed() (bool, error)

CanSupportBeAssumed returns true if the support of color can be assumed.

func SupportAssumptionDetectionGithubActions added in v0.9.0

func SupportAssumptionDetectionGithubActions() (bool, error)

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

func SupportAssumptionDetectionGitlabCi() (bool, error)

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

func SupportAssumptionDetectionIntellij() (bool, error)

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

func (instance Mode) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (*Mode) Set

func (instance *Mode) Set(plain string) error

Set will set this instance to the given plain value or errors.

func (Mode) ShouldColorize added in v0.9.0

func (instance Mode) ShouldColorize(checking Supported) bool

ShouldColorize will check for the given combination of this instance and the given Support value if the output should be colorized.

func (Mode) String

func (instance Mode) String() string

String prints out a meaningful representation of this instance.

func (*Mode) UnmarshalText

func (instance *Mode) UnmarshalText(text []byte) error

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.

func AllModes added in v0.9.0

func AllModes() Modes

AllModes returns all possible values of Mode.

func (Modes) String added in v0.9.0

func (instance Modes) String() string

String returns a meaningful representation of this instance.

func (Modes) Strings added in v0.9.0

func (instance Modes) Strings() []string

Strings returns a meaningful representation of all of it's values.

type SupportAssumptionDetection

type SupportAssumptionDetection func() (bool, error)

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

func DetectSupportForWriter(w io.Writer) (prepared io.Writer, supported Supported, err error)

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

func (instance Supported) IsSupported() bool

IsSupported returns true when color should most likely work.

func (Supported) MarshalText added in v0.9.0

func (instance Supported) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (*Supported) Set added in v0.9.0

func (instance *Supported) Set(plain string) error

Set will set this instance to the given plain value or errors.

func (Supported) String added in v0.9.0

func (instance Supported) String() string

String prints out a meaningful representation of this instance.

func (*Supported) UnmarshalText added in v0.9.0

func (instance *Supported) UnmarshalText(text []byte) error

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.

func (Supports) String added in v0.9.0

func (instance Supports) String() string

String returns a meaningful representation of this instance.

func (Supports) Strings added in v0.9.0

func (instance Supports) Strings() []string

Strings returns a meaningful representation of all of it's values.

Jump to

Keyboard shortcuts

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