Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[envsec] Improve project list style #245

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions envsec/internal/flow/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sort"
"strings"

"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/samber/lo"
"go.jetpack.io/envsec/internal/git"
"go.jetpack.io/pkg/api"
membersv1alpha1 "go.jetpack.io/pkg/api/gen/priv/members/v1alpha1"
Expand Down Expand Up @@ -118,19 +118,11 @@ func (i *Init) showExistingListPrompt(

prompt := promptui.Select{
Label: "What project would you like to link to",
Items: lo.Map(projects, func(proj *projectsv1alpha1.Project, _ int) string {
item := strings.TrimSpace(proj.GetName())
if item == "" {
item = "untitled"
}
if proj.GetRepo() != "" {
item += " repo: " + proj.GetRepo()
}
if proj.GetDirectory() != "" && proj.GetDirectory() != "." {
item += " dir: " + proj.GetDirectory()
}
return item + " id: " + proj.GetId()
}),
Items: formatProjectItems(projects),
Size: 10,
Templates: &promptui.SelectTemplates{
Active: "\U000025B8 {{ . }}",
},
}

idx, _, err := prompt.Run()
Expand All @@ -142,8 +134,11 @@ func (i *Init) showExistingListPrompt(
if err != nil {
return id.ProjectID{}, err
}

fmt.Fprintf(os.Stderr, "Linked to project %s\n", projects[idx].GetName())
name := projects[idx].GetName()
if name == "" {
name = "untitled"
}
fmt.Fprintf(os.Stderr, "Linked to project %s\n", name)
return projectID, nil
}

Expand Down Expand Up @@ -225,3 +220,49 @@ func boolPrompt(label, defaultResult string) (bool, error) {

return strings.ToLower(result) == "y", nil
}

func formatProjectItems(projects []*projectsv1alpha1.Project) []string {
longestNameLength := 0
longestRepoLength := 0
longestDirLength := 0
for _, proj := range projects {
name := proj.GetName()
if name == "" {
name = "untitled"
}
if l := len(name); l > longestNameLength {
longestNameLength = l
}
if l := len(proj.GetRepo()); l > longestRepoLength {
longestRepoLength = l
}
if l := len(proj.GetDirectory()); l > longestDirLength {
longestDirLength = l
}
}
// Add padding
table := make([][]string, len(projects))
for idx, proj := range projects {
name := proj.GetName()
if name == "" {
name = "untitled"
}
table[idx] = []string{
color.HiGreenString(
fmt.Sprintf("%-"+fmt.Sprintf("%d", longestNameLength)+"s", name),
),
color.HiBlueString("repo:"),
fmt.Sprintf("%-"+fmt.Sprintf("%d", longestRepoLength)+"s", proj.GetRepo()),
color.HiBlueString("dir:"),
fmt.Sprintf("%-"+fmt.Sprintf("%d", longestDirLength)+"s", proj.GetDirectory()),
color.HiBlueString("id:"),
proj.GetId(),
}
}

rows := []string{}
for _, cols := range table {
rows = append(rows, strings.Join(cols, " "))
}
return rows
}