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