Continuously keep playing till I press "q"
authorJoseph Nuthalapati <njoseph@riseup.net>
Tue, 18 Feb 2020 15:30:59 +0000 (21:00 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Tue, 18 Feb 2020 15:30:59 +0000 (21:00 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
lib.nim
nimcoon.nim

diff --git a/lib.nim b/lib.nim
index de088b403d679784de4b993e0a828af2917434bc..acc4fc12dd33d46cfb13a905a6f54c28f1fd83ed 100644 (file)
--- 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=")
index 5c2dc08520d57900b2bcb2933ba13f51ffcaf48e..ae22576213d2131ac10982ef17158c24a54e7346 100644 (file)
@@ -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()