diff --git a/ansicolors.go b/ansicolors.go index e82eaef..ee303e2 100644 --- a/ansicolors.go +++ b/ansicolors.go @@ -1,5 +1,6 @@ package termenv +// ANSI color codes const ( ANSIBlack ANSIColor = iota ANSIRed diff --git a/color.go b/color.go index 08cc8c7..7cf7dae 100644 --- a/color.go +++ b/color.go @@ -12,41 +12,48 @@ import ( ) var ( + // ErrInvalidColor gets returned when a color is invalid. ErrInvalidColor = errors.New("invalid color") ) +// Foreground and Background sequence codes const ( Foreground = "38" Background = "48" ) +// Color is an interface implemented by all colors that can be converted to an +// ANSI sequence. type Color interface { + // Sequence returns the ANSI Sequence for the color. Sequence(bg bool) string } +// NoColor is a nop for terminals that don't support colors. type NoColor struct{} -func (nc NoColor) String() string { +func (c NoColor) String() string { return "" } // ANSIColor is a color (0-15) as defined by the ANSI Standard. type ANSIColor int -func (a ANSIColor) String() string { - return ansiHex[a] +func (c ANSIColor) String() string { + return ansiHex[c] } // ANSI256Color is a color (16-255) as defined by the ANSI Standard. type ANSI256Color int -func (a ANSI256Color) String() string { - return ansiHex[a] +func (c ANSI256Color) String() string { + return ansiHex[c] } // RGBColor is a hex-encoded color, e.g. "#abcdef". type RGBColor string +// ConvertToRGB converts a Color to a colorful.Color. func ConvertToRGB(c Color) colorful.Color { var hex string switch v := c.(type) { @@ -62,6 +69,7 @@ func ConvertToRGB(c Color) colorful.Color { return ch } +// Convert transforms a given Color to a Color supported within the Profile. func (p Profile) Convert(c Color) Color { if p == Ascii { return NoColor{} @@ -95,6 +103,8 @@ func (p Profile) Convert(c Color) Color { return c } +// Color creates a Color from a string. Valid inputs are hex colors, as well as +// ANSI color codes (0-15, 16-255). func (p Profile) Color(s string) Color { if len(s) == 0 { return nil @@ -119,15 +129,18 @@ func (p Profile) Color(s string) Color { return p.Convert(c) } +// FromColor creates a Color from a color.Color. func (p Profile) FromColor(c color.Color) Color { col, _ := colorful.MakeColor(c) return p.Color(col.Hex()) } +// Sequence returns the ANSI Sequence for the color. func (c NoColor) Sequence(bg bool) string { return "" } +// Sequence returns the ANSI Sequence for the color. func (c ANSIColor) Sequence(bg bool) string { col := int(c) bgMod := func(c int) int { @@ -143,6 +156,7 @@ func (c ANSIColor) Sequence(bg bool) string { return fmt.Sprintf("%d", bgMod(col-8)+90) } +// Sequence returns the ANSI Sequence for the color. func (c ANSI256Color) Sequence(bg bool) string { prefix := Foreground if bg { @@ -151,6 +165,7 @@ func (c ANSI256Color) Sequence(bg bool) string { return fmt.Sprintf("%s;5;%d", prefix, c) } +// Sequence returns the ANSI Sequence for the color. func (c RGBColor) Sequence(bg bool) string { f, err := colorful.Hex(string(c)) if err != nil { diff --git a/screen.go b/screen.go index b5355e1..c62b6d7 100644 --- a/screen.go +++ b/screen.go @@ -5,6 +5,7 @@ import ( "strings" ) +// Sequence definitions. const ( // Cursor positioning. CursorUpSeq = "%dA" diff --git a/style.go b/style.go index 8153ea0..577aa29 100644 --- a/style.go +++ b/style.go @@ -7,6 +7,7 @@ import ( "github.com/mattn/go-runewidth" ) +// Sequence definitions. const ( ResetSeq = "0" BoldSeq = "1" diff --git a/termenv.go b/termenv.go index 7fc3bbe..9161d99 100644 --- a/termenv.go +++ b/termenv.go @@ -8,18 +8,26 @@ import ( ) var ( + // ErrStatusReport gets returned when the terminal can't be queried. ErrStatusReport = errors.New("unable to retrieve status report") ) +// Profile is a color profile: Ascii, ANSI, ANSI256, or TrueColor. type Profile int const ( + // Control Sequence Introducer CSI = "\x1b[" + // Operating System Command OSC = "\x1b]" - Ascii = Profile(iota) + // Ascii, uncolored profile. + Ascii = Profile(iota) //nolint:revive + // ANSI, 4-bit color profile ANSI + // ANSI256, 8-bit color profile ANSI256 + // TrueColor, 24-bit color profile TrueColor )