]> njoseph.me Git - nimcoon.git/blobdiff - cli_tube.nim
Add terminal colors!!!
[nimcoon.git] / cli_tube.nim
index ccf09ae0c0ad618aabc4b26ab9c66732fd1eb6fa..a1ba868a8c0d5c45e045a4b6885a153c7770a451 100644 (file)
@@ -8,20 +8,25 @@ import strutils
 import uri
 import xmltree
 
-# 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"]
 
+# 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 findSupportedPlayer(): string =
-  for player in supported_players:
-    let playerBin = execProcess("which " & player)
-    if playerBin.len != 0:
-      return strip(playerBin)
+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)
@@ -38,7 +43,7 @@ proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
 
 proc presentVideoOptions(searchResults: seq[SearchResult]) =
   for index, (title, url) in searchResults:
-    echo index, ". ", title, "\n", url, "\n"
+    echo index, ". ", title.bold.fgMagenta, "\n", url.fgCyan, "\n"
 
 presentVideoOptions(
   extractTitlesAndUrls(