Continuously keep playing till I press "q"
[nimcoon.git] / nimcoon.nim
1 import
2 parseopt,
3 std/[terminal],
4 strutils
5
6 import lib
7
8 proc parseOptions(): CommandLineOptions =
9 var
10 searchQuery = ""
11 musicOnly = false
12 feelingLucky = false
13 fullScreen = false
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
23 of "f", "full-screen": fullScreen = true
24 of cmdEnd:
25 discard
26
27 return (searchQuery, musicOnly, feelingLucky, fullScreen)
28
29
30 proc main() =
31 let
32 player = selectMediaPlayer()
33 (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions()
34
35 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
36 directPlay(searchQuery, player)
37
38 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
39
40 proc getUserInput(): string =
41 if feelingLucky: "0"
42 else:
43 presentVideoOptions(searchResults)
44 stdout.styledWrite(fgYellow, "Choose video number: ")
45 readLine(stdin)
46
47 # This is a pure function with no side effects
48 func buildArgs(number: int): seq[string] =
49 var args = @[searchResults[number].url]
50 if musicOnly: args.add("--no-video")
51 if fullScreen: args.add("--fullscreen")
52 return args
53
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
67
68 when isMainModule:
69 main()