From ba35766a1106378af1105c52ebdf3c997d6d8c53 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 25 Dec 2019 20:46:34 +0530 Subject: [PATCH] Consolidate playing into one function Signed-off-by: Joseph Nuthalapati --- clitube.nim | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/clitube.nim b/clitube.nim index b15d5a9..e2ddffa 100644 --- a/clitube.nim +++ b/clitube.nim @@ -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() -- 2.43.0