From 92879003fd8d82a5d7d3260ce24a77e0e55cedc8 Mon Sep 17 00:00:00 2001 From: Michael Lorant Date: Sun, 14 Apr 2024 09:24:12 +1000 Subject: [PATCH] Fix alignment with available commands When upgrading the Color package from `v1.15.0` to `v1.16.0`, the list of available commands becomes incorrectly aligned: ```text Available Commands: completion Generate the autocompletion script for the specified shell help Help about any command test Test command for clarity ``` This was related to an [issue][1] where reset codes were not correctly set. The [fix][2], which added extra reset codes, increased the length of the string (which includes the ANSI codes) causing the amount of padding to be insufficient. This change, which upgrades the Color package to `v1.16.0`, also increass the padding to account for the additional ANSI reset codes that are now part of the string. This restores the text alignment to how it was previously displayed. Unit tests also needed to be updated to account for this change and are passing successfully. The Color package had updated its dependencies which has caused the changes in `go.mod` to impact more than itself. Fixes: #4 [1]: https://github.com/fatih/color/issues/206 [2]: https://github.com/fatih/color/pull/210 Signed-off-by: Michael Lorant --- coloredcobra.go | 2 +- coloredcobra_test.go | 4 ++-- go.mod | 2 +- go.sum | 22 +++++++++++----------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/coloredcobra.go b/coloredcobra.go index f06625e..6fd483e 100644 --- a/coloredcobra.go +++ b/coloredcobra.go @@ -155,7 +155,7 @@ func Init(cfg *Config) { // Patch usage template re := regexp.MustCompile(`(?i){{\s*rpad\s+.Name\s+.NamePadding\s*}}`) - tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}") + tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .Name) (sum .NamePadding 15)}}") re = regexp.MustCompile(`(?i){{\s*rpad\s+.CommandPath\s+.CommandPathPadding\s*}}`) tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}") diff --git a/coloredcobra_test.go b/coloredcobra_test.go index aff1a25..7240336 100644 --- a/coloredcobra_test.go +++ b/coloredcobra_test.go @@ -77,8 +77,8 @@ var templateTestHeadings = []templateTest{ } var templateTests = []templateTest{ - {`{{rpad .Name .NamePadding}}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}`}, - {`{{ rpad .Name .NamePadding }}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}`}, + {`{{rpad .Name .NamePadding}}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 15)}}`}, + {`{{ rpad .Name .NamePadding }}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 15)}}`}, {`{{rpad .CommandPath .CommandPathPadding}}`, `{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}`}, {`{{ rpad .CommandPath .CommandPathPadding }}`, `{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}`}, {`{{range .Commands}}{{.Short}}`, `{{range .Commands}}{{CmdShortStyle .Short}}`}, diff --git a/go.mod b/go.mod index 96096f9..efad6ce 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/ivanpirog/coloredcobra go 1.15 require ( - github.com/fatih/color v1.13.0 + github.com/fatih/color v1.16.0 github.com/spf13/cobra v1.4.0 ) diff --git a/go.sum b/go.sum index 6586c50..2386539 100644 --- a/go.sum +++ b/go.sum @@ -1,21 +1,21 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= -github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= -github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=