Make all sequences immutable
authorJoseph Nuthalapati <njoseph@riseup.net>
Thu, 4 Jun 2020 18:08:21 +0000 (23:38 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Thu, 4 Jun 2020 18:08:21 +0000 (23:38 +0530)
src/lib.nim

index 24aa9305218c02a599af07e93eeb223589b0e071..6153992432c28b5bbcd8f4323b65063aa322e3b1 100644 (file)
@@ -58,14 +58,14 @@ func isPlaylist(url: string): bool =
 
 # This is a pure function with no side effects
 func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
-  var args = @[url]
-  if options["musicOnly"]: args.add("--no-video")
-  if options["fullScreen"]: args.add("--fullscreen")
-  # Playlists are only supported for MPV player
-  if isPlaylist(url) and player == "mpv":
-    let list_arg = url.split('&')[1]
-    args[0] = "https://www.youtube.com/playlist?" & list_arg
-  return args
+  let url =
+    # Playlists are only supported for MPV player
+    if isPlaylist(url) and player == "mpv":
+      "https://www.youtube.com/playlist?" & url.split('&')[1]
+    else: url
+  let musicOnly = if options["musicOnly"]: "--no-video" else: ""
+  let fullScreen = if options["fullScreen"]: "--fullscreen" else: ""
+  return filterIt([url, musicOnly, fullScreen], it != "")
 
 proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
   let args = buildPlayerArgs(url, options, player)
@@ -78,19 +78,13 @@ proc play*(player: string, options: Table[string, bool], url: string, title: str
 
 func buildMusicDownloadArgs*(url: string): seq[string] =
   {.noSideEffect.}:
-    var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
     let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
-    args.add(downloadLocation)
-    args.add(url)
-    return args
+    return @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", downloadLocation, url]
 
 func buildVideoDownloadArgs*(url: string): seq[string] =
   {.noSideEffect.}:
-    var args = @["-f", "best", "-o"]
     let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
-    args.add(downloadLocation)
-    args.add(url)
-    return args
+    return @["-f", "best", "-o", downloadLocation, url]
 
 proc download*(args: openArray[string], title: string) =
   styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title