From 07bea24d5a5cfb9d04fab4e01f89957f16db17af Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 1 Nov 2023 14:20:49 +0300 Subject: [PATCH 1/2] [fmtc] Fixed 'IsTag' compatibility with sequence of tags --- CHANGELOG.md | 5 +++++ fmtc/fmtc.go | 10 ++++------ fmtc/fmtc_test.go | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31ec3fa2..75988b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog +### 12.83.2 + +* `[fmtc]` Fixed `IsTag` compatibility with sequence of tags (e.g. `{*}{_}{r}`) +* `[fmtc]` Fixed bug in `Clean` with writing reset escape sequence if there is no reset tag in the given string + ### 12.83.1 * `[protip]` Disabling tips using environment variable (`PROTIP=0`) diff --git a/fmtc/fmtc.go b/fmtc/fmtc.go index 72e70d89..8e0efcda 100644 --- a/fmtc/fmtc.go +++ b/fmtc/fmtc.go @@ -344,8 +344,8 @@ func IsTrueColorSupported() bool { return colorsTCSupported } -// IsTag checks if the given value is valid color tag and can be encoded -// to escape sequence +// IsTag tests whether the given value is a valid color tag (or sequence +// of tags) and can be encoded into an escape sequence func IsTag(tag string) bool { if tag == "" { return true // Empty value is valid color tag ¯\_(ツ)_/¯ @@ -355,9 +355,7 @@ func IsTag(tag string) bool { return false } - tag = tag[1 : len(tag)-1] - - return isValidTag(tag) + return Clean(tag) == "" } // ////////////////////////////////////////////////////////////////////////////////// // @@ -542,7 +540,7 @@ func searchColors(text string, limit int, clean bool) string { } } - if !closed { + if !closed && !clean { output.WriteString(_CODE_RESET) } diff --git a/fmtc/fmtc_test.go b/fmtc/fmtc_test.go index 86359c76..ef3f7d8e 100644 --- a/fmtc/fmtc_test.go +++ b/fmtc/fmtc_test.go @@ -93,6 +93,7 @@ func (s *FormatSuite) TestIsTag(c *C) { c.Assert(IsTag("{r}"), Equals, true) c.Assert(IsTag("{r*}"), Equals, true) c.Assert(IsTag("{#123}"), Equals, true) + c.Assert(IsTag("{*}{_}{#123}"), Equals, true) c.Assert(IsTag("{%123}"), Equals, true) c.Assert(IsTag("{*}"), Equals, true) c.Assert(IsTag("{w-}"), Equals, true) From e0491e0d2d5683057b15a081df51479f86a1480c Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 1 Nov 2023 15:31:52 +0300 Subject: [PATCH 2/2] [fmtutil/panel] Improve panel rendering with disabled colors --- CHANGELOG.md | 1 + ek.go | 2 +- fmtutil/panel/panel.go | 14 ++++++++++---- fmtutil/panel/panel_test.go | 12 ++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75988b22..a8615de6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 12.83.2 +* `[fmtutil/panel]` Improved panel rendering with disabled colors * `[fmtc]` Fixed `IsTag` compatibility with sequence of tags (e.g. `{*}{_}{r}`) * `[fmtc]` Fixed bug in `Clean` with writing reset escape sequence if there is no reset tag in the given string diff --git a/ek.go b/ek.go index 4b7de9f7..cc03ef4e 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.83.1" +const VERSION = "12.83.2" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/fmtutil/panel/panel.go b/fmtutil/panel/panel.go index e7b363a8..796fa5d9 100644 --- a/fmtutil/panel/panel.go +++ b/fmtutil/panel/panel.go @@ -135,10 +135,16 @@ func renderPanel(label, colorTag, title, message string, options Options) { fmtc.NewLine() } - if options.Has(LABEL_POWERLINE) { - fmtc.Printf(colorTag+indent+"{@*} %s {!}"+colorTag+"{!} "+colorTag+"%s{!}", label, title) + labelFormat := "{@*} %s {!}" + + if fmtc.DisableColors { + labelFormat = "[%s]" + } + + if options.Has(LABEL_POWERLINE) && !fmtc.DisableColors { + fmtc.Printf(colorTag+indent+labelFormat+colorTag+"{!} "+colorTag+"%s{!}", label, title) } else { - fmtc.Printf(colorTag+indent+"{@*} %s {!} "+colorTag+"%s{!}", label, title) + fmtc.Printf(colorTag+indent+labelFormat+colorTag+" %s{!}", label, title) } if !options.Has(TOP_LINE) { @@ -146,7 +152,7 @@ func renderPanel(label, colorTag, title, message string, options Options) { } else { lineSize := width - (strutil.LenVisual(label+title) + 4) - if options.Has(LABEL_POWERLINE) { + if options.Has(LABEL_POWERLINE) && !fmtc.DisableColors { lineSize-- } diff --git a/fmtutil/panel/panel_test.go b/fmtutil/panel/panel_test.go index 9f1a4d50..55cece66 100644 --- a/fmtutil/panel/panel_test.go +++ b/fmtutil/panel/panel_test.go @@ -10,6 +10,8 @@ package panel import ( "testing" + "github.com/essentialkaos/ek/v12/fmtc" + . "github.com/essentialkaos/check" ) @@ -37,6 +39,16 @@ func (s *PanelSuite) TestBasicInfoPanel(c *C) { Info("Test info", "Message") } +func (s *PanelSuite) TestNoColor(c *C) { + fmtc.DisableColors = true + Panel( + "使用上のヒント", "{m}", "Test with no colors", + `Lorem ipsum dolor sit amet.`, + WRAP, INDENT_OUTER, INDENT_INNER, TOP_LINE, BOTTOM_LINE, LABEL_POWERLINE, + ) + fmtc.DisableColors = false +} + func (s *PanelSuite) TestPanelAllOptions(c *C) { Width = 60 Indent = 2