-
Notifications
You must be signed in to change notification settings - Fork 33
/
column.go
140 lines (121 loc) · 2.85 KB
/
column.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const APPEND = -1
type column struct {
focus bool
status status
list list.Model
height int
width int
}
func (c *column) Focus() {
c.focus = true
}
func (c *column) Blur() {
c.focus = false
}
func (c *column) Focused() bool {
return c.focus
}
func newColumn(status status) column {
var focus bool
if status == todo {
focus = true
}
defaultList := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
defaultList.SetShowHelp(false)
return column{focus: focus, status: status, list: defaultList}
}
// Init does initial setup for the column.
func (c column) Init() tea.Cmd {
return nil
}
// Update handles all the I/O for columns.
func (c column) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
c.setSize(msg.Width, msg.Height)
c.list.SetSize(msg.Width/margin, msg.Height/2)
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Edit):
if len(c.list.VisibleItems()) != 0 {
task := c.list.SelectedItem().(Task)
f := NewForm(task.title, task.description)
f.index = c.list.Index()
f.col = c
return f.Update(nil)
}
case key.Matches(msg, keys.New):
f := newDefaultForm()
f.index = APPEND
f.col = c
return f.Update(nil)
case key.Matches(msg, keys.Delete):
return c, c.DeleteCurrent()
case key.Matches(msg, keys.Enter):
return c, c.MoveToNext()
}
}
c.list, cmd = c.list.Update(msg)
return c, cmd
}
func (c column) View() string {
return c.getStyle().Render(c.list.View())
}
func (c *column) DeleteCurrent() tea.Cmd {
if len(c.list.VisibleItems()) > 0 {
c.list.RemoveItem(c.list.Index())
}
var cmd tea.Cmd
c.list, cmd = c.list.Update(nil)
return cmd
}
func (c *column) Set(i int, t Task) tea.Cmd {
if i != APPEND {
return c.list.SetItem(i, t)
}
return c.list.InsertItem(APPEND, t)
}
func (c *column) setSize(width, height int) {
c.width = width / margin
}
func (c *column) getStyle() lipgloss.Style {
if c.Focused() {
return lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Height(c.height).
Width(c.width)
}
return lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.HiddenBorder()).
Height(c.height).
Width(c.width)
}
type moveMsg struct {
Task
}
func (c *column) MoveToNext() tea.Cmd {
var task Task
var ok bool
// If nothing is selected, the SelectedItem will return Nil.
if task, ok = c.list.SelectedItem().(Task); !ok {
return nil
}
// move item
c.list.RemoveItem(c.list.Index())
task.status = c.status.getNext()
// refresh list
var cmd tea.Cmd
c.list, cmd = c.list.Update(nil)
return tea.Sequence(cmd, func() tea.Msg { return moveMsg{task} })
}