diff options
| author | Paul Buetow <paul@buetow.org> | 2024-06-17 09:29:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-06-17 09:29:40 +0300 |
| commit | 2f353f6d9aee9d9a989d11757289c11679752789 (patch) | |
| tree | 68ff89aa37c661ad6de00c3774ce45b31591a4e0 /internal | |
| parent | 4867c040ffddfc18b235e1950f44870c99acf096 (diff) | |
initial TUI for the client
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/client/tui/tui.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/client/tui/tui.go b/internal/client/tui/tui.go new file mode 100644 index 0000000..6486ff2 --- /dev/null +++ b/internal/client/tui/tui.go @@ -0,0 +1,81 @@ +package tui + +import ( + "fmt" + "log" + + config "codeberg.org/snonux/gos/internal/config/client" + tea "github.com/charmbracelet/bubbletea" +) + +func Run(config config.ClientConfig) { + p := tea.NewProgram(initModel()) + if _, err := p.Run(); err != nil { + log.Fatal("error starting TUI:", err) + } +} + +type model struct { + choices []string + cursor int + selected map[int]struct{} +} + +func initModel() model { + return model{ + choices: []string{"Compose post", "Schedule post"}, + selected: make(map[int]struct{}), + } +} + +func (m model) Init() tea.Cmd { + return nil +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "q": + return m, tea.Quit + case "up", "k": + if m.cursor > 0 { + m.cursor-- + } + case "down", "j": + if m.cursor < len(m.choices)-1 { + m.cursor++ + } + case "enter", " ": + _, ok := m.selected[m.cursor] + if ok { + delete(m.selected, m.cursor) + } else { + m.selected[m.cursor] = struct{}{} + } + } + } + + return m, nil +} + +func (m model) View() string { + s := "Please choose your destiny\n\n" + + for i, choice := range m.choices { + cursor := " " // no cursor + if m.cursor == i { + cursor = "==>" + } + + checked := " " + if _, ok := m.selected[i]; ok { + checked = "x" + } + + s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice) + } + + s += "\nPress q to quiet.\n" + return s +} |
