-
Notifications
You must be signed in to change notification settings - Fork 0
/
episode-view.go
149 lines (137 loc) · 3.78 KB
/
episode-view.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
package main
// Episode UI state management
import (
"math"
"strings"
"github.com/nsf/termbox-go"
)
func episodeCursorUp(tx *termboxState) {
if tx.episodeIndex > 0 {
tx.episodeIndex--
updateScreen(tx, drawEpisode)
}
}
func episodeCursorDown(tx *termboxState) {
if tx.episodeIndex < tx.totalEpisodes-1 {
tx.episodeIndex++
updateScreen(tx, drawEpisode)
}
}
func episodeCursorPgup(tx *termboxState) {
_, height := getViewPortSize(tx)
if tx.totalEpisodes > height {
tx.episodeIndex = int(math.Max(float64(tx.episodeIndex-height), float64(0)))
updateScreen(tx, drawEpisode)
}
}
func episodeCursorPgdn(tx *termboxState) {
_, height := getViewPortSize(tx)
if tx.totalEpisodes > height {
tx.episodeIndex = int(math.Min(float64(tx.episodeIndex+height), float64(tx.totalEpisodes-1)))
updateScreen(tx, drawEpisode)
}
}
// When inside the Episode UI this is the termbox event handler
func EpisodeEventHandler(tx *termboxState) stateFn {
switch tx.ev.Type {
case termbox.EventKey:
switch tx.ev.Key {
case termbox.KeyEsc, termbox.KeyArrowLeft:
return transitionToSeriesState(tx)
case termbox.KeyArrowUp:
episodeCursorUp(tx)
case termbox.KeyArrowDown:
episodeCursorDown(tx)
case termbox.KeyPgup, termbox.KeyCtrlB:
episodeCursorPgup(tx)
case termbox.KeyPgdn, termbox.KeyCtrlF:
episodeCursorPgdn(tx)
case termbox.KeyEnter, termbox.KeyArrowRight:
updateScreen(tx, drawDetails)
return DetailEventHandler
}
switch tx.ev.Ch {
case keyh:
return transitionToSeriesState(tx)
case keyj:
episodeCursorDown(tx)
case keyk:
episodeCursorUp(tx)
case keyl:
updateScreen(tx, drawDetails)
return DetailEventHandler
case keyQuestion: // jump to key help
tx.Push(func(tx *termboxState) stateFn {
// use a closure on the stack so we can redraw the
// screen correctly. This means the caller of the pop'ed
// function *must* execute it and return the result
updateScreen(tx, drawEpisode)
return EpisodeEventHandler
})
updateScreen(tx, drawHelp)
return HelpEventHandler(tx)
case keySearch:
tx.searchText = ""
tx.consoleFn = searchConsoleFn
updateScreen(tx, drawEpisode)
return SearchEpisodeEventHandler
}
case termbox.EventResize:
updateScreen(tx, drawEpisode)
}
return EpisodeEventHandler
}
func transitionToSeriesState(tx *termboxState) stateFn {
tx.consoleFn = emptyConsoleFn
updateScreen(tx, drawSeries)
return SeriesEventHandler
}
func searchConsoleFn(tx *termboxState) string {
return tx.searchText
}
func SearchEpisodeEventHandler(tx *termboxState) stateFn {
switch tx.ev.Type {
case termbox.EventKey:
switch tx.ev.Key {
case termbox.KeyEsc:
tx.searchText = ""
tx.consoleFn = episodeConsoleFn
updateScreen(tx, drawEpisode)
return EpisodeEventHandler
case termbox.KeyArrowUp:
episodeCursorUp(tx)
return SearchEpisodeEventHandler
case termbox.KeyArrowDown:
episodeCursorDown(tx)
return SearchEpisodeEventHandler
case termbox.KeyPgup, termbox.KeyCtrlB:
episodeCursorPgup(tx)
return SearchEpisodeEventHandler
case termbox.KeyPgdn, termbox.KeyCtrlF:
episodeCursorPgdn(tx)
return SearchEpisodeEventHandler
case termbox.KeyBackspace, termbox.KeyBackspace2, termbox.KeyDelete:
l := len(tx.searchText)
if l > 0 {
tx.searchText = tx.searchText[:l-1]
updateScreen(tx, drawEpisode)
return SearchEpisodeEventHandler
}
case termbox.KeySpace:
tx.searchText += " "
updateScreen(tx, drawEpisode)
return SearchEpisodeEventHandler
}
switch tx.ev.Ch {
default:
if strings.ContainsRune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwyxz0123456789", tx.ev.Ch) {
// white list
tx.searchText += string(tx.ev.Ch)
updateScreen(tx, drawEpisode)
}
}
case termbox.EventResize:
updateScreen(tx, drawEpisode)
}
return SearchEpisodeEventHandler
}