From: Joseph Nuthalapati Date: Tue, 18 Feb 2020 15:30:59 +0000 (+0530) Subject: Continuously keep playing till I press "q" X-Git-Tag: 0.1.0~13 X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/commitdiff_plain/d807245d4d9041d9098ea7bb273debfb0224d0ff?hp=cb5756d8e831412dab18952e13acda058ff3575d Continuously keep playing till I press "q" Signed-off-by: Joseph Nuthalapati --- diff --git a/lib.nim b/lib.nim index de088b4..acc4fc1 100644 --- a/lib.nim +++ b/lib.nim @@ -45,7 +45,6 @@ proc presentVideoOptions*(searchResults: seq[SearchResult]) = proc play*(player: string, args: openArray[string]) = # poEchoCmd can be added to options for debugging discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath}) - quit(0) func urlLongen(url: string): string = url.replace("youtu.be/", "www.youtube.com/watch?v=") diff --git a/nimcoon.nim b/nimcoon.nim index 5c2dc08..ae22576 100644 --- a/nimcoon.nim +++ b/nimcoon.nim @@ -26,6 +26,7 @@ proc parseOptions(): CommandLineOptions = return (searchQuery, musicOnly, feelingLucky, fullScreen) + proc main() = let player = selectMediaPlayer() @@ -36,24 +37,33 @@ proc main() = let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) - let number = - if feelingLucky: 0 + proc getUserInput(): string = + if feelingLucky: "0" else: presentVideoOptions(searchResults) stdout.styledWrite(fgYellow, "Choose video number: ") - parseInt(readLine(stdin)) - - styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title + readLine(stdin) # This is a pure function with no side effects - func buildArgs(): seq[string] = + func buildArgs(number: int): seq[string] = var args = @[searchResults[number].url] if musicOnly: args.add("--no-video") if fullScreen: args.add("--fullscreen") return args - # Play the video using the preferred/available media player - play(player, buildArgs()) + while(true): + let userInput = getUserInput() + if userInput == "q": + break + + let videoNumber = parseInt(userInput) + styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[videoNumber].title + + # Play the video using the preferred/available media player + play(player, buildArgs(videoNumber)) + if feelingLucky: + break + when isMainModule: main()