From ae355783778b0ac33e553d6f89f86a89b735c232 Mon Sep 17 00:00:00 2001 From: Sunny Guduru Date: Wed, 13 Mar 2024 15:00:04 -0700 Subject: [PATCH] updated to use list as delegate and also replicated style from other models --- internal/setup/autoCreate.go | 113 +++++++++++++++++++++++---------- internal/setup/environments.go | 4 +- internal/setup/wizard.go | 4 +- 3 files changed, 82 insertions(+), 39 deletions(-) diff --git a/internal/setup/autoCreate.go b/internal/setup/autoCreate.go index 63fcccb9..ee4dc1f4 100644 --- a/internal/setup/autoCreate.go +++ b/internal/setup/autoCreate.go @@ -4,21 +4,55 @@ package setup // program after the Bubble Tea has exited. import ( + "fmt" + "io" "strings" + "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" ) -var choices = []string{"Yes", "No"} -var title = "Do you want to get started with our recommended project, environment, and flag?" +var ( + choiceStyle = lipgloss.NewStyle().PaddingLeft(4) + selectedChoiceItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170")) + + _ list.Item = choice{} +) + +type choice struct { + Key string `json:"key"` + Name string `json:"name"` +} + +func (p choice) FilterValue() string { return "" } type autoCreateModel struct { - cursor int choice string + err error + list list.Model } func NewAutoCreate() autoCreateModel { - return autoCreateModel{} + choices := []choice{ + { + Key: "yes", + Name: "Yes", + }, + { + Key: "no", + Name: "No", + }, + } + l := list.New(choicesToItems(choices), autoCreateDelegate{}, 85, 14) + l.Title = "Do you want to get started with our recommended project, environment, and flag?" + l.SetShowStatusBar(false) + l.SetFilteringEnabled(false) + + return autoCreateModel{ + list: l, + } } func (m autoCreateModel) Init() tea.Cmd { @@ -26,48 +60,57 @@ func (m autoCreateModel) Init() tea.Cmd { } func (m autoCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmd tea.Cmd switch msg := msg.(type) { case tea.KeyMsg: - switch msg.String() { - case "ctrl+c", "q", "esc": + switch { + case key.Matches(msg, keys.Enter): + i, ok := m.list.SelectedItem().(choice) + if ok { + m.choice = i.Key + } + case key.Matches(msg, keys.Quit): return m, tea.Quit + default: + m.list, cmd = m.list.Update(msg) + } + } - case "enter": - // Send the choice on the channel and exit. - m.choice = choices[m.cursor] - return m, tea.Quit + return m, cmd +} - case "down", "j": - m.cursor++ - if m.cursor >= len(choices) { - m.cursor = 0 - } +func (m autoCreateModel) View() string { + return "\n" + m.list.View() +} - case "up", "k": - m.cursor-- - if m.cursor < 0 { - m.cursor = len(choices) - 1 - } +type autoCreateDelegate struct{} + +func (d autoCreateDelegate) Height() int { return 1 } +func (d autoCreateDelegate) Spacing() int { return 0 } +func (d autoCreateDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } +func (d autoCreateDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { + i, ok := listItem.(choice) + if !ok { + return + } + + str := i.Name + + fn := choiceStyle.Render + if index == m.Index() { + fn = func(s ...string) string { + return selectedChoiceItemStyle.Render("> " + strings.Join(s, " ")) } } - return m, nil + fmt.Fprint(w, fn(str)) } -func (m autoCreateModel) View() string { - s := strings.Builder{} - s.WriteString(title + "\n\n") - - for i := 0; i < len(choices); i++ { - if m.cursor == i { - s.WriteString("(•) ") - } else { - s.WriteString("( ) ") - } - s.WriteString(choices[i]) - s.WriteString("\n") +func choicesToItems(choices []choice) []list.Item { + items := make([]list.Item, len(choices)) + for i, c := range choices { + items[i] = list.Item(c) } - s.WriteString("\n(press q to quit)\n") - return s.String() + return items } diff --git a/internal/setup/environments.go b/internal/setup/environments.go index 6f82ac09..6cb977af 100644 --- a/internal/setup/environments.go +++ b/internal/setup/environments.go @@ -107,8 +107,8 @@ func (d envDelegate) Render(w io.Writer, m list.Model, index int, listItem list. func environmentsToItems(environments []environment) []list.Item { items := make([]list.Item, len(environments)) - for i, proj := range environments { - items[i] = list.Item(proj) + for i, e := range environments { + items[i] = list.Item(e) } return items diff --git a/internal/setup/wizard.go b/internal/setup/wizard.go index f60e772e..1ff75063 100644 --- a/internal/setup/wizard.go +++ b/internal/setup/wizard.go @@ -62,9 +62,9 @@ func (m WizardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch m.currStep { case autoCreateStep: model, _ := m.steps[autoCreateStep].Update(msg) - f, ok := model.(autoCreateModel) + p, ok := model.(autoCreateModel) if ok { - m.useRecommendedResources = f.choice == "Yes" + m.useRecommendedResources = p.choice == "yes" if !m.useRecommendedResources { projModel, _ := m.steps[projectsStep].Update(fetchProjects{}) // we need to cast this to get the data out of it, but maybe we can create our own interface with