ttycolor

package module
v0.0.0-...-c7d7dcd Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 3 Imported by: 91

README

ttycolor

Conditionally expose ANSI color codes for use on a terminal

This code is suitable for use in CockroachDB as it satisfies the following requirements:

  1. it should abstract the color numeric codes into symbolic names
  2. it must test the right file descriptor (either stderr or stdout depending on caller requirements)
  3. it must be conservative about which color codes are sent to which terminal type. Not every terminal supports the same colors.

Other Go packages for terminal colors typically fail to satisfy requirements 2 or 3, or both.

Documentation

Overview

Package color is a lightweight terminal color library. It is only compatible with terminals which support ANSI escape codes.

The API exposes two programming styles. You can use functions that print the color escape sequences directly to the output stream:

color.Stdout(color.Red)
fmt.Println("Red text")
color.Stdout(color.Reset)

Or, you can retrieve the color escape sequence by directly looking up a color.Code in a color.Profile:

fmt.Printf("%sRed text%s\n", color.StdoutProfile[color.Red],
  color.StdoutProfile[color.Reset])

Text destined for stderr should use color.Stderr and color.StderrProfile instead.

When the output stream is not a character device, or the TERM environment variable indicates an unsupported terminal type, functions will print nothing or return a nil byte slice.

While we link the main CockroachDB binary against libncurses or libterminfo, which bundle information about nearly every terminal ever produced, handling colors for non-ANSI terminals is no small task. See, for example, the difference between the setaf (set ANSI foreground) and setf (set foreground) capabilities. Since non-ANSI terminals are few and far between, it's not worth the trouble.

Index

Constants

This section is empty.

Variables

View Source
var Profile256 = Profile{

	Black:   []byte("\033[38;5;0m"),
	Red:     []byte("\033[38;5;160m"),
	Green:   []byte("\033[38;5;2m"),
	Yellow:  []byte("\033[38;5;214m"),
	Blue:    []byte("\033[38;5;4m"),
	Magenta: []byte("\033[38;5;5m"),
	Cyan:    []byte("\033[38;5;33m"),
	White:   []byte("\033[38;5;315"),
	Gray:    []byte("\033[38;5;246m"),
	Reset:   []byte("\033[0m"),
}

Profile256 is suitable for terminals with 256-color support.

View Source
var Profile8 = Profile{

	Black:   []byte("\033[0;30;49m"),
	Red:     []byte("\033[0;31;49m"),
	Green:   []byte("\033[0;32;49m"),
	Yellow:  []byte("\033[0;33;49m"),
	Blue:    []byte("\033[0;34;49m"),
	Magenta: []byte("\033[0;35;49m"),
	Cyan:    []byte("\033[0;36;49m"),
	White:   []byte("\033[0;37;49m"),
	Gray:    []byte("\033[2;37;49m"),
	Reset:   []byte("\033[0m"),
}

Profile8 is suitable for terminals with 8-color support.

View Source
var StderrProfile = detectProfileOrPanic(os.Stderr)

StderrProfile is the Profile to use for stderr.

View Source
var StdoutProfile = detectProfileOrPanic(os.Stdout)

StdoutProfile is the Profile to use for stdout.

Functions

func Stderr

func Stderr(code Code)

Stderr sets the color for future output to os.Stderr.

func Stdout

func Stdout(code Code)

Stdout sets the color for future output to os.Stdout.

Types

type Code

type Code int

Code represents a terminal color code.

const (
	Black Code = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	Gray
	Reset
)

Color codes.

func PickArbitraryColor

func PickArbitraryColor(input uint32) Code

PickArbitraryColor maps a uint32 to an arbitrary color code (excluding Reset) in a deterministic fashion.

type Profile

type Profile map[Code][]byte

Profile defines escape sequences which provide color in terminals. Some terminals support 8 colors, some 256, others none at all.

func DetectProfile

func DetectProfile(f *os.File) (Profile, error)

DetectProfile configures a profile suitable for the given file output.

func (Profile) BackgroundColorSequence

func (cp Profile) BackgroundColorSequence(code Code) []byte

BackgroundColorSequence returns the corresponding color in the profile as a background color escape sequence.

Jump to

Keyboard shortcuts

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