Skip to content

Commit

Permalink
Implement the Stringer interface for ansi color types
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrsalah authored and muesli committed Aug 30, 2021
1 parent 1d2849b commit 166cf37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions color.go
Expand Up @@ -26,12 +26,24 @@ type Color interface {

type NoColor struct{}

func (nc 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]
}

// ANSI256Color is a color (16-255) as defined by the ANSI Standard.
type ANSI256Color int

func (a ANSI256Color) String() string {
return ansiHex[a]
}

// RGBColor is a hex-encoded color, e.g. "#abcdef".
type RGBColor string

Expand Down
4 changes: 3 additions & 1 deletion examples/hello-world/main.go
Expand Up @@ -37,5 +37,7 @@ func main() {
termenv.String("gray").Foreground(p.Color("0")).Background(p.Color("#B9BFCA")),
)

fmt.Printf("\n\t%s %t\n", termenv.String("Has dark background?").Bold(), termenv.HasDarkBackground())
fmt.Printf("\n\t%s %s\n", termenv.String("Has foreground color").Bold(), termenv.ForegroundColor())
fmt.Printf("\t%s %s\n", termenv.String("Has background color").Bold(), termenv.BackgroundColor())
fmt.Printf("\t%s %t\n", termenv.String("Has dark background?").Bold(), termenv.HasDarkBackground())
}

0 comments on commit 166cf37

Please sign in to comment.