-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_field.go
405 lines (345 loc) · 10.9 KB
/
date_field.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package ext
import (
"strconv"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type DateField struct {
*tview.Box
location *time.Location
currentYear int
currentMonth int
currentDay int
yearDropDown *tview.DropDown
monthDropDown *tview.DropDown
datePalette *tview.Table
yearOpen bool
monthOpen bool
open bool
label string
labelWidth int
labelColor tcell.Color
backgroundColor tcell.Color
fieldTextColor tcell.Color
fieldBackgroundColor tcell.Color
finished func(tcell.Key)
}
func NewDateField() *DateField {
now := time.Now()
y, m, d := now.Date()
df := &DateField{
Box: tview.NewBox(),
location: now.Location(),
labelColor: tview.Styles.SecondaryTextColor,
fieldBackgroundColor: tview.Styles.ContrastBackgroundColor,
fieldTextColor: tview.Styles.PrimaryTextColor,
}
df.initYearDropDown(2017, y)
df.initMonthDropDown(int(m))
df.initDatePalette(d)
return df
}
func (d *DateField) initYearDropDown(startYear, currentYear int) {
d.currentYear = currentYear
opts := rangeStrings(startYear, currentYear+1, " ")
d.yearDropDown = tview.NewDropDown().
SetFieldWidth(10).
SetOptions(opts, nil).
SetCurrentOption(len(opts) - 1)
d.yearDropDown.SetSelectedFunc(func(text string, index int) {
d.currentYear = startYear + index
d.refreshDatePalette()
})
}
func (d *DateField) initMonthDropDown(currentMonth int) {
d.currentMonth = currentMonth
opts := []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
d.monthDropDown = tview.NewDropDown().
SetFieldWidth(10).
SetOptions(opts, nil).
SetCurrentOption(currentMonth - 1)
d.monthDropDown.SetSelectedFunc(func(text string, index int) {
d.currentMonth = index + 1
d.refreshDatePalette()
})
}
func (d *DateField) initDatePalette(currentDay int) {
d.currentDay = currentDay
d.datePalette = tview.NewTable().SetBorders(false).SetSelectable(true, true)
d.datePalette.SetSelectionChangedFunc(func(row, column int) {
day, ok := d.datePalette.GetCell(row, column).GetReference().(int)
if ok {
d.currentDay = day
}
})
d.refreshDatePalette()
}
func (d *DateField) refreshDatePalette() {
d.datePalette.Clear()
daysInMonth := time.Date(d.currentYear, time.Month(d.currentMonth)+1, 0, 0, 0, 0, 0, d.location).Day()
offset := (int(time.Date(d.currentYear, time.Month(d.currentMonth), 1, 0, 0, 0, 0, d.location).Weekday()) + 6) % 7
// create content starting from weekdays titles then empty strings (if month starts not from monday) then days.
content := make([]string, 7+offset+daysInMonth)
copy(content, []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"})
copy(content[7+offset:], rangeStrings(1, daysInMonth+1, ""))
// set numbers as selectable cells with reference & make other cells not selectable
for i, v := range content {
r, c := i/7, i%7
if i > 6+offset {
d.datePalette.SetCell(r, c, tview.NewTableCell(v).SetAlign(tview.AlignRight).SetReference(i-offset-6))
} else {
d.datePalette.SetCell(r, c, tview.NewTableCell(v).SetSelectable(false))
}
}
i := d.currentDay + offset + 6
d.datePalette.Select(i/7, i%7)
}
// SetLabel sets the text to be displayed before the input area.
func (d *DateField) SetLabel(label string) *DateField {
d.label = label
return d
}
// GetLabel returns the text to be displayed before the input area.
func (d *DateField) GetLabel() string {
return d.label
}
// SetFormAttributes sets attributes shared by all form items.
func (d *DateField) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) tview.FormItem {
d.labelWidth = labelWidth
d.labelColor = labelColor
d.backgroundColor = bgColor
d.fieldTextColor = fieldTextColor
d.fieldBackgroundColor = fieldBgColor
return d
}
// GetFieldWidth returns this primitive's field screen width.
func (d *DateField) GetFieldWidth() int {
return 10
}
// GetFieldHeight returns this primitive's field height.
func (d *DateField) GetFieldHeight() int {
return 1
}
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
func (d *DateField) SetFinishedFunc(handler func(key tcell.Key)) tview.FormItem {
d.finished = handler
return d
}
// SetText sets the current text of the input field.
func (d *DateField) SetTextDate(text string) *DateField {
dt, _ := time.Parse("2006-01-02", text)
y, m, _d := dt.Date()
d.currentYear = y
d.currentMonth = int(m)
d.currentDay = _d
d.refreshDatePalette()
return d
}
// GetText returns the current text of the date field.
func (d *DateField) GetTextDate() string {
return time.Date(d.currentYear, time.Month(d.currentMonth), d.currentDay, 0, 0, 0, 0, d.location).Format("2006-01-02")
}
// Draw draws this primitive onto the screen.
func (d *DateField) Draw(screen tcell.Screen) {
d.Box.DrawForSubclass(screen, d)
// Prepare.
x, y, width, height := d.GetInnerRect()
rightLimit := x + width
if height < 1 || rightLimit <= x {
return
}
// Draw label.
if d.labelWidth > 0 {
labelWidth := d.labelWidth
if labelWidth > rightLimit-x {
labelWidth = rightLimit - x
}
tview.Print(screen, d.label, x, y, labelWidth, tview.AlignLeft, d.labelColor)
x += labelWidth
} else {
_, drawnWidth := tview.Print(screen, d.label, x, y, rightLimit-x, tview.AlignLeft, d.labelColor)
x += drawnWidth
}
// Draw selection area.
fieldWidth := d.GetFieldWidth()
if rightLimit-x < fieldWidth {
fieldWidth = rightLimit - x
}
fieldStyle := tcell.StyleDefault.Background(d.fieldBackgroundColor)
if d.HasFocus() && !d.open {
fieldStyle = fieldStyle.Background(d.fieldTextColor)
}
for index := 0; index < fieldWidth; index++ {
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
}
// Draw selected date.
color := d.fieldTextColor
text := time.Date(d.currentYear, time.Month(d.currentMonth), d.currentDay, 0, 0, 0, 0, d.location).Format("2006-01-02")
// Just show the current selection.
if d.HasFocus() && !d.open {
color = d.fieldBackgroundColor
}
tview.Print(screen, text, x, y, fieldWidth, tview.AlignLeft, color)
// Draw calendar.
if d.HasFocus() && d.open {
lx := x
ly := y + 1
lwidth := 20
lheight := 2 + d.datePalette.GetRowCount()
swidth, sheight := screen.Size()
// We prefer to align the left sides of the list and the main widget, but
// if there is no space to the right, then shift the list to the left.
if lx+lwidth >= swidth {
lx = swidth - lwidth
if lx < 0 {
lx = 0
}
}
// We prefer to drop down but if there is no space, maybe drop up?
if ly+lheight >= sheight && ly-2 > lheight-ly {
ly = y - lheight
if ly < 0 {
ly = 0
}
}
if ly+lheight >= sheight {
lheight = sheight - ly
}
d.yearDropDown.SetRect(lx, ly, 10, 1)
d.monthDropDown.SetRect(lx+10, ly, 10, 1)
d.datePalette.SetRect(lx, ly+1, lwidth, lheight-2)
d.yearDropDown.SetFieldBackgroundColor(tview.Styles.MoreContrastBackgroundColor)
d.monthDropDown.SetFieldBackgroundColor(tview.Styles.MoreContrastBackgroundColor)
d.datePalette.SetBackgroundColor(tview.Styles.MoreContrastBackgroundColor)
datePaletteStyle := tcell.StyleDefault.Background(tview.Styles.MoreContrastBackgroundColor)
datePaletteStyle = datePaletteStyle.Foreground(tview.Styles.PrimaryTextColor)
d.datePalette.SetSelectedStyle(datePaletteStyle)
if d.datePalette.HasFocus() {
d.datePalette.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.PrimaryTextColor))
}
if d.yearOpen || d.monthOpen {
d.yearDropDown.SetFieldBackgroundColor(d.fieldBackgroundColor)
d.monthDropDown.SetFieldBackgroundColor(d.fieldBackgroundColor)
d.datePalette.SetBackgroundColor(d.fieldBackgroundColor)
d.datePalette.SetSelectedStyle(datePaletteStyle.Background(tview.Styles.ContrastBackgroundColor))
}
d.datePalette.Draw(screen)
d.yearDropDown.Draw(screen)
d.monthDropDown.Draw(screen)
}
}
// InputHandler returns the handler for this primitive.
func (d *DateField) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
// handle opening and closing calendar
if d.open {
if event.Key() == tcell.KeyEsc && !d.yearOpen && !d.monthOpen {
d.open = false
setFocus(d)
return
}
if event.Key() == tcell.KeyEnter && d.datePalette.HasFocus() {
d.open = false
setFocus(d)
return
}
} else {
if event.Key() == tcell.KeyEnter {
d.open = true
setFocus(d.datePalette)
return
}
if event.Key() == tcell.KeyEsc || event.Key() == tcell.KeyTab || event.Key() == tcell.KeyBacktab {
if d.finished != nil {
d.finished(event.Key())
}
}
}
// handle year dropdown keys
if d.yearDropDown.HasFocus() {
// ignore runes to ignore prefix input
if event.Key() == tcell.KeyRune {
return
}
if !d.yearOpen {
// handle keys if dropdown is closed
if event.Key() == tcell.KeyEnter {
d.yearOpen = true
}
if event.Key() == tcell.KeyTab {
setFocus(d.monthDropDown)
return
}
if event.Key() == tcell.KeyBacktab {
setFocus(d.datePalette)
return
}
} else {
// handle keys if dropdown is opened
if event.Key() == tcell.KeyEnter || event.Key() == tcell.KeyEsc {
d.yearOpen = false
}
}
if handler := d.yearDropDown.InputHandler(); handler != nil {
handler(event, setFocus)
}
return
}
// handle month dropdown keys
if d.monthDropDown.HasFocus() {
// ignore runes to ignore prefix input
if event.Key() == tcell.KeyRune {
return
}
if !d.monthOpen {
// handle keys if dropdown is closed
if event.Key() == tcell.KeyEnter {
d.monthOpen = true
}
if event.Key() == tcell.KeyTab {
setFocus(d.datePalette)
}
if event.Key() == tcell.KeyBacktab {
setFocus(d.yearDropDown)
}
} else {
// handle keys if dropdown is opened
if event.Key() == tcell.KeyEnter || event.Key() == tcell.KeyEsc {
d.monthOpen = false
}
}
if handler := d.monthDropDown.InputHandler(); handler != nil {
handler(event, setFocus)
}
return
}
// handle date palette keys
if d.datePalette.HasFocus() {
if event.Key() == tcell.KeyTab {
setFocus(d.yearDropDown)
}
if event.Key() == tcell.KeyBacktab {
setFocus(d.monthDropDown)
}
if handler := d.datePalette.InputHandler(); handler != nil {
handler(event, setFocus)
}
return
}
})
}
// HasFocus returns whether or not this primitive has focus.
func (d *DateField) HasFocus() bool {
if d.open {
return d.yearDropDown.HasFocus() || d.monthDropDown.HasFocus() || d.datePalette.HasFocus()
}
return d.Box.HasFocus()
}
func rangeStrings(start, end int, prefix string) []string {
res := make([]string, 0, end-start)
for i := start; i < end; i++ {
res = append(res, prefix+strconv.Itoa(i))
}
return res
}