From: Joseph Nuthalapati Date: Wed, 25 Dec 2019 15:16:34 +0000 (+0530) Subject: Consolidate playing into one function X-Git-Tag: 0.1.0~31 X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/commitdiff_plain/ba35766a1106378af1105c52ebdf3c997d6d8c53 Consolidate playing into one function Signed-off-by: Joseph Nuthalapati --- 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()