colors

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package colors contains color-related utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enabled

func Enabled() bool

Enabled returns whether it is ok to write colorized output to stdout or stderr. It's true only if all the following conditions are met:

  • The environment variable NO_COLOR is not set.
  • The environment variable TERM is not equal to "dumb".
  • stdout and stderr are both terminals.

func FullDim

func FullDim(prev, row []string) []bool

FullDim dims any columns that are identical in prev.

func NoDim

func NoDim(prev, row []string) []bool

NoDim doesn't dim any columns.

func PrefixDim

func PrefixDim(prev, row []string) []bool

PrefixDim dims the longest prefix of row that is identical to prev.

Types

type Atom

type Atom struct {
	S         string // the text
	Underline bool   // is it underlined?
	Bold      bool   // is it bold?
	Color     Code   // text color
}

An Atom is a segment of text with a single formatting style.

func (Atom) String

func (a Atom) String() string

String returns the atom with the appropriate ANSI escape sequences.

type Code

type Code string

Code represents an ANSI escape code for colors and formatting.

const (
	Reset     Code = "\x1b[0m" // The ANSI escape code that resets formatting.
	Bold      Code = "\x1b[1m" // The ANSI escape code for bold text.
	Underline Code = "\x1b[4m" // The ANSI escape code for underlining.
)

func Color256

func Color256(i byte) Code

Color256 returns the ANSI escape code for the provided 256 (a.k.a. 8-bit) foreground color. See 1 for details and a depiction of the color space.

func ColorHash

func ColorHash(s string) Code

ColorHash returns a 256 color based on the hash of the provided string.

type Tabularizer

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

A Tabularizer produces pretty-printed tabularized text. Unlike tabwriter.Writer 1, Tabularizer properly handles text with ANSI escape codes. Here's what an example table looks like:

╭───────────────────────╮
│ CATS                  │
├────────┬─────┬────────┤
│ NAME   │ AGE │ COLOR  │
├────────┼─────┼────────┤
│ belle  │ 1y  │ tortie │
│ sidney │ 2y  │ calico │
│ dakota │ 8m  │ tuxedo │
╰────────┴─────┴────────╯

The table format comes from duf 2.

func NewTabularizer

func NewTabularizer(w io.Writer, title []Text, dim func(prev, row []string) []bool) *Tabularizer

NewTabularizer returns a new tabularizer. The provided dim function determines which columns in a row, if any, are dimmed.

func (*Tabularizer) Flush

func (t *Tabularizer) Flush()

Flush writes all buffered rows. Flush should only be called once, after all rows have been written.

func (*Tabularizer) Row

func (t *Tabularizer) Row(values ...any)

Row buffers a new Row to be tabularized. The Row isn't written until Flush is called. Note that every Row reported to a tabularizer must be the same length. A value can be a text, atom, string, or fmt.Stringer.

type Text

type Text []Atom

Text represents a contiguous sequence of atoms.

func (Text) String

func (t Text) String() string

String returns the text with the appropriate ANSI escape sequences.

Jump to

Keyboard shortcuts

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