logic.go 10.6 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
package main

import (
	"fmt"
	"os"
	"os/exec"
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
7
	"path/filepath"
Jonny Schäfer's avatar
Jonny Schäfer committed
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
	"regexp"
	"sort"
	"strconv"
	"strings"
)

// maps user input to events
type logicMatchEntry struct {
	event func(string)
	match *regexp.Regexp
}

// compile regex patterns and map to actions
var (
	logicMatchTable = map[string]*logicMatchEntry{
		"logicSearch": &logicMatchEntry{logicSearch, regexp.MustCompile(`(?m)^[^\:]`)},
		"logicNumber": &logicMatchEntry{logicNumber, regexp.MustCompile(`(?m)^\:[0-9]+(\ |$)`)},
		"logicPage":   &logicMatchEntry{logicPage, regexp.MustCompile(`(?m)^\::[0-9]+$`)},
		"logicSub":    &logicMatchEntry{logicSub, regexp.MustCompile(`(?m)^\:sub$`)}, // avoid matching conflict with subadd!
		"logicSubAdd": &logicMatchEntry{logicSubAdd, regexp.MustCompile(`(?m)^\:subadd`)},
		"logicSubDel": &logicMatchEntry{logicSubDel, regexp.MustCompile(`(?m)^\:subdel`)},
		"logicUser":   &logicMatchEntry{logicUser, regexp.MustCompile(`(?m)^\:user`)},
		"logicNew":    &logicMatchEntry{logicNew, regexp.MustCompile(`(?m)^\:new$`)},
		"logicSet":    &logicMatchEntry{logicSet, regexp.MustCompile(`(?m)^\:set`)},
		"logicHelp":   &logicMatchEntry{logicHelp, regexp.MustCompile(`(?m)^\:help`)},
		"logicQuit":   &logicMatchEntry{logicQuit, regexp.MustCompile(`(?m)^\:q$`)},
	}
)

type logicWindowState int

// window states
const (
	logicWindowNone logicWindowState = iota
	logicWindowSearch
	logicWindowSub
	logicWindowUser
)

var (
48
49
50
51
52
53
54
	logicVarWindow                   = logicWindowNone
	logicVarVideos []*videoContainer = []*videoContainer{}
	logicVarLazy                     = &listLazy{}
	logicVarUsers                    = []string{}
	logicVarUser                     = ""
	logicVarSearch                   = ""
	logicVarCurrentPage = 0
Jonny Schäfer's avatar
Jonny Schäfer committed
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
)

// logicAction parses the input string and performs the given action
func logicAction(input string) {
	// match pattern
	logicMatch(input)(logicTrim(input))
}

// logicMatch returns the best fitting function for the given input
func logicMatch(input string) func(string) {
	for _, entry := range logicMatchTable {
		if entry.match.MatchString(input) {
			return entry.event
		}
	}
	return logicUnknown
}

// logicTrim separates and returns the commands arguments
func logicTrim(trim string) string {
	if logicMatchTable["logicNumber"].match.MatchString(trim) {
		return trim[1:]
	}

	if logicMatchTable["logicPage"].match.MatchString(trim) {
		return trim[2:]
	}

	if strings.Index(trim, ":") == 0 {
		out := strings.SplitN(trim, " ", 2)
		if len(out) > 1 {
			return out[1]
		}
		return ""
	}

	return trim
}

94
95
96
97
98
99
100
// logicOnWinch reloads the page if the window size was changed
func logicOnWinch() {
	cliClear()
	logicLoadPage(logicVarCurrentPage)
	cliHome()
}

Jonny Schäfer's avatar
Jonny Schäfer committed
101
102
// logicSearch searches for the given video title
func logicSearch(title string) {
103
	upd := func(state int) []*videoContainer {
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
104
		return videoListSearch(title, strconv.Itoa(state+1))
105
106
107
	}

	logicVarLazy = listNew(upd)
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
108
	logicSearchPage("searching: "+title, 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
109
110
111
}

//logicSearchPage searches the title in the given page
112
113
func logicSearchPage(title string, page int) {
	cliStatus("page " + strconv.Itoa(page) + " | " + title)
Jonny Schäfer's avatar
Jonny Schäfer committed
114
115
	logicVarWindow = logicWindowSearch
	logicVarSearch = title
116
117
	linecount := termLines() - 2 
	list := logicVarLazy.listSlice(page*linecount, (page+1)*linecount)
118

Jonny Schäfer's avatar
Jonny Schäfer committed
119
120
121
122
123
124
125
	if list == nil {
		cliStatusError("network error: unable to load data")
		return
	}
	if len(list) == 0 {
		cliStatusError("no results for the given query")
	}
Jonny Schäfer's avatar
Jonny Schäfer committed
126
	logicList(-1, list)
Jonny Schäfer's avatar
Jonny Schäfer committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
}

// logicNumber selects the given list number
func logicNumber(command string) {
	splits := strings.SplitN(command, " ", 2)
	player := logicGetPlayer()
	if len(splits) > 1 {
		player = splits[1]
	}

	sel, err := strconv.Atoi(splits[0])
	errorCare(err)

	switch logicVarWindow {
	case logicWindowNone:
		cliStatusError("error: video list is empty")
	case logicWindowUser:
		fallthrough
	case logicWindowSearch:
		if sel < len(logicVarVideos) {
Jonny Schäfer's avatar
Jonny Schäfer committed
147
			logicPlay(player, sel) // non blocking
Jonny Schäfer's avatar
Jonny Schäfer committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
		} else {
			cliStatusError("error: video id not in list")
		}
	case logicWindowSub:
		if sel < len(logicVarUsers) {
			logicUser(logicVarUsers[sel])
		} else {
			cliStatusError("error: user id not in list")
		}
	}
}

// logicPage selects the given page
func logicPage(page string) {
162
163
164
	pnum, err := strconv.Atoi(page)
	errorCare(err)

165
166
167
168
169
170
171
	logicLoadPage(pnum)
}

// logicLoadPage calls the relevant function according to the
// window state
func logicLoadPage(pnum int) {
	logicVarCurrentPage = pnum // save for list redraw
Jonny Schäfer's avatar
Jonny Schäfer committed
172
173
	switch logicVarWindow {
	case logicWindowSearch:
174
		logicSearchPage(logicVarSearch, pnum)
Jonny Schäfer's avatar
Jonny Schäfer committed
175
	case logicWindowUser:
176
		logicUserPage(logicVarUser, pnum)
Jonny Schäfer's avatar
Jonny Schäfer committed
177
178
179
	}
}

Jonny Schäfer's avatar
Jonny Schäfer committed
180
// logicGetUsers retieves a list of all subscribed users
Jonny Schäfer's avatar
Jonny Schäfer committed
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
func logicGetUsers() []string {
	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)
	users := []string{}

	for _, user := range strings.Split(subs, "\n") {
		if len(user) > 2 && user[0] == '"' && user[len(user)-1] == '"' {
			users = append(users, user[1:len(user)-1])
		}
	}
	return users
}

// logicSub lists the subscriptions
func logicSub(none string) {
	columns := 4

	logicVarWindow = logicWindowSub
	id := make([][]string, columns)
	us := make([][]string, columns)

	users := logicGetUsers()
	sort.Strings(users)

	newcol := len(users)/columns + 1
	for i, u := range users {
		id[i/newcol] = append(id[i/newcol], strconv.Itoa(i))
		us[i/newcol] = append(us[i/newcol], u)
	}

	logicVarUsers = users
	cliStatus("list of subscriptions")
Jonny Schäfer's avatar
Jonny Schäfer committed
213
	cliTable(-1, id[0], us[0], id[1], us[1], id[2], us[2], id[3], us[3])
Jonny Schäfer's avatar
Jonny Schäfer committed
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
}

// logicSubAdd adds the given user to the subscription list
func logicSubAdd(user string) {
	if user == "" && logicVarWindow == logicWindowUser {
		user = logicVarUser
	}

	if strings.Contains(user, "\"") {
		cliStatusError("error: illicit username")
		return
	}

	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)

	if strings.Contains(subs, "\""+user+"\"") {
		cliStatusError("error: user '" + user + "' already in list")
		return
	}

	errorCare(storeWriteFile(storeDefaultSubscriptions, subs+"\""+user+"\"\n"))
	cliStatus("subscription '" + user + "' added")
	cliClearDown()
}

// logicSubAdd adds the given user to the subscription list
func logicSubDel(user string) {
	if user == "" && logicVarWindow == logicWindowUser {
		user = logicVarUser
	}

	if strings.Contains(user, "\"") {
		cliStatusError("error: illicit username")
		return
	}

	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)

	if !strings.Contains(subs, "\""+user+"\"") {
		cliStatusError("error: user '" + user + "' not in list")
		return
	}

	errorCare(storeWriteFile(storeDefaultSubscriptions, strings.Replace(subs, "\""+user+"\"\n", "", 1))) // meh
	cliStatus("subscription '" + user + "' deleted")
	cliClearDown()
}

// logicUser shows the videos of the given user
func logicUser(user string) {
266
	logicUserPage(user, 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
267
268
269
}

// logicUserPage shows the page of the given user
270
func logicUserPage(user string, page int) {
Jonny Schäfer's avatar
Jonny Schäfer committed
271
	// TODO: support pages (priority: low)
272
	cliStatus("page " + strconv.Itoa(page) + " | user: " + user)
Jonny Schäfer's avatar
Jonny Schäfer committed
273
274
	logicVarWindow = logicWindowUser
	logicVarUser = user
Jonny Schäfer's avatar
Jonny Schäfer committed
275
	logicList(-1, videoListUser(user))
Jonny Schäfer's avatar
Jonny Schäfer committed
276
277
278
279
280
281
282
}

// logicNew shows a list of new videos
func logicNew(none string) {
	logicVarWindow = logicWindowSearch
	logicVarSearch = ""
	users := logicGetUsers()
283
284
285
286
287
288

	upd := func(state int) []*videoContainer {
		if state < len(users) {
			return videoListUser(users[state])
		}
		return nil // all users updated (wont occur in reality)
Jonny Schäfer's avatar
Jonny Schäfer committed
289
	}
290
291
292
293
294

	logicVarLazy = listNew(upd)
	cliStatus("updating users ...")
	logicVarLazy.listPar(len(users))
	logicVarLazy.listGet(0) // downloads all user pages as par = len(users)
Jonny Schäfer's avatar
Jonny Schäfer committed
295
296
297
298

	cliHome()
	fmt.Println()
	cliStatus("sorting ...")
299
	sort.Sort(&videoSort{logicVarLazy.listCached()})
Jonny Schäfer's avatar
Jonny Schäfer committed
300

301
	logicSearchPage("new videos", 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
302
303
304
305
306
307
308
309
310
311
312
313
314
}

// logicSet sets the key to the given value. Format: "key value"
func logicSet(perform string) {
	logicSetPlayer(perform)
}

// logicHelp shows the help
func logicHelp(none string) {
	cliStatus("available commands and usage")
	cliClearDown()

	help := `
Jonny Schäfer's avatar
Jonny Schäfer committed
315
to search for a video input keywords
Jonny Schäfer's avatar
Jonny Schäfer committed
316
317
318

commands:

Jonny Schäfer's avatar
Jonny Schäfer committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
:<number> [player]	select list item (optional: video player/script*)
::<page_number>		show the specified page
:user <username>	show videos of the given user
:sub			show subscriptions
:subadd <username>	subscribe to the given or current user
:subdel <username>	delete subscription of the given or current user
:new			show newest videos of subscribed users
:set <player>		set the default video player
:help			show this help
:q			quit the program

*Scripts placed in the ~/.config/tubus/scripts directory are preferred
to other executable programs in $PATH. The video url will be passed as
first command-line argument.
Jonny Schäfer's avatar
Jonny Schäfer committed
333
334
335
336
337
338
339
340
341
342
343
344
`
	fmt.Println(help)
}

// logicUnknown shows a message if the command could not be parsed
func logicUnknown(command string) {
	cliStatusError("error: unknown command")
}

// logicQuit exits the program
func logicQuit(none string) {
	cliReset()
345
	termClose()
Jonny Schäfer's avatar
Jonny Schäfer committed
346
347
348
	os.Exit(0)
}

Jonny Schäfer's avatar
Jonny Schäfer committed
349
350
351
352
// logicList shows a video list and highlights the line with the given
// line number. If hightlight < 0 no line will be highlighted.
// The global video list logicVarVideos will be set by this function.
func logicList(highlight int, videos []*videoContainer) {
Jonny Schäfer's avatar
Jonny Schäfer committed
353
	logicVarVideos = videos
Jonny Schäfer's avatar
Jonny Schäfer committed
354
355

	cliClearDown()
Jonny Schäfer's avatar
Jonny Schäfer committed
356
357
358
359
360
361
362
363
364
365
	id, titles, urls, durations, users, dates, views := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
	for i, v := range videos {
		id = append(id, strconv.Itoa(i))
		titles = append(titles, cliTrimLength(v.title, " >>", 55))
		urls = append(urls, v.url)
		durations = append(durations, v.duration)
		users = append(users, v.user)
		dates = append(dates, v.date)
		views = append(views, v.views)
	}
Jonny Schäfer's avatar
Jonny Schäfer committed
366
	cliTable(highlight, id, titles, durations, users, views, dates)
Jonny Schäfer's avatar
Jonny Schäfer committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
}

// logicGetPlayer gets the default video player
func logicGetPlayer() string {
	player, err := storeReadFile(storeDefaultPlayer)
	errorCare(err)

	return strings.Replace(player, "\n", "", -1)
}

// logicSetPlayer sets the default video player
func logicSetPlayer(player string) {
	errorCare(storeWriteFile(storeDefaultPlayer, player))
	cliStatus("default player set to '" + player + "'")
	cliClearDown()
}

Jonny Schäfer's avatar
Jonny Schäfer committed
384
385
386
// logicPlay plays the video with the given video id in the player
func logicPlay(player string, id int) {
	video := logicVarVideos[id]
Jonny Schäfer's avatar
Jonny Schäfer committed
387
	cmd := append(strings.Split(player, " "), "https://www.youtube.com/watch?v="+video.url)
388
389
390
391
392
393
394
395
396

	script := filepath.Join(storeDefaultScripts, cmd[0])
	exists, err := storeFileExists(script)
	errorCare(err)
	if exists {
		cmd[0] = script
		player = "$" + player // visual indicator for user script
	}

Jonny Schäfer's avatar
Jonny Schäfer committed
397
398
399
400
	exe := exec.Command(cmd[0], cmd[1:]...)
	if err := exe.Start(); err != nil {
		cliStatusError(fmt.Sprint("playback error: ", err))
	} else {
Jonny Schäfer's avatar
Jonny Schäfer committed
401
402
		cliStatus("[" + player + "] " + video.title)
		logicList(id, logicVarVideos)
Jonny Schäfer's avatar
Jonny Schäfer committed
403
404
405
	}
	go exe.Wait()
}