-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcolumn.go
More file actions
228 lines (194 loc) · 4.89 KB
/
column.go
File metadata and controls
228 lines (194 loc) · 4.89 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"fmt"
"os"
"os/exec"
"github.com/DerTimonius/twkb/styles"
"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 {
list list.Model
status status
height int
width int
focus bool
}
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
}
defaultDelegate := list.NewDefaultDelegate()
defaultDelegate.Styles.SelectedTitle = styles.DefaultSelectedTitleStyle
defaultDelegate.Styles.SelectedDesc = styles.DefaultSelectedDesc
defaultList := list.New([]list.Item{}, defaultDelegate, 0, 0)
defaultList.SetShowHelp(false)
defaultList.Styles.Title = styles.DefaultListTitleStyle
return column{focus: focus, status: status, list: defaultList}
}
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-8)
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Edit):
if len(c.list.VisibleItems()) != 0 {
task := c.list.SelectedItem().(Task)
f := NewEditForm(task)
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.Unblock):
task := c.list.SelectedItem().(Task)
conf := NewConfirmation(fmt.Sprintf("Are you sure you want to unblock the task '%s'?", task.description), c.Unblock)
conf.index = APPEND
conf.column = c
return conf.Update(nil)
case key.Matches(msg, keys.Delete):
task := c.list.SelectedItem().(Task)
conf := NewConfirmation(fmt.Sprintf("Are you sure you want to delete the task '%s'?", task.description), c.DeleteCurrent)
conf.index = APPEND
conf.column = c
return conf.Update(nil)
case key.Matches(msg, keys.Block):
task := c.list.SelectedItem().(Task)
todoTasks := board.cols[todo].list.Items()
b := NewBlockForm(task, todoTasks, c.height, c.width)
b.index = APPEND
b.column = c
return b.Update(nil)
case key.Matches(msg, keys.Space):
return c, c.MoveToNext()
case key.Matches(msg, keys.Enter):
return c, c.MoveToDone()
}
}
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 {
var task Task
var ok bool
if task, ok = c.list.SelectedItem().(Task); !ok {
return nil
}
if len(c.list.VisibleItems()) > 0 {
c.list.RemoveItem(c.list.Index())
}
cmdStr, err := DeleteCmd(&task)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cmd := exec.Command(cmdStr[0], cmdStr[1:]...)
err = cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var cm tea.Cmd
c.list, cm = c.list.Update(nil)
return cm
}
func (c *column) Unblock() tea.Cmd {
var task Task
var ok bool
if task, ok = c.list.SelectedItem().(Task); !ok {
return nil
}
task.UnblockTask()
task.blocked = false
c.list.SetItem(c.list.Index(), task)
var cm tea.Cmd
c.list, cm = c.list.Update(task)
return cm
}
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
c.height = height - (margin * 2)
}
func (c *column) getStyle() lipgloss.Style {
baseColumnStyle := styles.ColumnBaseStyle
if c.Focused() {
return baseColumnStyle.
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color(styles.Blue)).
Height(c.height).
Width(c.width)
}
return baseColumnStyle.
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
}
// Don't move the task if it is in the done column
if task.status == done {
return nil
}
// move item
c.list.RemoveItem(c.list.Index())
task.StartStop()
// refresh list
var cmd tea.Cmd
c.list, cmd = c.list.Update(nil)
return tea.Sequence(cmd, func() tea.Msg { return moveMsg{task} })
}
func (c *column) MoveToDone() 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
}
c.list.RemoveItem(c.list.Index())
task.Finish()
// refresh list
var cmd tea.Cmd
c.list, cmd = c.list.Update(nil)
return tea.Sequence(cmd, func() tea.Msg { return moveMsg{task} })
}