Better UX for playing all search results

Also fix a bug with looping over video numbers
Signed-off-by: Joseph Nuthalapati's avatarJoseph Nuthalapati <[email protected]>
parent 72720bec
Pipeline #118988072 passed with stages
in 1 minute and 47 seconds
......@@ -17,6 +17,8 @@ type
SearchResult* = tuple[title: string, url: string]
CommandLineOptions* = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool]
let processOptions = {poStdErrToStdOut, poUsePath}
proc selectMediaPlayer*(): string =
let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
if len(availablePlayers) == 0:
......@@ -42,9 +44,10 @@ proc presentVideoOptions*(searchResults: seq[SearchResult]) =
for index, (title, url) in searchResults:
styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
proc play*(player: string, args: openArray[string]) =
proc play*(player: string, args: openArray[string], title: string) =
# poEchoCmd can be added to options for debugging
discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath})
styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
discard execProcess(player, args=args, options=processOptions)
func urlLongen(url: string): string =
url.replace("", "")
......@@ -58,6 +61,6 @@ func sanitizeURL*(url: string): string =
proc directPlay*(searchQuery: string, player: string) =
let url = sanitizeURL(searchQuery)
if searchQuery.startswith("magnet:"):
play("peerflix", args=[url, &"--{player}"])
discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
play(player, args=[url])
discard execProcess("peerflix", args=[url], options=processOptions)
......@@ -35,6 +35,7 @@ proc main() =
if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
directPlay(searchQuery, player)
let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
......@@ -57,16 +58,14 @@ proc main() =
if userInput == "all":
for number in 0..(len(searchResults)):
play(player, buildArgs(number))
play(player, buildArgs(number), searchResults[number].title)
if userInput == "q":
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))
let videoNumber = parseInt(userInput)
play(player, buildArgs(videoNumber), searchResults[videoNumber].title)
if feelingLucky:
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment