|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/list" |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | +) |
| 11 | + |
| 12 | +type status int |
| 13 | + |
| 14 | +const divisor = 4 |
| 15 | + |
| 16 | +const ( |
| 17 | + todo status = iota |
| 18 | + inProgress |
| 19 | + done |
| 20 | +) |
| 21 | + |
| 22 | +/* STYLING */ |
| 23 | +var ( |
| 24 | + columnStyle = lipgloss.NewStyle(). |
| 25 | + Padding(1, 2). |
| 26 | + Border(lipgloss.HiddenBorder()) |
| 27 | + focusedStyle = lipgloss.NewStyle(). |
| 28 | + Padding(1, 2). |
| 29 | + Border(lipgloss.RoundedBorder()). |
| 30 | + BorderForeground(lipgloss.Color("62")) |
| 31 | + helpStyle = lipgloss.NewStyle(). |
| 32 | + Foreground(lipgloss.Color("241")) |
| 33 | +) |
| 34 | + |
| 35 | +/* CUSTOM ITEM */ |
| 36 | + |
| 37 | +type Task struct { |
| 38 | + status status |
| 39 | + title string |
| 40 | + description string |
| 41 | +} |
| 42 | + |
| 43 | +// implement the list.Item interface |
| 44 | +func (t Task) FilterValue() string { |
| 45 | + return t.title |
| 46 | +} |
| 47 | + |
| 48 | +func (t Task) Title() string { |
| 49 | + return t.title |
| 50 | +} |
| 51 | + |
| 52 | +func (t Task) Description() string { |
| 53 | + return t.description |
| 54 | +} |
| 55 | + |
| 56 | +/* MAIN MODEL */ |
| 57 | + |
| 58 | +type Model struct { |
| 59 | + loaded bool |
| 60 | + focused status |
| 61 | + lists []list.Model |
| 62 | + err error |
| 63 | + quitting bool |
| 64 | +} |
| 65 | + |
| 66 | +func New() *Model { |
| 67 | + return &Model{} |
| 68 | +} |
| 69 | + |
| 70 | +func (m *Model) Next() { |
| 71 | + if m.focused == done { |
| 72 | + m.focused = todo |
| 73 | + } else { |
| 74 | + m.focused++ |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (m *Model) Prev() { |
| 79 | + if m.focused == todo { |
| 80 | + m.focused = done |
| 81 | + } else { |
| 82 | + m.focused-- |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (m *Model) initLists(width, height int) { |
| 87 | + defaultList := list.New([]list.Item{}, list.NewDefaultDelegate(), width/divisor, height-divisor) |
| 88 | + defaultList.SetShowHelp(false) |
| 89 | + m.lists = []list.Model{defaultList, defaultList, defaultList} |
| 90 | + |
| 91 | + // Init To Do |
| 92 | + m.lists[todo].Title = "To Do" |
| 93 | + m.lists[todo].SetItems([]list.Item{ |
| 94 | + Task{status: todo, title: "buy milk", description: "strawberry milk"}, |
| 95 | + Task{status: todo, title: "eat sushi", description: "negitoro roll, miso soup, rice"}, |
| 96 | + Task{status: todo, title: "fold laundry", description: "or wear wrinkly t-shirts"}, |
| 97 | + }) |
| 98 | + // Init in progress |
| 99 | + m.lists[inProgress].Title = "In Progress" |
| 100 | + m.lists[inProgress].SetItems([]list.Item{ |
| 101 | + Task{status: todo, title: "write code", description: "don't worry, it's Go"}, |
| 102 | + }) |
| 103 | + // Init done |
| 104 | + m.lists[done].Title = "Done" |
| 105 | + m.lists[done].SetItems([]list.Item{ |
| 106 | + Task{status: todo, title: "stay cool", description: "as a cucumber"}, |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func (m Model) Init() tea.Cmd { |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 115 | + switch msg := msg.(type) { |
| 116 | + case tea.WindowSizeMsg: |
| 117 | + if !m.loaded { |
| 118 | + m.initLists(msg.Width, msg.Height) |
| 119 | + m.loaded = true |
| 120 | + } |
| 121 | + case tea.KeyMsg: |
| 122 | + switch msg.String() { |
| 123 | + case "ctrl+c", "q": |
| 124 | + m.quitting = true |
| 125 | + return m, tea.Quit |
| 126 | + case "left", "h": |
| 127 | + m.Prev() |
| 128 | + case "right", "l": |
| 129 | + m.Next() |
| 130 | + } |
| 131 | + } |
| 132 | + var cmd tea.Cmd |
| 133 | + m.lists[m.focused], cmd = m.lists[m.focused].Update(msg) |
| 134 | + return m, cmd |
| 135 | +} |
| 136 | + |
| 137 | +func (m Model) View() string { |
| 138 | + if m.quitting { |
| 139 | + return "" |
| 140 | + } |
| 141 | + if m.loaded { |
| 142 | + todoView := m.lists[todo].View() |
| 143 | + inProgView := m.lists[inProgress].View() |
| 144 | + doneView := m.lists[done].View() |
| 145 | + switch m.focused { |
| 146 | + case inProgress: |
| 147 | + return lipgloss.JoinHorizontal( |
| 148 | + lipgloss.Left, |
| 149 | + columnStyle.Render(todoView), |
| 150 | + focusedStyle.Render(inProgView), |
| 151 | + columnStyle.Render(doneView), |
| 152 | + ) |
| 153 | + case done: |
| 154 | + return lipgloss.JoinHorizontal( |
| 155 | + lipgloss.Left, |
| 156 | + columnStyle.Render(todoView), |
| 157 | + columnStyle.Render(inProgView), |
| 158 | + focusedStyle.Render(doneView), |
| 159 | + ) |
| 160 | + default: |
| 161 | + return lipgloss.JoinHorizontal( |
| 162 | + lipgloss.Left, |
| 163 | + focusedStyle.Render(todoView), |
| 164 | + columnStyle.Render(inProgView), |
| 165 | + columnStyle.Render(doneView), |
| 166 | + ) |
| 167 | + } |
| 168 | + } else { |
| 169 | + return "loading..." |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func main() { |
| 174 | + m := New() |
| 175 | + p := tea.NewProgram(m) |
| 176 | + if err := p.Start(); err != nil { |
| 177 | + fmt.Println(err) |
| 178 | + os.Exit(1) |
| 179 | + } |
| 180 | +} |
0 commit comments