-
Notifications
You must be signed in to change notification settings - Fork 0
/
timewidget.go
175 lines (154 loc) · 3.3 KB
/
timewidget.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
package main
import (
"strconv"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type TimeWidget struct {
*tview.Box
content string
cursorPos int
changed func(t time.Time, err error)
}
func NewTimeWidget(t time.Time) *TimeWidget {
return &TimeWidget{
Box: tview.NewBox(),
content: t.UTC().Format(time.RFC3339),
cursorPos: 0,
}
}
func (w *TimeWidget) parse() (time.Time, error) {
return time.Parse(time.RFC3339, w.content)
}
func (w *TimeWidget) Draw(screen tcell.Screen) {
w.Box.DrawForSubclass(screen, w)
x, y, _, _ := w.GetInnerRect()
text := w.content
if _, err := w.parse(); err != nil {
text = text + " [yellow]"
idx := strings.Index(err.Error(), w.content)
if idx == -1 {
text += err.Error()
} else {
text += err.Error()[idx+len(w.content)+3:]
}
}
tview.PrintSimple(screen, text, x, y)
if w.HasFocus() {
screen.ShowCursor(x+w.cursorPos, y)
}
}
func moveRight(cursorPos int) int {
switch cursorPos {
case 18:
// do nothing
case 3, 6, 9, 12, 15:
cursorPos += 2
default:
cursorPos += 1
}
return cursorPos
}
func (w *TimeWidget) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return w.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
changed := false
switch event.Key() {
case tcell.KeyLeft:
switch w.cursorPos {
case 0:
// do nothing
case 5, 8, 11, 14, 17:
w.cursorPos -= 2
default:
w.cursorPos -= 1
}
case tcell.KeyRight:
w.cursorPos = moveRight(w.cursorPos)
case tcell.KeyBacktab:
switch w.cursorPos {
case 0, 1, 2, 3:
w.cursorPos = 17
case 5, 6:
w.cursorPos = 0
case 8, 9:
w.cursorPos = 5
case 11, 12:
w.cursorPos = 8
case 14, 15:
w.cursorPos = 11
case 17, 18:
w.cursorPos = 14
}
case tcell.KeyTab:
switch w.cursorPos {
case 0, 1, 2, 3:
w.cursorPos = 5
case 5, 6:
w.cursorPos = 8
case 8, 9:
w.cursorPos = 11
case 11, 12:
w.cursorPos = 14
case 14, 15:
w.cursorPos = 17
case 17, 18:
w.cursorPos = 0
}
case tcell.KeyUp, tcell.KeyDown:
if t, err := w.parse(); err == nil {
n := 1
if event.Key() == tcell.KeyUp {
n = -1
}
switch w.cursorPos {
case 0, 1, 2, 3:
t = t.AddDate(n, 0, 0)
case 5, 6:
t = t.AddDate(0, n, 0)
case 8, 9:
t = t.AddDate(0, 0, n)
case 11, 12:
t = t.Add(time.Duration(n) * time.Hour)
case 14, 15:
t = t.Add(time.Duration(n) * time.Minute)
case 17, 18:
t = t.Add(time.Duration(n) * time.Second)
}
w.content = t.Format(time.RFC3339)
changed = true
}
case tcell.KeyRune:
switch event.Rune() {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
valid := true
n, _ := strconv.Atoi(string(event.Rune()))
switch w.cursorPos {
case 5:
valid = n <= 1
case 8:
valid = n >= 1 && n <= 3
case 11:
valid = n <= 2
case 14, 17:
valid = n <= 5
}
if valid {
rs := []rune(w.content)
rs[w.cursorPos] = event.Rune()
w.content = string(rs)
changed = true
w.cursorPos = moveRight(w.cursorPos)
}
}
}
if changed {
w.changed(w.parse())
}
})
}
func (w *TimeWidget) SetChangedFunc(handler func(t time.Time, err error)) *TimeWidget {
w.changed = handler
return w
}