Skip to content

Commit

Permalink
Add godoc style comments
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jan 31, 2022
1 parent 919ab49 commit 55e6225
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions ansicolors.go
@@ -1,5 +1,6 @@
package termenv

// ANSI color codes
const (
ANSIBlack ANSIColor = iota
ANSIRed
Expand Down
25 changes: 20 additions & 5 deletions color.go
Expand Up @@ -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) {
Expand All @@ -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{}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions screen.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// Sequence definitions.
const (
// Cursor positioning.
CursorUpSeq = "%dA"
Expand Down
1 change: 1 addition & 0 deletions style.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mattn/go-runewidth"
)

// Sequence definitions.
const (
ResetSeq = "0"
BoldSeq = "1"
Expand Down
10 changes: 9 additions & 1 deletion termenv.go
Expand Up @@ -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
)

Expand Down

0 comments on commit 55e6225

Please sign in to comment.