-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
224 lines (205 loc) · 7.14 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"encoding/json"
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"os"
"time"
"tui-deck/deck_board"
"tui-deck/deck_card"
"tui-deck/deck_comment"
"tui-deck/deck_db"
"tui-deck/deck_help"
"tui-deck/deck_http"
"tui-deck/deck_stack"
"tui-deck/deck_structs"
"tui-deck/deck_ui"
"tui-deck/utils"
)
var app = tview.NewApplication()
var pages = tview.NewPages()
var configuration utils.Configuration
func main() {
deck_help.InitHelp()
var err error
configFile, err := utils.InitConfingDirectory()
if err != nil {
deck_ui.FooterBar.SetText(err.Error())
}
configuration, err = utils.GetConfiguration(configFile)
if err != nil {
deck_ui.FooterBar.SetText(err.Error())
}
fmt.Print("Getting boards...\n")
deck_ui.Init(app, configuration)
deck_board.Init(app, configuration)
var fatalError = false
deck_board.Boards, err = deck_http.GetBoards(configuration)
if err != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("FATAL ERROR: Error getting boards: %s", err.Error()))
fatalError = true
}
if !fatalError {
if len(deck_board.Boards) > 0 {
for i, b := range deck_board.Boards {
localBoardFile, _ := os.Open(fmt.Sprintf("%s/db/board-%d.json", configuration.ConfigDir, b.Id))
decoder := json.NewDecoder(localBoardFile)
localBoard := deck_structs.Board{}
err = decoder.Decode(&localBoard)
if b.Etag != localBoard.Etag {
var boardFile *os.File
boardFile, err = utils.CreateFile(fmt.Sprintf("%s/db/board-%d.json", configuration.ConfigDir, b.Id))
if err != nil {
panic(err)
}
marshal, _ := json.Marshal(b)
_, err = boardFile.Write(marshal)
if err != nil {
panic(err)
}
b.Updated = true
deck_board.Boards[i] = b
}
}
fmt.Print("Getting board detail...\n")
deck_board.CurrentBoard, err = deck_db.GetBoardDetails(deck_board.Boards[0].Id, deck_board.Boards[0].Updated, configuration)
if err != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("Error getting board detail: %s", err.Error()))
}
go deck_board.BuildSwitchBoard(configuration)
} else {
deck_ui.FooterBar.SetText("No boards found")
}
deck_ui.MainFlex.SetTitle(fmt.Sprintf(" TUI DECK: [#%s]%s ", deck_board.CurrentBoard.Color, deck_board.CurrentBoard.Title))
fmt.Print("Getting stacks...\n")
deck_stack.Init(app, configuration)
deck_card.Init(app, configuration, deck_board.CurrentBoard)
deck_comment.Init(app, configuration)
deck_stack.Stacks, err = deck_db.GetStacks(deck_board.CurrentBoard.Id, deck_board.CurrentBoard.Updated, configuration)
if err != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("Error getting stacks: %s", err.Error()))
}
deck_card.BuildStacks()
deck_ui.MainFlex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if deck_card.Modal.HasFocus() {
return deck_card.Modal.GetInputCapture()(event)
}
if event.Rune() == 113 {
// q -> quit app
app.Stop()
} else if event.Key() == tcell.KeyTab {
// tab -> switch focus between stacks
primitive := app.GetFocus()
list := primitive.(*tview.List)
list.SetTitleColor(tcell.ColorWhite)
actualPrimitiveIndex := deck_ui.Primitives[primitive]
app.SetFocus(deck_ui.GetNextFocus(actualPrimitiveIndex + 1))
} else if event.Rune() == 114 {
// r -> reload stacks
deck_stack.Stacks, err = deck_http.GetStacks(deck_board.CurrentBoard.Id, configuration)
if err != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("Error reloading stacks: %s", err.Error()))
}
deck_card.BuildStacks()
} else if event.Rune() == 115 {
// s -> switch board
deck_ui.BuildFullFlex(deck_board.BoardFlex, nil)
} else if event.Rune() == 97 {
// a -> add card
if len(deck_stack.Stacks) == 0 {
return nil
}
actualList := app.GetFocus().(*tview.List)
addForm, card := deck_card.BuildAddForm()
addForm.AddButton("Save", func() {
if len(card.DueDate) > 0 {
dueDate := card.DueDate
pattern := "02/01/2006 15:04"
parse, err2 := time.Parse(pattern, dueDate)
if err2 != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("Not a valid date, format must be dd/MM/YYYY HH:mm: %s", err2.Error()))
return
}
card.DueDate = parse.Format("2006-01-02T15:04:05+00:00")
}
deck_card.AddCard(actualList, *card)
})
deck_ui.BuildFullFlex(addForm, nil)
} else if event.Rune() == 100 {
if len(deck_stack.Stacks) == 0 {
return nil
}
// d -> delete card
actualList := app.GetFocus().(*tview.List)
var _, stack, _ = deck_stack.GetActualStack(actualList)
var currentItemIndex = actualList.GetCurrentItem()
mainText, _ := actualList.GetItemText(currentItemIndex)
cardId := utils.GetId(mainText)
deck_card.DeleteCard(cardId, stack, actualList, currentItemIndex)
} else if event.Key() == tcell.KeyCtrlA {
// ctrl + a -> add stack
addForm, stack := deck_stack.BuildAddForm(deck_structs.Stack{})
addForm.AddButton("Save", func() {
err := deck_stack.AddStack(deck_board.CurrentBoard.Id, *stack)
deck_card.BuildStacks()
deck_ui.BuildFullFlex(deck_ui.MainFlex, err)
})
deck_ui.BuildFullFlex(addForm, nil)
} else if event.Key() == tcell.KeyCtrlD {
// ctrl + d -> delete stack
if len(deck_stack.Stacks) == 0 {
return nil
}
actualList := app.GetFocus().(*tview.List)
index := deck_ui.Primitives[app.GetFocus()]
currentStack := deck_stack.Stacks[index]
deck_stack.DeleteStack(currentStack.Id, actualList)
deck_stack.Modal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
go func() {
_, err = deck_http.DeleteStack(deck_board.CurrentBoard.Id, currentStack.Id, configuration)
if err != nil {
deck_ui.FooterBar.SetText(fmt.Sprintf("Error deleting stack: %s", err.Error()))
}
}()
deck_ui.MainFlex.RemoveItem(deck_stack.Modal)
deck_ui.MainFlex.RemoveItem(actualList)
deck_stack.Stacks = append(deck_stack.Stacks[:index], deck_stack.Stacks[index+1:]...)
deck_card.BuildStacks()
deck_ui.BuildFullFlex(deck_ui.MainFlex, nil)
} else if buttonLabel == "No" {
deck_ui.MainFlex.RemoveItem(deck_stack.Modal)
app.SetFocus(actualList)
}
})
} else if event.Key() == tcell.KeyCtrlE {
// ctrl + e -> edit stack
actualList := app.GetFocus().(*tview.List)
index := deck_ui.Primitives[app.GetFocus()]
currentStack := deck_stack.Stacks[index]
editForm, editedStack := deck_stack.BuildAddForm(currentStack)
editForm.AddButton("Save", func() {
actualList.SetTitle(fmt.Sprintf("# %s ", editedStack.Title))
var err error
go func() {
err = deck_stack.EditStack(deck_board.CurrentBoard.Id, *editedStack)
}()
deck_stack.Stacks[index] = *editedStack
deck_card.BuildStacks()
deck_ui.BuildFullFlex(deck_ui.MainFlex, err)
})
deck_ui.BuildFullFlex(editForm, nil)
} else if event.Rune() == 63 {
// ? deck_help menu
deck_ui.BuildHelp(deck_ui.MainFlex, deck_help.HelpMain)
}
return event
})
deck_card.BuildCardViewer()
}
pages.AddPage("Main", deck_ui.FullFlex, true, true)
if err := app.SetRoot(pages, true).EnableMouse(false).Run(); err != nil {
panic(err)
}
}