Use a user-defined type for search results
authorJoseph Nuthalapati <njoseph@riseup.net>
Sat, 5 Oct 2019 08:06:39 +0000 (13:36 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Sat, 5 Oct 2019 08:06:39 +0000 (13:36 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
cli_tube.nim

index 2c29825d91b3fc421e01662d07188effcba1f7d0..ccf09ae0c0ad618aabc4b26ab9c66732fd1eb6fa 100644 (file)
@@ -2,11 +2,11 @@ import htmlparser
 import httpClient
 import os
 import osproc
+import sequtils, sugar
 import strtabs
 import strutils
 import uri
 import xmltree
-import sequtils, sugar
 
 # TODO Pretty colors in terminal
 # import terminal
@@ -15,6 +15,8 @@ import sequtils, sugar
 # TODO Should go into a config file
 let supportedPlayers = @["mpv", "mplayer", "vlc"]
 
+type SearchResult = tuple[title: string, url: string]
+
 proc findSupportedPlayer(): string =
   for player in supported_players:
     let playerBin = execProcess("which " & player)
@@ -29,14 +31,14 @@ proc getYoutubePage(searchQuery: string): string =
   writeFile("/tmp/cli-tube-page.html", response.body)
   return "/tmp/cli-tube-page.html"
 
-proc extractTitlesAndUrls(htmlFile: string): seq[tuple[title: string, url: string]] =
+proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
   loadHtml(htmlFile).findAll("a").
     filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
     map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))
 
-proc presentVideoOptions(titlesAndUrls: seq[tuple[title: string, url: string]]) =
-  for number, (title, url) in titlesAndUrls:
-    echo number, ". ", title, "\n", url, "\n"
+proc presentVideoOptions(searchResults: seq[SearchResult]) =
+  for index, (title, url) in searchResults:
+    echo index, ". ", title, "\n", url, "\n"
 
 presentVideoOptions(
   extractTitlesAndUrls(