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