From d807245d4d9041d9098ea7bb273debfb0224d0ff Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Tue, 18 Feb 2020 21:00:59 +0530 Subject: [PATCH] Continuously keep playing till I press "q" Signed-off-by: Joseph Nuthalapati --- lib.nim | 1 - nimcoon.nim | 26 ++++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) 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() -- 2.43.0