Add option to play "all" the search results
[nimcoon.git] / nimcoon.nim
CommitLineData
6ff2dbac 1import
6ff2dbac 2 parseopt,
6ff2dbac 3 std/[terminal],
d65a1dcf 4 strutils
44978125 5
72720bec 6import config
d65a1dcf 7import lib
44978125 8
121e06b2
JN
9proc parseOptions(): CommandLineOptions =
10 var
11 searchQuery = ""
12 musicOnly = false
13 feelingLucky = false
d1e4d2de 14 fullScreen = false
121e06b2
JN
15
16 for kind, key, value in getopt():
17 case kind
18 of cmdArgument:
19 searchQuery = key
20 of cmdShortOption, cmdLongOption:
21 case key
22 of "m", "music": musicOnly = true
23 of "l", "lucky": feelingLucky = true
d1e4d2de 24 of "f", "full-screen": fullScreen = true
121e06b2
JN
25 of cmdEnd:
26 discard
27
d1e4d2de 28 return (searchQuery, musicOnly, feelingLucky, fullScreen)
121e06b2 29
d807245d 30
121e06b2
JN
31proc main() =
32 let
33 player = selectMediaPlayer()
d1e4d2de 34 (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions()
121e06b2 35
92f80e5a
JN
36 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
37 directPlay(searchQuery, player)
121e06b2
JN
38
39 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
40
d807245d
JN
41 proc getUserInput(): string =
42 if feelingLucky: "0"
121e06b2 43 else:
72720bec 44 presentVideoOptions(searchResults[..(limit-1)])
121e06b2 45 stdout.styledWrite(fgYellow, "Choose video number: ")
d807245d 46 readLine(stdin)
efcc0441 47
c8812b68 48 # This is a pure function with no side effects
d807245d 49 func buildArgs(number: int): seq[string] =
c8812b68
JN
50 var args = @[searchResults[number].url]
51 if musicOnly: args.add("--no-video")
52 if fullScreen: args.add("--fullscreen")
53 return args
d1e4d2de 54
d807245d
JN
55 while(true):
56 let userInput = getUserInput()
72720bec
JN
57
58 if userInput == "all":
59 for number in 0..(len(searchResults)):
60 play(player, buildArgs(number))
61
d807245d
JN
62 if userInput == "q":
63 break
64
65 let videoNumber = parseInt(userInput)
66 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[videoNumber].title
67
68 # Play the video using the preferred/available media player
69 play(player, buildArgs(videoNumber))
70 if feelingLucky:
71 break
72
a2319a3f 73
d65a1dcf
JN
74when isMainModule:
75 main()