diff --git a/CHANGELOG.md b/CHANGELOG.md index ba73ceb7..ff2543a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 12.88.0 * `[pager]` Added new package for pager (`less`/`more`) setup +* `[fmtc]` Added method `IsColorsSupported` ### 12.87.0 diff --git a/fmtc/examples_test.go b/fmtc/examples_test.go index 09d68a1b..1d1c08e4 100644 --- a/fmtc/examples_test.go +++ b/fmtc/examples_test.go @@ -337,6 +337,10 @@ func ExampleClean() { // Output: Text } +func ExampleIsColorsSupported() { + fmt.Printf("16 Colors Supported: %t\n", IsColorsSupported()) +} + func ExampleIs256ColorsSupported() { fmt.Printf("256 Colors Supported: %t\n", Is256ColorsSupported()) } diff --git a/fmtc/fmtc.go b/fmtc/fmtc.go index 0a68225a..0309552f 100644 --- a/fmtc/fmtc.go +++ b/fmtc/fmtc.go @@ -75,8 +75,9 @@ var DisableColors = os.Getenv("NO_COLOR") != "" // ////////////////////////////////////////////////////////////////////////////////// // -var colors256Supported bool -var colorsTCSupported bool +var colorsSupported bool // 16 colors support +var colors256Supported bool // 256 colors support +var colorsTCSupported bool // 24bit (TrueColor) colors support var colorsSupportChecked bool var colorsMap *sync.Map @@ -328,6 +329,17 @@ func Bell() { fmt.Print(_CODE_BELL) } +// IsColorsSupported returns true if 16 colors is supported by terminal +func IsColorsSupported() bool { + if colorsSupportChecked { + return colorsSupported + } + + checkForColorsSupport() + + return colorsSupported +} + // Is256ColorsSupported returns true if 256 colors is supported by terminal func Is256ColorsSupported() bool { if colorsSupportChecked { @@ -644,6 +656,13 @@ func isValidNamedTag(tag string) bool { } func checkForColorsSupport() { + switch { + case strings.Contains(term, "xterm"), + strings.Contains(term, "color"), + term == "screen": + colorsSupported = true + } + if strings.Contains(term, "256color") { colors256Supported = true } diff --git a/fmtc/fmtc_test.go b/fmtc/fmtc_test.go index ef3f7d8e..0e8775e5 100644 --- a/fmtc/fmtc_test.go +++ b/fmtc/fmtc_test.go @@ -146,6 +146,7 @@ func (s *FormatSuite) Test24BitColors(c *C) { c.Assert(IsTrueColorSupported(), Equals, true) c.Assert(Is256ColorsSupported(), Equals, true) + c.Assert(IsColorsSupported(), Equals, true) colorsSupportChecked = false colors256Supported = false