-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuTask.go
More file actions
247 lines (217 loc) · 7.57 KB
/
Copy pathmenuTask.go
File metadata and controls
247 lines (217 loc) · 7.57 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"fmt"
"slices"
"github.com/VladiTNT/Todo-tcell/tcellUtils"
"github.com/gdamore/tcell/v2"
)
type taskMenu struct {
screen tcell.Screen
currentInput *[]rune
isHoursGood bool
isDaysGood bool
currentMaxLen int
inputTitle []rune
inputHours []rune
inputDays []rune
inputInfo []rune
}
func (t *taskMenu) Draw() {
t.screen.Clear()
// Ui Title
tcellUtils.DrawStringHoriz("-[ Task Edit Menu ]-", 49, 1, t.screen)
tcellUtils.DrawStringHoriz("In this menu you can add and edit tasks, write each field and press <KeyDown> to jump to the next one. Press <KeyUp>", 4, 2, t.screen)
tcellUtils.DrawStringHoriz("to jump to the previous field. If you are done, press <Enter> to add/modify the task, or <Esc> to exit.", 4, 3, t.screen)
// Ui Margins
tcellUtils.DrawLine('=', 2, 122, 5, t.screen)
tcellUtils.DrawColumn('|', 4, 6, 16, t.screen)
tcellUtils.DrawColumn('=', 3, 6, 16, t.screen)
tcellUtils.DrawColumn('|', 2, 6, 16, t.screen)
tcellUtils.DrawColumn('|', 120, 6, 16, t.screen)
tcellUtils.DrawColumn('=', 121, 6, 16, t.screen)
tcellUtils.DrawColumn('|', 122, 6, 16, t.screen)
tcellUtils.DrawLine('=', 2, 122, 17, t.screen)
// Side bars
tcellUtils.DrawColumn('|', 0, 0, 31, t.screen)
tcellUtils.DrawColumn('|', 1, 0, 31, t.screen)
tcellUtils.DrawColumn('|', 123, 0, 31, t.screen)
tcellUtils.DrawColumn('|', 124, 0, 31, t.screen)
tcellUtils.DrawLine('=', 2, 122, 0, t.screen)
tcellUtils.DrawLine('=', 2, 122, 26, t.screen)
for i := 27; i < 32; i++ {
tcellUtils.DrawLine('-', 2, 122, i, t.screen)
}
// Ui Text
tcellUtils.DrawStringHoriz("Input Title:", 6, 7, t.screen)
tcellUtils.DrawStringHoriz("Add hours until deadline:", 6, 9, t.screen)
tcellUtils.DrawStringHoriz("Add days until deadline:", 6, 11, t.screen)
tcellUtils.DrawStringHoriz("Extra notes:", 6, 13, t.screen)
// InputTitle
tcellUtils.DrawLine('.', 19, 118, 7, t.screen)
for i, r := range t.inputTitle {
t.screen.SetContent(19+i, 7, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
}
validRunes := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
// InputHours
t.isHoursGood = true
tcellUtils.DrawLine('.', 32, 35, 9, t.screen)
for i, r := range t.inputHours {
t.screen.SetContent(32+i, 9, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
if !slices.Contains(validRunes, r) {
t.isHoursGood = false
tcellUtils.DrawColorHoriz("Error: invalid rune, must be a number!", 6, 10, t.screen, tcell.ColorRed)
} else {
t.isHoursGood = true
}
}
// InputDays
t.isDaysGood = true
tcellUtils.DrawLine('.', 31, 33, 11, t.screen)
for i, r := range t.inputDays {
t.screen.SetContent(31+i, 11, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
if !slices.Contains(validRunes, r) {
t.isDaysGood = false
tcellUtils.DrawColorHoriz("Error: invalid rune, must be a number!", 6, 12, t.screen, tcell.ColorRed)
} else {
t.isDaysGood = true
}
}
// InputInfo
tcellUtils.DrawLine('.', 19, 118, 13, t.screen)
tcellUtils.DrawLine('.', 19, 118, 14, t.screen)
tcellUtils.DrawLine('.', 19, 118, 15, t.screen)
for i, r := range t.inputInfo {
switch {
case i < 100:
t.screen.SetContent(19+i, 13, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
case i > 199:
t.screen.SetContent(19+i-200, 15, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
default:
t.screen.SetContent(19+i-100, 14, r, nil, tcell.StyleDefault.Foreground(tcell.ColorLightGreen))
}
}
// Cursor
switch t.currentInput {
case &t.inputTitle:
t.screen.SetContent(19+len(*t.currentInput), 7, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
case &t.inputHours:
t.screen.SetContent(32+len(*t.currentInput), 9, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
case &t.inputDays:
t.screen.SetContent(31+len(*t.currentInput), 11, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
case &t.inputInfo:
switch {
case len(*t.currentInput) < 100:
t.screen.SetContent(len(*t.currentInput)+19, 13, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
case len(*t.currentInput) > 199:
t.screen.SetContent(len(*t.currentInput)+19-200, 15, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
default:
t.screen.SetContent(len(*t.currentInput)+19-100, 14, '_', nil, tcell.StyleDefault.Foreground(tcell.ColorGold))
}
}
// Fields info at the bottom
titleColorStatus := tcell.ColorRed
titleStatus := "✖"
if len(t.inputTitle) > 0 {
titleColorStatus = tcell.ColorLightGreen
titleStatus = "✔"
}
titleString := fmt.Sprintf("- Title[%d/100]: %s", len(t.inputTitle), titleStatus)
tcellUtils.DrawColorHoriz(titleString, 6, 19, t.screen, titleColorStatus)
daysColorStatus := tcell.ColorGray
daysStatus := "✖"
if len(t.inputDays) > 0 && t.isDaysGood {
daysStatus = "✔"
daysColorStatus = tcell.ColorLightGreen
}
daysString := fmt.Sprintf("- Days (optional): %s", daysStatus)
tcellUtils.DrawColorHoriz(daysString, 6, 21, t.screen, daysColorStatus)
hoursColorStatus := tcell.ColorRed
hoursStatus := "✖"
if len(t.inputDays) > 0 && t.isDaysGood {
hoursColorStatus = tcell.ColorGray
}
if len(t.inputHours) > 0 && t.isHoursGood {
hoursStatus = "✔"
hoursColorStatus = tcell.ColorLightGreen
}
hoursString := fmt.Sprintf("- Hours: %s", hoursStatus)
tcellUtils.DrawColorHoriz(hoursString, 6, 20, t.screen, hoursColorStatus)
infoColorStatus := tcell.ColorGray
infoStatus := "✖"
if len(t.inputInfo) > 0 {
infoStatus = "✔"
infoColorStatus = tcell.ColorLightGreen
}
infoString := fmt.Sprintf("- Info[%d/300] (optional): %s", len(t.inputInfo), infoStatus)
tcellUtils.DrawColorHoriz(infoString, 6, 22, t.screen, infoColorStatus)
canAddColorStatus := tcell.ColorRed
canAdd := "=> Task can't be added, requires a valid title and a deadline."
if titleStatus == "✔" && t.isHoursGood && t.isDaysGood && (hoursStatus == "✔" || daysStatus == "✔") {
canAdd = "=> Task can be added! Press <Enter> to add."
canAddColorStatus = tcell.ColorLightGreen
}
tcellUtils.DrawColorHoriz(canAdd, 6, 24, t.screen, canAddColorStatus)
t.screen.Show()
}
func (t *taskMenu) Menu(a *App, isEdit bool) {
t.currentInput = &t.inputTitle
t.currentMaxLen = 100
quit := false
for !quit {
t.Draw()
isGood := len(t.inputTitle) > 0 && (len(t.inputHours) > 0 || len(t.inputDays) > 0)
ev := t.screen.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEsc:
quit = true
case tcell.KeyEnter:
if isGood && t.isHoursGood && t.isDaysGood {
t.AddTask(a, isEdit)
quit = true
}
case tcell.KeyUp:
switch t.currentInput {
case &t.inputHours:
t.currentInput = &t.inputTitle
t.currentMaxLen = 100
case &t.inputDays:
t.currentInput = &t.inputHours
t.currentMaxLen = 4
case &t.inputInfo:
t.currentInput = &t.inputDays
t.currentMaxLen = 3
}
case tcell.KeyDown:
switch t.currentInput {
case &t.inputTitle:
t.currentInput = &t.inputHours
t.currentMaxLen = 4
case &t.inputHours:
if t.isHoursGood {
t.currentInput = &t.inputDays
t.currentMaxLen = 3
}
case &t.inputDays:
if t.isDaysGood {
t.currentInput = &t.inputInfo
t.currentMaxLen = 300
}
}
case tcell.KeyBackspace, tcell.KeyBackspace2:
if len(*t.currentInput) > 0 {
*t.currentInput = (*t.currentInput)[:len(*t.currentInput)-1]
}
default:
if ev.Rune() != 0 && len(*t.currentInput) < t.currentMaxLen {
(*t.currentInput) = append(*t.currentInput, ev.Rune())
}
}
}
}
t.inputTitle = []rune{}
t.inputHours = []rune{}
t.inputDays = []rune{}
t.inputInfo = []rune{}
}