]> njoseph.me Git - nimcoon.git/blob - nimcoon.nim
Modularity and Tests
[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 proc main() =
30 let
31 player = selectMediaPlayer()
32 (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions()
33
34 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
35 directPlay(searchQuery, player)
36
37 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
38
39 let number =
40 if feelingLucky: 0
41 else:
42 presentVideoOptions(searchResults)
43 stdout.styledWrite(fgYellow, "Choose video number: ")
44 parseInt(readLine(stdin))
45
46 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
47
48 # This is a pure function with no side effects
49 func buildArgs(): seq[string] =
50 var args = @[searchResults[number].url]
51 if musicOnly: args.add("--no-video")
52 if fullScreen: args.add("--fullscreen")
53 return args
54
55 # Play the video using the preferred/available media player
56 play(player, buildArgs())
57
58 when isMainModule:
59 main()