X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/39a495a94d0a6bb74d778c2f62162ad10c50c23b..2d9a92ad0d51695e6a4e7ea457d297b7abe9d4ac:/cli_tube.nim diff --git a/cli_tube.nim b/cli_tube.nim index 2c29825..a1ba868 100644 --- a/cli_tube.nim +++ b/cli_tube.nim @@ -2,24 +2,31 @@ 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 # Supported video players in order of preference -# TODO Should go into a config file let supportedPlayers = @["mpv", "mplayer", "vlc"] -proc findSupportedPlayer(): string = - for player in supported_players: - let playerBin = execProcess("which " & player) - if playerBin.len != 0: - return strip(playerBin) +# Hard-coded terminal colors +proc reset(): string {.procvar.} = "\e[0m" +proc bold*(s: string): string {.procvar.} = "\e[1m" & s & reset() + +proc fgMagenta*(s: string): string {.procvar.} = "\e[35m" & s & reset() +proc fgCyan*(s: string): string {.procvar.} = "\e[36m" & s & reset() + +type SearchResult = tuple[title: string, url: string] + +proc selectMediaPlayer(): string = + let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) + if len(availablePlayers) == 0: + echo "Please install one of the supported media players: ", supportedPlayers + raise newException(OSError, "No supported media player found") + else: + return availablePlayers[0] proc getYoutubePage(searchQuery: string): string = let queryParam = encodeUrl(searchQuery) @@ -29,14 +36,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.bold.fgMagenta, "\n", url.fgCyan, "\n" presentVideoOptions( extractTitlesAndUrls(