Consolidate playing into one function
authorJoseph Nuthalapati <njoseph@riseup.net>
Wed, 25 Dec 2019 15:16:34 +0000 (20:46 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Wed, 25 Dec 2019 15:16:34 +0000 (20:46 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
clitube.nim

index b15d5a9a47756692adf75feb416a4e5efee5c019..e2ddffa0ec5f646db81ecb6576c8caea5e992155 100644 (file)
@@ -1,6 +1,7 @@
 import
   htmlparser,
   httpClient,
+  logging,
   parseopt,
   osproc,
   sequtils,
@@ -18,6 +19,9 @@ type
   SearchResult = tuple[title: string, url: string]
   CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool]
 
+let logger = newConsoleLogger()
+setLogFilter(lvlInfo)
+
 proc selectMediaPlayer(): string =
   let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
   if len(availablePlayers) == 0:
@@ -61,14 +65,16 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) =
   for index, (title, url) in searchResults:
     styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
 
+proc play(command: string) =
+  logger.log(lvlDebug, &"Executing: ${command}")
+  discard execProcess(command)
+  quit(0)
+
 proc directPlay(searchQuery: string, player: string) =
-  styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchQuery
   if "watch?" in searchQuery or "videos/watch" in searchQuery :
-    discard execProcess(&"{player} {searchQuery}")
-    quit(0)
+    play(&"{player} {searchQuery}")
   elif searchQuery.startswith("magnet:"):
-    discard execProcess(&"peerflix \"{searchQuery}\" --{player}")
-    quit(0)
+    play(&"peerflix \"{searchQuery}\" --{player}")
 
 proc main() =
   let
@@ -94,6 +100,6 @@ proc main() =
     command.add("--no-video")
 
   # Play the video using the preferred/available media player
-  discard execProcess(command.join(" "))
+  play(command.join(" "))
 
 main()