-
Notifications
You must be signed in to change notification settings - Fork 33
/
model.go
85 lines (79 loc) · 2 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Board struct {
help help.Model
loaded bool
focused status
cols []column
quitting bool
}
func NewBoard() *Board {
help := help.New()
help.ShowAll = true
return &Board{help: help, focused: todo}
}
func (m *Board) Init() tea.Cmd {
return nil
}
func (m *Board) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
var cmd tea.Cmd
var cmds []tea.Cmd
m.help.Width = msg.Width - margin
for i := 0; i < len(m.cols); i++ {
var res tea.Model
res, cmd = m.cols[i].Update(msg)
m.cols[i] = res.(column)
cmds = append(cmds, cmd)
}
m.loaded = true
return m, tea.Batch(cmds...)
case Form:
return m, m.cols[m.focused].Set(msg.index, msg.CreateTask())
case moveMsg:
return m, m.cols[m.focused.getNext()].Set(APPEND, msg.Task)
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, keys.Left):
m.cols[m.focused].Blur()
m.focused = m.focused.getPrev()
m.cols[m.focused].Focus()
case key.Matches(msg, keys.Right):
m.cols[m.focused].Blur()
m.focused = m.focused.getNext()
m.cols[m.focused].Focus()
}
}
res, cmd := m.cols[m.focused].Update(msg)
if _, ok := res.(column); ok {
m.cols[m.focused] = res.(column)
} else {
return res, cmd
}
return m, cmd
}
// Changing to pointer receiver to get back to this model after adding a new task via the form... Otherwise I would need to pass this model along to the form and it becomes highly coupled to the other models.
func (m *Board) View() string {
if m.quitting {
return ""
}
if !m.loaded {
return "loading..."
}
board := lipgloss.JoinHorizontal(
lipgloss.Left,
m.cols[todo].View(),
m.cols[inProgress].View(),
m.cols[done].View(),
)
return lipgloss.JoinVertical(lipgloss.Left, board, m.help.View(keys))
}