]> njoseph.me Git - nimcoon.git/blame - src/nimcoon.nim
Minor bug fix
[nimcoon.git] / src / nimcoon.nim
CommitLineData
6ff2dbac 1import
6ff2dbac 2 parseopt,
6f161e0b
JN
3 strformat,
4 strutils,
5 tables
44978125 6
e08e5cbe
JN
7import
8 config,
9 lib,
10 types,
11 youtube
44978125 12
6f161e0b
JN
13
14proc parseArguments(): CommandLineOptions =
121e06b2
JN
15 var
16 searchQuery = ""
26229fac 17 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false, "non-interactive": false})
121e06b2
JN
18
19 for kind, key, value in getopt():
20 case kind
21 of cmdArgument:
22 searchQuery = key
23 of cmdShortOption, cmdLongOption:
24 case key
6f161e0b
JN
25 of "m", "music": options["musicOnly"] = true
26 of "l", "lucky": options["feelingLucky"] = true
27 of "f", "full-screen": options["fullScreen"] = true
28 of "d", "download": options["download"] = true
26229fac 29 of "n", "non-interactive": options["non-interactive"] = true
6f161e0b
JN
30 of cmdEnd: discard
31
26229fac
JN
32 if searchQuery == "":
33 stderr.writeLine "NimCoon doesn't permit browsing. You must provide a search query."
34 quit(1)
35
55b0cae7 36 (searchQuery, options)
121e06b2 37
6f161e0b 38
fabf0294 39proc isValidOptions*(options: Options): bool =
6f161e0b 40 # Check for invalid combinations of options
95683732 41 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
86e6cb72 42 result = true
6f161e0b
JN
43 for combination in invalidCombinations:
44 if options[combination[0]] and options[combination[1]]:
45 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
86e6cb72 46 result = false
121e06b2 47
121e06b2
JN
48proc main() =
49 let
50 player = selectMediaPlayer()
6f161e0b
JN
51 (searchQuery, options) = parseArguments()
52
53 if(not isValidOptions(options)):
54 quit(1)
121e06b2 55
25f5a034 56 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
9a8ef4ad 57 if options["download"]:
811928a7 58 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
9a8ef4ad 59 else:
d36e2201 60 directPlay(sanitizeURL(searchQuery), player, options)
9e6b8568 61 quit(0)
121e06b2 62
e08e5cbe 63 let searchResults = getSearchResults(searchQuery)
26229fac
JN
64 if options["non-interactive"]:
65 for index, (title, url) in searchResults:
66 echo title
67 echo url
68 echo ""
69 quit(0)
70
fac67037 71 let numResults = min(limit, len(searchResults))
121e06b2 72
fac67037 73 present(searchResults, options, (0, numResults-1), player)
d807245d 74
a2319a3f 75
d65a1dcf
JN
76when isMainModule:
77 main()