From 166cf3773788aab7e9bf5e34d8c0deb176b92bc8 Mon Sep 17 00:00:00 2001 From: Salah Eddine Taouririt Date: Sun, 29 Aug 2021 12:09:49 +0100 Subject: [PATCH] Implement the Stringer interface for ansi color types --- color.go | 12 ++++++++++++ examples/hello-world/main.go | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/color.go b/color.go index 2019c73..08cc8c7 100644 --- a/color.go +++ b/color.go @@ -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 diff --git a/examples/hello-world/main.go b/examples/hello-world/main.go index 26d063f..2637868 100644 --- a/examples/hello-world/main.go +++ b/examples/hello-world/main.go @@ -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()) }