|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + tea "github.com/charmbracelet/bubbletea" |
| 8 | + "github.com/charmbracelet/lipgloss" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + p := tea.NewProgram(initialModel()) |
| 13 | + if _, err := p.Run(); err != nil { |
| 14 | + fmt.Println("Error running program:", err) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +const ( |
| 19 | + dotChar = " • " |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + keywordStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211")) |
| 24 | + subtleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")) |
| 25 | + checkboxStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("212")) |
| 26 | + dotStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("236")).Render(dotChar) |
| 27 | + mainStyle = lipgloss.NewStyle().MarginLeft(2) |
| 28 | +) |
| 29 | + |
| 30 | +type model struct { |
| 31 | + quitting bool |
| 32 | + choice int |
| 33 | + chosen bool |
| 34 | +} |
| 35 | + |
| 36 | +type ( |
| 37 | + frameMsg struct{} |
| 38 | +) |
| 39 | + |
| 40 | +func initialModel() model { |
| 41 | + return model{ |
| 42 | + quitting: false, |
| 43 | + choice: 0, |
| 44 | + chosen: false, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (m model) Init() tea.Cmd { |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func frame() tea.Cmd { |
| 53 | + return tea.Tick(time.Second/60, func(time.Time) tea.Msg { |
| 54 | + return frameMsg{} |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 59 | + switch msg := msg.(type) { |
| 60 | + case tea.KeyMsg: |
| 61 | + if !m.chosen { |
| 62 | + return m.handleMainScreen(msg) |
| 63 | + } else { |
| 64 | + return m.handleSubScreen(msg) |
| 65 | + } |
| 66 | + } |
| 67 | + return m, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (m model) handleMainScreen(msg tea.KeyMsg) (tea.Model, tea.Cmd) { |
| 71 | + switch msg.String() { |
| 72 | + case "ctrl+c", "esc": |
| 73 | + m.quitting = true |
| 74 | + return m, tea.Quit |
| 75 | + case "j", "down": |
| 76 | + m.choice++ |
| 77 | + if m.choice > 4 { |
| 78 | + m.choice = 4 |
| 79 | + } |
| 80 | + case "k", "up": |
| 81 | + m.choice-- |
| 82 | + if m.choice < 0 { |
| 83 | + m.choice = 0 |
| 84 | + } |
| 85 | + case "enter": |
| 86 | + m.chosen = true |
| 87 | + return m, frame() |
| 88 | + } |
| 89 | + |
| 90 | + return m, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (m model) handleSubScreen(msg tea.KeyMsg) (tea.Model, tea.Cmd) { |
| 94 | + switch msg.String() { |
| 95 | + case "ctrl+c": |
| 96 | + m.quitting = true |
| 97 | + return m, tea.Quit |
| 98 | + |
| 99 | + case "esc": |
| 100 | + m.chosen = false |
| 101 | + |
| 102 | + } |
| 103 | + return m, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (m model) View() string { |
| 107 | + var s string |
| 108 | + if m.quitting { |
| 109 | + return "\n See you later!\n\n" |
| 110 | + } |
| 111 | + if !m.chosen { |
| 112 | + s = choicesView(m) |
| 113 | + } else { |
| 114 | + switch m.choice { |
| 115 | + case 0: |
| 116 | + s = keywordStyle.Render("You chose Git / Git LFS!") |
| 117 | + case 1: |
| 118 | + s = keywordStyle.Render("You chose Godot!") |
| 119 | + case 2: |
| 120 | + s = keywordStyle.Render("You chose Rust!") |
| 121 | + case 3: |
| 122 | + s = keywordStyle.Render("You chose Zig!") |
| 123 | + case 4: |
| 124 | + s = keywordStyle.Render("You chose Docker!") |
| 125 | + default: |
| 126 | + s = keywordStyle.Render("Unknown choice") |
| 127 | + } |
| 128 | + } |
| 129 | + return mainStyle.Render("\n" + s + "\n\n") |
| 130 | +} |
| 131 | + |
| 132 | +func choicesView(m model) string { |
| 133 | + c := m.choice |
| 134 | + |
| 135 | + tpl := "Where would you like to begin?\n\n" |
| 136 | + tpl += "%s\n\n" |
| 137 | + tpl += subtleStyle.Render("j/k, up/down: select") + dotStyle + |
| 138 | + subtleStyle.Render("enter: choose") + dotStyle + |
| 139 | + subtleStyle.Render("ctrl+c, esc: quit") |
| 140 | + |
| 141 | + choices := fmt.Sprintf( |
| 142 | + "%s\n%s\n%s\n%s\n%s", |
| 143 | + checkbox("Git / Git LFS", c == 0), |
| 144 | + checkbox("Godot", c == 1), |
| 145 | + checkbox("Rust", c == 2), |
| 146 | + checkbox("Zig", c == 3), |
| 147 | + checkbox("Docker", c == 4), |
| 148 | + ) |
| 149 | + |
| 150 | + return fmt.Sprintf(tpl, choices) |
| 151 | +} |
| 152 | + |
| 153 | +func checkbox(label string, checked bool) string { |
| 154 | + if checked { |
| 155 | + return checkboxStyle.Render("[x] " + label) |
| 156 | + } |
| 157 | + return fmt.Sprintf("[ ] %s", label) |
| 158 | +} |
0 commit comments