Be a nimble package
[nimcoon.git] / src / nimcoon.nim
1 import
2 parseopt,
3 std/[terminal],
4 strutils
5
6 import config
7 import lib
8
9 proc parseOptions(): CommandLineOptions =
10 var
11 searchQuery = ""
12 musicOnly = false
13 feelingLucky = false
14 fullScreen = false
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
24 of "f", "full-screen": fullScreen = true
25 of cmdEnd:
26 discard
27
28 return (searchQuery, musicOnly, feelingLucky, fullScreen)
29
30
31 proc main() =
32 let
33 player = selectMediaPlayer()
34 (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions()
35
36 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
37 directPlay(searchQuery, player)
38 quit(0)
39
40 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
41
42 proc getUserInput(): string =
43 if feelingLucky: "0"
44 else:
45 presentVideoOptions(searchResults[..(limit-1)])
46 stdout.styledWrite(fgYellow, "Choose video number: ")
47 readLine(stdin)
48
49 # This is a pure function with no side effects
50 func buildArgs(number: int): seq[string] =
51 var args = @[searchResults[number].url]
52 if musicOnly: args.add("--no-video")
53 if fullScreen: args.add("--fullscreen")
54 return args
55
56 while(true):
57 let userInput = getUserInput()
58
59 if userInput == "all":
60 for number in 0..(len(searchResults)):
61 play(player, buildArgs(number), searchResults[number].title)
62
63 if userInput == "q":
64 break
65
66 # Play the video using the preferred/available media player
67 let videoNumber = parseInt(userInput)
68 play(player, buildArgs(videoNumber), searchResults[videoNumber].title)
69 if feelingLucky:
70 break
71
72
73 when isMainModule:
74 main()