Get rid of all global state mutation

Signed-off-by: Joseph Nuthalapati's avatarJoseph Nuthalapati <[email protected]>
parent 02114658
Pipeline #105460582 passed with stage
in 53 seconds
......@@ -14,7 +14,9 @@ import
import preferences
type SearchResult = tuple[title: string, url: string]
type
SearchResult = tuple[title: string, url: string]
CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool]
proc selectMediaPlayer(): string =
let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
......@@ -24,6 +26,25 @@ proc selectMediaPlayer(): string =
else:
return availablePlayers[0]
proc parseOptions(): CommandLineOptions =
var
searchQuery = ""
musicOnly = false
feelingLucky = false
for kind, key, value in getopt():
case kind
of cmdArgument:
searchQuery = key
of cmdShortOption, cmdLongOption:
case key
of "m", "music": musicOnly = true
of "l", "lucky": feelingLucky = true
of cmdEnd:
discard
return (searchQuery, musicOnly, feelingLucky)
proc getYoutubePage(searchQuery: string): string =
let queryParam = encodeUrl(searchQuery)
let client = newHttpClient()
......@@ -40,45 +61,39 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) =
for index, (title, url) in searchResults:
styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
let player = selectMediaPlayer()
var searchQuery = ""
var musicOnly = false
var feelingLucky = false
for kind, key, value in getopt():
case kind
of cmdArgument:
searchQuery = key
of cmdShortOption, cmdLongOption:
case key
of "m", "music": musicOnly = true
of "l", "lucky": feelingLucky = true
of cmdEnd:
discard
if "watch?" in searchQuery or "videos/watch" in searchQuery :
discard execProcess(&"{player} {searchQuery}")
quit(0)
elif searchQuery.startswith("magnet:"):
discard execProcess(&"peerflix \"{searchQuery}\" --{player}")
quit(0)
let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
let number =
if feelingLucky: 0
else:
presentVideoOptions(searchResults)
stdout.styledWrite(fgYellow, "Choose video number: ")
parseInt(readLine(stdin))
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)
elif searchQuery.startswith("magnet:"):
discard execProcess(&"peerflix \"{searchQuery}\" --{player}")
quit(0)
proc main() =
let
player = selectMediaPlayer()
(searchQuery, musicOnly, feelingLucky) = parseOptions()
directPlay(searchQuery, player)
let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
let number =
if feelingLucky: 0
else:
presentVideoOptions(searchResults)
stdout.styledWrite(fgYellow, "Choose video number: ")
parseInt(readLine(stdin))
styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
var command = @[player, searchResults[number].url]
var command = @[player, searchResults[number].url]
if musicOnly:
command.add("--no-video")
if musicOnly:
command.add("--no-video")
# Play the video using the preferred/available media player
discard execProcess(command.join(" "))
# Play the video using the preferred/available media player
discard execProcess(command.join(" "))
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment