-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
290 lines (258 loc) · 7.28 KB
/
ui.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"fmt"
"math/rand"
"os"
"slices"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
CHARSET = "⠿⠾⠽⠼⠻⠺⠹⠸⠷⠶⠵⠴⠳⠲⠱⠰⠯⠮⠭⠬⠫⠪⠩⠨⠧⠦⠥⠤⠣⠢⠡⠠⠟⠞⠝⠜⠛⠚⠙⠘⠗⠖⠕⠔⠓⠒⠑⠐⠏⠎⠍⠌⠋⠊⠉⠈⠇⠆⠅⠄⠃⠂⠁"
totalPoints int
)
func getRandomString(length int) string {
letters := []rune(CHARSET)
b := make([]rune, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
type listScreenModel struct {
todoList []ToDo
activeTodoList []ToDo
cursor int
viewport viewport.Model
width, height int
tempFile *os.File
key string
}
func initialListmodel() *listScreenModel {
const width = 85
vp := viewport.New(width, 25)
vp.Style = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(2)
return &listScreenModel{
cursor: 0,
viewport: vp,
}
}
type TodoIO []ToDo
func startIO() tea.Msg {
var todos []ToDo
return TodoIO(todos)
}
func (m listScreenModel) Init() tea.Cmd {
return startIO
}
func getPoints(todos []ToDo) int {
count := 0
for _, todo := range todos {
if todo.isDone {
count += todo.Points
} else {
continue
}
}
return count
}
func (m listScreenModel) hideCategory(name string, value int){
if getPoints(m.todoList) >= value {
for i := range(m.todoList){
if m.todoList[i].Category == name {
m.todoList[i].isHidden = false
}
}
}
}
func (m listScreenModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case TodoIO:
m.key = "thisis32bitlongpassphraseimusing"
// To be deleted when final txt is done
todos, f := getEncryptedTodos(m.key)
m.todoList = todos
m.tempFile = f
m.activeTodoList = m.todoList
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
if m.width/2 < 85 {
m.viewport.Width = 85
} else {
m.viewport.Width = m.width / 2
}
if m.width < 85 {
m.viewport.Width = m.width - 2
}
m.viewport.Height = m.height - 2
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
encryptTodos(m.key, m.todoList, m.tempFile)
return m, tea.Quit
case "down", "j", "s":
if m.cursor < len(m.todoList)-1 {
m.cursor++
}
case "up", "k", "w":
if m.cursor > 0 {
m.cursor--
}
case "enter", " ":
if m.cursor < len(m.activeTodoList) {
if m.activeTodoList[m.cursor].isHidden {
return m, cmd
}
m.activeTodoList[m.cursor].isDone = !m.activeTodoList[m.cursor].isDone
m.activeTodoList[m.cursor].CompletionDate = time.Now()
idx := slices.IndexFunc(m.todoList, func(t ToDo) bool {return t.Text == m.activeTodoList[m.cursor].Text})
m.todoList[idx] = m.activeTodoList[m.cursor]
return m, cmd
}
case "f":
var activeTodo []ToDo
for _, todo := range(m.todoList){
if todo.isDone {
activeTodo = append(activeTodo, todo)
}
}
m.cursor = 0
m.activeTodoList = activeTodo
case "r":
var activeTodo []ToDo
for _, todo := range(m.todoList){
if !todo.isDone {
activeTodo = append(activeTodo, todo)
}
}
m.cursor = 0
m.activeTodoList = activeTodo
case "a":
m.activeTodoList = m.todoList
default:
return m, cmd
}
}
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}
func (m listScreenModel) View() string {
if totalPoints == 0 {
for _, todo := range(m.todoList){
totalPoints += todo.Points
}
}
m.hideCategory("Planes por Madrid", 20)
m.hideCategory("Bares", 30)
m.hideCategory("Cafés", 30)
m.hideCategory("Aventura", 45)
m.hideCategory("Bonus Cerca de Madrid", 50)
if getPoints(m.todoList) >= 70 {
for i := range(m.todoList){
if m.todoList[i].Category == "Bonus España" {
m.todoList[i].isHidden = false
}
}
}
if getPoints(m.todoList) >= 100 {
for i := range(m.todoList){
if m.todoList[i].Category == "Bonus Europa" {
m.todoList[i].isHidden = false
}
}
}
l := "Points: " + strconv.Itoa(getPoints(m.todoList)) + "/" + strconv.Itoa(totalPoints)
remainSpaces := m.viewport.Width - len(l) - 23
l += strings.Repeat(" ", remainSpaces)
l += "Points Date \n"
start := 0
end := m.viewport.Height - 4
if m.cursor >= end {
start = m.cursor - (m.viewport.Height - 4) + 1
end = m.cursor + 1
}
end = min(end, len(m.activeTodoList))
var currentCategory string
// var style = lipgloss.NewStyle().
// Bold(true).
// Foreground(lipgloss.Color("#FAFAFA")).
// Background(lipgloss.Color("#7D56F4")).
// Width(22)
for i := start; i < end ; i++ {
var todoStr string
todo := m.activeTodoList[i]
remainSpaces := m.viewport.Width - utf8.RuneCountInString(todo.Text) - 26
if remainSpaces < 0 {
remainSpaces = 0
}
spacesStr := strings.Repeat(" ", remainSpaces)
cursor := " "
checked := " "
date := ""
categoryStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("10")).Bold(true)
completedTodoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#68DA37"))
if currentCategory == "" {
currentCategory = todo.Category
categorySpaces := strings.Repeat(" ", m.viewport.Width - utf8.RuneCountInString(currentCategory) - 25)
var currentCategoryPoints int
var totalCategoryPoints int
for _, todo := range(m.todoList){
if todo.Category == currentCategory {
totalCategoryPoints += todo.Points
if todo.isDone {
currentCategoryPoints += todo.Points
}
}
}
c := fmt.Sprintf("%s %s %d / %d", currentCategory, categorySpaces, currentCategoryPoints, totalCategoryPoints)
l += categoryStyle.Render(c)
l += "\n"
}
if currentCategory != todo.Category {
currentCategory = todo.Category
categorySpaces := strings.Repeat(" ", m.viewport.Width - utf8.RuneCountInString(currentCategory) - 25)
var currentCategoryPoints int
var totalCategoryPoints int
for _, todo := range(m.todoList){
if todo.Category == currentCategory {
totalCategoryPoints += todo.Points
if todo.isDone {
currentCategoryPoints += todo.Points
}
}
}
c := fmt.Sprintf("%s %s %d / %d", currentCategory, categorySpaces, currentCategoryPoints, totalCategoryPoints)
l += categoryStyle.Render(c)
l += "\n"
}
if m.cursor == i {
cursor = ">"
}
if todo.isDone {
date = todo.CompletionDate.Format("2006-01-02")
checked = "x"
s := fmt.Sprintf("%s [%s] %s %s %s %s", cursor, checked, todo.Text, spacesStr, lipgloss.NewStyle().Foreground(lipgloss.Color("#7F00FF")).Render(strconv.Itoa(todo.Points)), lipgloss.NewStyle().AlignHorizontal(lipgloss.Right).Render(date))
todoStr = completedTodoStyle.Render(s)
} else {
todoStr = fmt.Sprintf("%s [%s] %s %s %s", cursor, checked, todo.Text, spacesStr, lipgloss.NewStyle().Foreground(lipgloss.Color("#7F00FF")).Render(strconv.Itoa(todo.Points)))
}
if todo.isHidden {
todoStr = fmt.Sprintf("%s [%s] %s %s %s", cursor, checked, getRandomString(utf8.RuneCountInString(todo.Text)), spacesStr, lipgloss.NewStyle().Foreground(lipgloss.Color("#7F00FF")).Render(strconv.Itoa(todo.Points)))
}
l += todoStr + "\n"
}
l += "\nPress q to quit."
m.viewport.SetContent(l)
return lipgloss.NewStyle().Width(m.width).AlignHorizontal(lipgloss.Center).Render(m.viewport.View())
}