Skip to content

Commit

Permalink
updated to use list as delegate and also replicated style from other …
Browse files Browse the repository at this point in the history
…models
  • Loading branch information
Sunny Guduru committed Mar 13, 2024
1 parent f1bdbca commit ae35578
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 39 deletions.
113 changes: 78 additions & 35 deletions internal/setup/autoCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,113 @@ 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 {
return nil
}

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
}
4 changes: 2 additions & 2 deletions internal/setup/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/setup/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae35578

Please sign in to comment.