-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskMenu.go
110 lines (98 loc) · 2.32 KB
/
taskMenu.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
package main
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/psethwick/tuidoist/style"
todoist "github.com/psethwick/todoist/lib"
)
type taskMenuModel struct {
content textinput.Model
desc textinput.Model
item todoist.Item
project *todoist.Project
focus taskMenuFocus
main *mainModel
}
type taskMenuFocus uint
const (
focusBox taskMenuFocus = iota
focusContent
focusDesc
)
func newTaskMenuModel(m *mainModel) taskMenuModel {
return taskMenuModel{
textinput.New(),
textinput.New(),
todoist.Item{},
nil,
focusBox,
m,
}
}
func (tm *taskMenuModel) updateFocus() {
switch tm.focus {
case focusBox:
tm.content.Blur()
tm.desc.Blur()
case focusContent:
tm.content.Focus()
tm.desc.Blur()
case focusDesc:
tm.content.Blur()
tm.desc.Focus()
}
}
func (tm *taskMenuModel) Update(msg tea.Msg) tea.Cmd {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "v":
t, err := tm.main.taskList.GetCursorItem()
if err != nil {
dbg(err)
}
if t.Url != "" {
cmds = append(cmds, tm.main.OpenUrl(t.Url))
}
case "m":
cmds = append(cmds, tm.main.OpenProjects(moveToProject))
case "c":
cmds = append(cmds, tm.main.completeTasks())
tm.main.state = viewTasks
case "delete":
cmds = append(cmds, tm.main.deleteTasks())
case "enter":
tm.item.Content = tm.content.Value()
tm.item.Description = tm.desc.Value()
cmds = append(cmds, tm.main.UpdateItem(tm.item))
tm.main.state = viewTasks
case "tab":
tm.focus = (tm.focus + 1) % 3
tm.updateFocus()
case "shift+tab":
tm.focus = (tm.focus - 1) % 3
tm.updateFocus()
case "esc":
tm.main.state = viewTasks
}
}
input, cmd := tm.content.Update(msg)
tm.content = input
cmds = append(cmds, cmd)
input, cmd = tm.desc.Update(msg)
tm.desc = input
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}
func (tm *taskMenuModel) View() string {
title := style.DialogTitle.Render("Task")
help := "(e)dit (c)complete (m)ove (d)elete"
ui := lipgloss.JoinVertical(lipgloss.Left, title, tm.content.View(), tm.desc.View(), tm.project.Name, help)
dialog := lipgloss.Place(tm.main.width, tm.main.height,
lipgloss.Left, lipgloss.Center,
style.DialogBox.Render(ui),
)
return dialog
}