From b59b131e77212ee82b9a612bcb279811ffb4c84c Mon Sep 17 00:00:00 2001 From: jbaranick Date: Fri, 1 Mar 2024 10:42:21 -0800 Subject: [PATCH 1/4] Output tabular search results which show the platforms available --- go.mod | 2 ++ go.sum | 4 +++ internal/boxcli/search.go | 56 ++++++++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index f8fe4132d0d..186b3e1138a 100644 --- a/go.mod +++ b/go.mod @@ -24,9 +24,11 @@ require ( github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/hashicorp/go-envparse v0.1.0 + github.com/jedib0t/go-pretty/v6 v6.5.4 github.com/joho/godotenv v1.5.1 github.com/mattn/go-isatty v0.0.20 github.com/mholt/archiver/v4 v4.0.0-alpha.8 + github.com/mitchellh/go-wordwrap v1.0.1 github.com/pelletier/go-toml/v2 v2.1.1 github.com/pkg/errors v0.9.1 github.com/rogpeppe/go-internal v1.12.0 diff --git a/go.sum b/go.sum index 13fcd1bffc1..e8e1eaed463 100644 --- a/go.sum +++ b/go.sum @@ -227,6 +227,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s= +github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -276,6 +278,8 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM= github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= diff --git a/internal/boxcli/search.go b/internal/boxcli/search.go index dbe00c4e253..fbe8dbc324b 100644 --- a/internal/boxcli/search.go +++ b/internal/boxcli/search.go @@ -5,12 +5,16 @@ package boxcli import ( "fmt" + "github.com/jedib0t/go-pretty/v6/text" + "github.com/spf13/cobra" "io" "math" + "net/url" + "slices" "strings" - "github.com/spf13/cobra" - + "github.com/jedib0t/go-pretty/v6/table" + "github.com/mitchellh/go-wordwrap" "go.jetpack.io/devbox/internal/boxcli/usererr" "go.jetpack.io/devbox/internal/searcher" "go.jetpack.io/devbox/internal/ux" @@ -86,24 +90,51 @@ func printSearchResults( pkgs = results.Packages[:int(math.Min(10, float64(len(results.Packages))))] } + rowConfigAutoMerge := table.RowConfig{AutoMerge: true} + + t := table.NewWriter() + t.AppendHeader(table.Row{"Package", "Versions", "Platforms"}, rowConfigAutoMerge) for _, pkg := range pkgs { - nonEmptyVersions := []string{} + systemKey := "" + var versions []string for i, v := range pkg.Versions { - if !showAll && i >= 10 { - resultsAreTrimmed = true - break - } if v.Version != "" { - nonEmptyVersions = append(nonEmptyVersions, v.Version) + if !showAll && i >= 10 { + resultsAreTrimmed = true + break + } + + var systems []string + for _, sys := range v.Systems { + systems = append(systems, sys.System) + } + slices.Sort(systems) + key := strings.Join(systems, " ") + if systemKey != key && systemKey != "" { + wrappedVersions := wordwrap.WrapString(strings.Join(versions[:], " "), 35) + wrappedSystems := wordwrap.WrapString(systemKey, 15) + t.AppendRow(table.Row{pkg.Name, wrappedVersions, wrappedSystems}, rowConfigAutoMerge) + versions = nil + } + systemKey = key + versions = append(versions, v.Version) } } - versionString := "" - if len(nonEmptyVersions) > 0 { - versionString = fmt.Sprintf(" (%s)", strings.Join(nonEmptyVersions, ", ")) + if len(versions) > 0 { + wrappedVersions := wordwrap.WrapString(strings.Join(versions[:], " "), 35) + wrappedSystems := wordwrap.WrapString(systemKey, 15) + t.AppendRow(table.Row{pkg.Name, wrappedVersions, wrappedSystems}, rowConfigAutoMerge) } - fmt.Fprintf(w, "* %s %s\n", pkg.Name, versionString) } + t.SetColumnConfigs([]table.ColumnConfig{ + {Number: 1, AutoMerge: true, VAlign: text.VAlignMiddle}, + {Number: 2, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter}, + {Number: 3, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter}, + }) + t.SetStyle(table.StyleLight) + t.Style().Options.SeparateRows = true + fmt.Println(t.Render()) if resultsAreTrimmed { fmt.Println() @@ -113,6 +144,7 @@ func printSearchResults( "show all.\n\n", ) } + ux.Finfo(w, "For more information go to: https://www.nixhub.io/search?q=%s\n\n", url.QueryEscape(query)) return nil } From 700c5c9b9b32b0cf801bea0bfd9b1cfaf23aacd4 Mon Sep 17 00:00:00 2001 From: jbaranick Date: Fri, 1 Mar 2024 11:25:10 -0800 Subject: [PATCH 2/4] Columnize the versions --- go.mod | 1 - go.sum | 2 -- internal/boxcli/search.go | 39 ++++++++++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 186b3e1138a..32c426eb89d 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/joho/godotenv v1.5.1 github.com/mattn/go-isatty v0.0.20 github.com/mholt/archiver/v4 v4.0.0-alpha.8 - github.com/mitchellh/go-wordwrap v1.0.1 github.com/pelletier/go-toml/v2 v2.1.1 github.com/pkg/errors v0.9.1 github.com/rogpeppe/go-internal v1.12.0 diff --git a/go.sum b/go.sum index e8e1eaed463..be76a37cbea 100644 --- a/go.sum +++ b/go.sum @@ -278,8 +278,6 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM= github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A= -github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= -github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= diff --git a/internal/boxcli/search.go b/internal/boxcli/search.go index fbe8dbc324b..c7c6d42215a 100644 --- a/internal/boxcli/search.go +++ b/internal/boxcli/search.go @@ -4,17 +4,18 @@ package boxcli import ( + "bytes" "fmt" - "github.com/jedib0t/go-pretty/v6/text" "github.com/spf13/cobra" "io" "math" "net/url" "slices" "strings" + "text/tabwriter" "github.com/jedib0t/go-pretty/v6/table" - "github.com/mitchellh/go-wordwrap" + "github.com/jedib0t/go-pretty/v6/text" "go.jetpack.io/devbox/internal/boxcli/usererr" "go.jetpack.io/devbox/internal/searcher" "go.jetpack.io/devbox/internal/ux" @@ -111,9 +112,7 @@ func printSearchResults( slices.Sort(systems) key := strings.Join(systems, " ") if systemKey != key && systemKey != "" { - wrappedVersions := wordwrap.WrapString(strings.Join(versions[:], " "), 35) - wrappedSystems := wordwrap.WrapString(systemKey, 15) - t.AppendRow(table.Row{pkg.Name, wrappedVersions, wrappedSystems}, rowConfigAutoMerge) + t.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) versions = nil } systemKey = key @@ -122,15 +121,14 @@ func printSearchResults( } if len(versions) > 0 { - wrappedVersions := wordwrap.WrapString(strings.Join(versions[:], " "), 35) - wrappedSystems := wordwrap.WrapString(systemKey, 15) - t.AppendRow(table.Row{pkg.Name, wrappedVersions, wrappedSystems}, rowConfigAutoMerge) + t.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) } } + t.SetColumnConfigs([]table.ColumnConfig{ {Number: 1, AutoMerge: true, VAlign: text.VAlignMiddle}, {Number: 2, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter}, - {Number: 3, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter}, + {Number: 3, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter, WidthMaxEnforcer: text.WrapSoft, WidthMin: 15, WidthMax: 15}, }) t.SetStyle(table.StyleLight) t.Style().Options.SeparateRows = true @@ -148,3 +146,26 @@ func printSearchResults( return nil } + +func columnize(data []string, maxColumns int) string { + columns := maxColumns + if len(data) <= columns { + columns = 1 + } + + buf := bytes.NewBufferString("") + var versionsGroup []string + writer := tabwriter.NewWriter(buf, 0, 8, 1, '\t', tabwriter.AlignRight) + for _, version := range data { + if len(versionsGroup) == columns { + _, _ = fmt.Fprintf(writer, "%s\n", strings.Join(versionsGroup, "\t")) + versionsGroup = nil + } + versionsGroup = append(versionsGroup, version) + } + if len(versionsGroup) > 0 { + _, _ = fmt.Fprintf(writer, "%s\n", strings.Join(versionsGroup, "\t")) + } + _ = writer.Flush() + return buf.String() +} From 28cd93055dfaa5eb7892521e540637e078f0ba4d Mon Sep 17 00:00:00 2001 From: jbaranick Date: Fri, 1 Mar 2024 11:58:44 -0800 Subject: [PATCH 3/4] Format and lint code --- internal/boxcli/search.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/boxcli/search.go b/internal/boxcli/search.go index c7c6d42215a..e2a56a086d0 100644 --- a/internal/boxcli/search.go +++ b/internal/boxcli/search.go @@ -6,7 +6,6 @@ package boxcli import ( "bytes" "fmt" - "github.com/spf13/cobra" "io" "math" "net/url" @@ -14,6 +13,8 @@ import ( "strings" "text/tabwriter" + "github.com/spf13/cobra" + "github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/text" "go.jetpack.io/devbox/internal/boxcli/usererr" @@ -93,46 +94,46 @@ func printSearchResults( rowConfigAutoMerge := table.RowConfig{AutoMerge: true} - t := table.NewWriter() - t.AppendHeader(table.Row{"Package", "Versions", "Platforms"}, rowConfigAutoMerge) + tableWriter := table.NewWriter() + tableWriter.AppendHeader(table.Row{"Package", "Versions", "Platforms"}, rowConfigAutoMerge) for _, pkg := range pkgs { systemKey := "" var versions []string - for i, v := range pkg.Versions { - if v.Version != "" { + for i, pkgVersion := range pkg.Versions { + if pkgVersion.Version != "" { if !showAll && i >= 10 { resultsAreTrimmed = true break } var systems []string - for _, sys := range v.Systems { + for _, sys := range pkgVersion.Systems { systems = append(systems, sys.System) } slices.Sort(systems) key := strings.Join(systems, " ") if systemKey != key && systemKey != "" { - t.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) + tableWriter.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) versions = nil } systemKey = key - versions = append(versions, v.Version) + versions = append(versions, pkgVersion.Version) } } if len(versions) > 0 { - t.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) + tableWriter.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) } } - t.SetColumnConfigs([]table.ColumnConfig{ + tableWriter.SetColumnConfigs([]table.ColumnConfig{ {Number: 1, AutoMerge: true, VAlign: text.VAlignMiddle}, {Number: 2, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter}, {Number: 3, AutoMerge: true, Align: text.AlignJustify, AlignHeader: text.AlignCenter, WidthMaxEnforcer: text.WrapSoft, WidthMin: 15, WidthMax: 15}, }) - t.SetStyle(table.StyleLight) - t.Style().Options.SeparateRows = true - fmt.Println(t.Render()) + tableWriter.SetStyle(table.StyleLight) + tableWriter.Style().Options.SeparateRows = true + fmt.Println(tableWriter.Render()) if resultsAreTrimmed { fmt.Println() From 4e73e8ddc21f0cf19eb7c882bc3daf80f56b160c Mon Sep 17 00:00:00 2001 From: jbaranick Date: Fri, 1 Mar 2024 16:28:57 -0800 Subject: [PATCH 4/4] Reduce nesting --- internal/boxcli/search.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/internal/boxcli/search.go b/internal/boxcli/search.go index e2a56a086d0..656e449b2ab 100644 --- a/internal/boxcli/search.go +++ b/internal/boxcli/search.go @@ -100,25 +100,26 @@ func printSearchResults( systemKey := "" var versions []string for i, pkgVersion := range pkg.Versions { - if pkgVersion.Version != "" { - if !showAll && i >= 10 { - resultsAreTrimmed = true - break - } + if pkgVersion.Version == "" { + continue + } + if !showAll && i >= 10 { + resultsAreTrimmed = true + break + } - var systems []string - for _, sys := range pkgVersion.Systems { - systems = append(systems, sys.System) - } - slices.Sort(systems) - key := strings.Join(systems, " ") - if systemKey != key && systemKey != "" { - tableWriter.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) - versions = nil - } - systemKey = key - versions = append(versions, pkgVersion.Version) + var systems []string + for _, sys := range pkgVersion.Systems { + systems = append(systems, sys.System) + } + slices.Sort(systems) + key := strings.Join(systems, " ") + if systemKey != key && systemKey != "" { + tableWriter.AppendRow(table.Row{pkg.Name, columnize(versions, 2), systemKey}, rowConfigAutoMerge) + versions = nil } + systemKey = key + versions = append(versions, pkgVersion.Version) } if len(versions) > 0 {