Refactoring to be more functional
[nimcoon.git] / src / nimcoon.nim
1 import
2 parseopt,
3 strformat,
4 strutils,
5 tables
6
7 import config
8 import lib
9
10
11 proc parseArguments(): CommandLineOptions =
12 var
13 searchQuery = ""
14 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": 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": options["musicOnly"] = true
23 of "l", "lucky": options["feelingLucky"] = true
24 of "f", "full-screen": options["fullScreen"] = true
25 of "d", "download": options["download"] = true
26 of cmdEnd: discard
27
28 return (searchQuery, options)
29
30
31 proc isValidOptions*(options: Options): bool =
32 # Check for invalid combinations of options
33 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
34 for combination in invalidCombinations:
35 if options[combination[0]] and options[combination[1]]:
36 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
37 return false
38 return true
39
40 proc main() =
41 let
42 player = selectMediaPlayer()
43 (searchQuery, options) = parseArguments()
44
45 if(not isValidOptions(options)):
46 quit(1)
47
48 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
49 if options["download"]:
50 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
51 else:
52 directPlay(sanitizeURL(searchQuery), player, options["musicOnly"])
53 quit(0)
54
55 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
56
57 present(searchResults, options, (0, limit-1), player)
58
59
60 when isMainModule:
61 main()