-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuiWidgets.go
280 lines (265 loc) · 10.1 KB
/
tuiWidgets.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
package main
import (
"fmt"
ui "github.com/gizak/termui"
"time"
)
const (
playerHeight = 2
searchBarHeight = 3
controlsHeight = 2
podcastDetailDescriptionHeight = 3
)
func everyTwoSeconds() bool {
t := time.Now().Second() % 4
if t == 0 || t == 1 {
return true
}
return false
}
func everyOtherSecond() bool {
if time.Now().Second()%2 == 0 {
return true
}
return false
}
func producePodcastListWidget(configuration *Configuration, width int, height int) *ui.List {
podcastWidget := ui.NewList()
podcastWidget.Width = width
podcastWidget.Height = height - playerHeight - controlsHeight
podcastWidget.Y = 0
podcastWidget.Border = false
podcastWidget.ItemFgColor = getForegroundColorForTheme(configuration)
var listFormattedPodcasts []string
podcasts := getCurrentPagePodcasts()
cursor := getCurrentCursorPosition()
currentListSize = len(podcasts)
if currentListSize == 0 {
tutorialString := fmt.Sprintf("No subscriptions, go left (<left>/%s) to search for podcasts!", configuration.LeftKeybind)
listFormattedPodcasts = append(listFormattedPodcasts, tutorialString)
}
for currentNum, item := range podcasts {
formattedPodcast := formatPodcast(item, width)
if currentNum == cursor {
formattedPodcast = termuiStyleText(formattedPodcast, getBackgroundColorForThemeString(configuration), getForegroundColorForThemeString(configuration))
}
listFormattedPodcasts = append(listFormattedPodcasts, formattedPodcast)
}
podcastWidget.Items = listFormattedPodcasts
return podcastWidget
}
func produceNothingPlayingWidget(configuration *Configuration, width int, height int) ui.Bufferer {
var widgetText string
if GetPlayerState() == PlayerPause {
widgetText = "Paused"
} else {
widgetText = "Nothing playing"
}
playerWidget := ui.NewParagraph(widgetText)
playerWidget.TextFgColor = getForegroundColorForTheme(configuration)
playerWidget.Width = width
playerWidget.Height = playerHeight
playerWidget.Y = height - playerHeight
playerWidget.BorderLeft = false
playerWidget.BorderRight = false
playerWidget.BorderBottom = false
return playerWidget
}
func fillPlayerGauge(playerWidget *ui.Gauge, configuration *Configuration, width int, height int) {
var label string
if (downloadInProgress() && GetPlayerState() != PlayerPlay) || (downloadInProgress() && everyTwoSeconds()) {
label = "Downloading: "
var (
totalDownloadSize int64
totalDownloadCompleated int64
)
num := 0
for key, value := range downloading {
if num > 0 {
label += " & "
}
label += key + " [" + byteCountDecimal(value.TotalDownloaded) + "/" + byteCountDecimal(value.FileSize) + "] (" + byteCountDecimal(value.Speed) + "/s)"
totalDownloadSize += value.FileSize
totalDownloadCompleated += value.TotalDownloaded
num++
}
playerWidget.Percent = int((float64(totalDownloadCompleated) / float64(totalDownloadSize+1)) * 100)
} else {
lengthOfPlayingFile := GetLengthOfPlayingFile()
currentPlayingPosition := GetPlayerPosition()
label = fmt.Sprintf("%d/%d", int(currentPlayingPosition), lengthOfPlayingFile)
playerWidget.Percent = int((float64(currentPlayingPosition) / float64(lengthOfPlayingFile)) * 100)
}
playerWidget.Label = label
}
func producePlayerWidget(configuration *Configuration, width int, height int) ui.Bufferer {
// when nothing is happening, just display a generic message
if !downloadInProgress() && GetPlayerState() != PlayerPlay {
return produceNothingPlayingWidget(configuration, width, height)
}
playerWidget := ui.NewGauge()
fillPlayerGauge(playerWidget, configuration, width, height)
playerWidget.Width = width
playerWidget.Height = playerHeight
playerWidget.Y = height - playerHeight
playerWidget.BorderLeft = false
playerWidget.BorderRight = false
playerWidget.BorderBottom = false
playerWidget.BarColor = getForegroundColorForTheme(configuration)
playerWidget.PercentColor = getForegroundColorForTheme(configuration)
playerWidget.PercentColorHighlighted = getBackgroundColorForTheme(configuration)
playerWidget.LabelAlign = ui.AlignLeft | ui.AlignCenterVertical
return playerWidget
}
func produceSearchWidget(configuration *Configuration, width int, height int) *ui.Paragraph {
text := ""
podcasts := getCurrentPagePodcasts()
if len(podcasts) > 0 {
text = " Results:\n"
} else {
text = " Search for podcasts:\n"
}
if currentMode == Insert {
text += " >"
} else if len(userTextBuffer) == 0 {
text += " >"
} else {
text += " "
}
if len(userTextBuffer) > 0 {
text += userTextBuffer
}
if currentMode == Insert && everyOtherSecond() {
text += "_"
}
searchWidget := ui.NewParagraph(text)
searchWidget.Y = 0
searchWidget.TextFgColor = getForegroundColorForTheme(configuration)
searchWidget.Height = searchBarHeight
searchWidget.Width = width
searchWidget.Border = false
return searchWidget
}
func produceSearchResultsWidget(configuration *Configuration, width int, height int) *ui.List {
searchWidgetHeight := height - searchBarHeight - playerHeight - controlsHeight
searchResultsWidget := ui.NewList()
searchResultsWidget.Y = searchBarHeight
searchResultsWidget.Height = searchWidgetHeight
searchResultsWidget.Width = width
searchResultsWidget.Border = false
searchResultsWidget.ItemFgColor = getForegroundColorForTheme(configuration)
var listFormattedPodcasts []string
podcasts := getCurrentPagePodcasts()
currentListSize = len(podcasts)
cursor := -1
if currentListSize == 0 && searchFailed {
listFormattedPodcasts = append(listFormattedPodcasts, "No results found")
}
if currentListSize != 0 && currentMode == Normal {
// Only highlight when we are not searching and the list size is not zero
cursor = getCurrentCursorPosition()
}
for currentNum, item := range podcasts {
if currentNum < (cursor - (searchWidgetHeight / 2)) {
continue
}
formattedPodcast := formatPodcast(item, width)
if podcastIsSubscribed(configuration, &item) {
formattedPodcast = "S - " + formattedPodcast
} else {
formattedPodcast = " " + formattedPodcast
}
if currentNum == cursor {
formattedPodcast = termuiStyleText(formattedPodcast, getBackgroundColorForThemeString(configuration), getForegroundColorForThemeString(configuration))
}
listFormattedPodcasts = append(listFormattedPodcasts, formattedPodcast)
}
searchResultsWidget.Items = listFormattedPodcasts
return searchResultsWidget
}
func produceDownloadedWidget(configuration *Configuration, width int, height int) *ui.List {
searchResultsWidgetHeight := height - playerHeight - controlsHeight
searchResultsWidget := ui.NewList()
searchResultsWidget.Y = 0
searchResultsWidget.Height = searchResultsWidgetHeight
searchResultsWidget.Width = width
searchResultsWidget.Border = false
searchResultsWidget.ItemFgColor = getForegroundColorForTheme(configuration)
searchResultsWidget.Overflow = "wrap"
var listFormattedPodcasts []string
var podcast = getCurrentPagePodcastEpisodes()
currentListSize = len(podcast)
cursor := -1
if currentListSize == 0 {
listFormattedPodcasts = append(listFormattedPodcasts, "No podcasts downloaded yet")
} else {
cursor = getCurrentCursorPosition()
}
for currentNum, item := range podcast {
if currentNum < (cursor - (searchResultsWidgetHeight / 2)) {
continue
}
formattedPodcast := item.PodcastTitle + " " + item.Title + " " + item.Summary
formattedPodcast = wrapOrBreakText(configuration, formattedPodcast, width, currentNum == getCurrentCursorPosition())
listFormattedPodcasts = append(listFormattedPodcasts, formattedPodcast)
}
searchResultsWidget.Items = listFormattedPodcasts
return searchResultsWidget
}
func produceControlsWidget(configuration *Configuration, width int, height int) ui.Bufferer {
controlsWidget := ui.NewParagraph(controlsMap[currentScreen])
controlsWidget.TextFgColor = getForegroundColorForTheme(configuration)
controlsWidget.Width = width
controlsWidget.Height = controlsHeight
controlsWidget.Y = height - playerHeight - controlsHeight
controlsWidget.BorderLeft = false
controlsWidget.BorderRight = false
controlsWidget.BorderBottom = false
return controlsWidget
}
func producePodcastDetailDescriptionWidget(configuration *Configuration, width int, height int) ui.Bufferer {
blurb := currentSelectedPodcast.CollectionName + ", " + currentSelectedPodcast.ArtistName + "\n" + currentSelectedPodcast.Description
podcastDetailWidget := ui.NewParagraph(blurb)
podcastDetailWidget.TextFgColor = getForegroundColorForTheme(configuration)
podcastDetailWidget.Width = width
podcastDetailWidget.Height = podcastDetailDescriptionHeight
podcastDetailWidget.Y = 0
podcastDetailWidget.BorderLeft = false
podcastDetailWidget.BorderRight = false
podcastDetailWidget.BorderTop = false
return podcastDetailWidget
}
func producePodcastDetailListWidget(configuration *Configuration, width int, height int) ui.Bufferer {
podcastDetailListWidgetHeight := height - podcastDetailDescriptionHeight - playerHeight - controlsHeight
var listFormattedPodcasts []string
podcasts := currentPodcastsInBuffers[currentScreen].([]PodcastEpisode)
podcastDetailListWidget := ui.NewList()
podcastDetailListWidget.Width = width
podcastDetailListWidget.Height = podcastDetailListWidgetHeight
podcastDetailListWidget.Y = podcastDetailDescriptionHeight
podcastDetailListWidget.Border = false
podcastDetailListWidget.ItemFgColor = getForegroundColorForTheme(configuration)
podcastDetailListWidget.Overflow = "wrap"
currentListSize = len(podcasts)
cursor := -1
if currentListSize == 0 {
listFormattedPodcasts = append(listFormattedPodcasts, "Could not load podcast")
} else {
cursor = getCurrentCursorPosition()
}
for currentNum, item := range podcasts {
if currentNum < (cursor - (podcastDetailListWidgetHeight / 2)) {
continue
}
formattedPodcast := formatPodcastEpisode(item)
if podcastIsDownloaded(configuration, &item) {
formattedPodcast = "D - " + formattedPodcast
} else {
formattedPodcast = " " + formattedPodcast
}
formattedPodcast = wrapOrBreakText(configuration, formattedPodcast, width, currentNum == getCurrentCursorPosition())
listFormattedPodcasts = append(listFormattedPodcasts, formattedPodcast)
}
podcastDetailListWidget.Items = listFormattedPodcasts
return podcastDetailListWidget
}