Get rid of all global state mutation
authorJoseph Nuthalapati <njoseph@riseup.net>
Wed, 25 Dec 2019 13:11:23 +0000 (18:41 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Wed, 25 Dec 2019 13:11:23 +0000 (18:41 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
clitube.nim

index a9792c454956710cddbc4e15959c654510cfeaa5..b15d5a9a47756692adf75feb416a4e5efee5c019 100644 (file)
@@ -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()