-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
201 lines (172 loc) · 4.46 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
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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/redis/go-redis/v9"
)
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.Tasktitle, task.Taskdescription, task.Taskdeadline)
f.index = c.list.Index()
f.col = c
f.originalTask = task // Save the original task for comparison
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 {
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())
}
RemoveTask(task)
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())
// remove task from db before updating its status
RemoveTask(task)
// status update
task.Taskstatus = c.status.getNext()
// add the updated task to db
AddTask(task)
// refresh list
var cmd tea.Cmd
c.list, cmd = c.list.Update(nil)
return tea.Sequence(cmd, func() tea.Msg { return moveMsg{task} })
}
func RemoveTask(task Task) {
// Generate the hash key for the task
hashKey := fmt.Sprintf("task:%d", task.Taskid)
// Remove the task from the Hash and the Sorted Set using a pipeline
_, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.HDel(ctx, "tasks", hashKey) // Remove the task from the Hash
pipe.ZRem(ctx, "tasks:sorted", hashKey) // Remove the task from the Sorted Set
return nil
})
if err != nil {
fmt.Printf("Encountered an error in removing the task: %v", task)
}
}
func AddTask(task Task) {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
if err := encoder.Encode(task); err != nil {
fmt.Printf("Error encoding custom type: %v", err)
}
dl, err := time.Parse("2006-01-02", task.Taskdeadline)
if err != nil {
fmt.Println("Error parsing deadline:", err)
}
// Using a pipeline to perform multiple operations
_, err = client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
hashKey := fmt.Sprintf("task:%d", task.Taskid)
pipe.HSet(ctx, "tasks", hashKey, buffer.Bytes())
pipe.ZAdd(ctx, "tasks:sorted", redis.Z{Score: float64(dl.Unix()), Member: hashKey})
return nil
})
if err != nil {
fmt.Println("Error executing Redis pipeline:", err)
}
}