From 1899b835092f6467426734ea72469038f1f3e7db Mon Sep 17 00:00:00 2001 From: nobe4 Date: Fri, 10 Jan 2025 16:28:50 +0100 Subject: [PATCH 1/3] fix: padded display Apply code suggestion from @andyfeller from: - https://github.com/cli/cli/pull/10194#discussion_r1909761552 - https://github.com/cli/cli/pull/10194#discussion_r1909764278 --- pkg/cmd/extension/manager.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index 49c448b7e..32f0be26c 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -11,6 +11,7 @@ import ( "path" "path/filepath" "runtime" + "slices" "strings" "sync" @@ -469,18 +470,23 @@ func (m *Manager) Upgrade(name string, force bool) error { } func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error { - var longestExtName = 0 - for _, ext := range exts { - l := len(ext.Name()) - if len(ext.Name()) > longestExtName { - longestExtName = l + var longestExt = slices.MaxFunc(exts, func(a, b *Extension) int { + la, lb := len(a.Name()), len(b.Name()) + if la == lb { + return 0 } - } - format := fmt.Sprintf("[%%%ds]: ", longestExtName) + + if la < lb { + return -1 + } + + return 1 + }) + var longestExtName = len(longestExt.Name()) var failed bool for _, f := range exts { - fmt.Fprintf(m.io.Out, format, f.Name()) + fmt.Fprintf(m.io.Out, "[%*s] ", longestExtName, f.Name()) currentVersion := displayExtensionVersion(f, f.CurrentVersion()) err := m.upgradeExtension(f, force) if err != nil { From 69ab0c9c85c42f95900ec64951ef8351c6c45fdf Mon Sep 17 00:00:00 2001 From: nobe4 Date: Fri, 10 Jan 2025 16:31:13 +0100 Subject: [PATCH 2/3] fix: actually read how MaxFunc work and simplify the code --- pkg/cmd/extension/manager.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index 32f0be26c..c82d6e856 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -471,16 +471,7 @@ func (m *Manager) Upgrade(name string, force bool) error { func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error { var longestExt = slices.MaxFunc(exts, func(a, b *Extension) int { - la, lb := len(a.Name()), len(b.Name()) - if la == lb { - return 0 - } - - if la < lb { - return -1 - } - - return 1 + return len(a.Name()) - len(b.Name()) }) var longestExtName = len(longestExt.Name()) From 69b17272d60ce36d785e416f40bace47b8fe8b05 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Fri, 10 Jan 2025 17:17:14 +0100 Subject: [PATCH 3/3] fix: add back colon that I removed --- pkg/cmd/extension/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index c82d6e856..7f4bcfd70 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -477,7 +477,7 @@ func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error { var failed bool for _, f := range exts { - fmt.Fprintf(m.io.Out, "[%*s] ", longestExtName, f.Name()) + fmt.Fprintf(m.io.Out, "[%*s]: ", longestExtName, f.Name()) currentVersion := displayExtensionVersion(f, f.CurrentVersion()) err := m.upgradeExtension(f, force) if err != nil {