Use std/[terminal] library for CLI colors

Moar colors!!!
Signed-off-by: Joseph Nuthalapati's avatarJoseph Nuthalapati <[email protected]>
parent efcc0441
......@@ -3,6 +3,7 @@ import httpClient
import os
import osproc
import sequtils, sugar
import std/[terminal]
import strtabs
import strutils
import uri
......@@ -14,13 +15,6 @@ let supportedPlayers = @["mpv", "mplayer", "vlc"]
# Only show these many results
let limit = 10
# 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 =
......@@ -35,8 +29,8 @@ proc getYoutubePage(searchQuery: string): string =
let queryParam = encodeUrl(searchQuery)
var client = newHttpClient()
let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & queryParam)
writeFile("/tmp/cli-tube-page.html", response.body)
return "/tmp/cli-tube-page.html"
writeFile("/tmp/clitube-page.html", response.body)
return "/tmp/clitube-page.html"
proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
loadHtml(htmlFile).findAll("a").
......@@ -46,17 +40,17 @@ proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
proc presentVideoOptions(searchResults: seq[SearchResult]) =
echo ""
for index, (title, url) in searchResults:
echo index, ". ", title.bold.fgMagenta, "\n", url.fgCyan, "\n"
styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
let searchResults = extractTitlesAndUrls(getYoutubePage(paramStr(1)))
presentVideoOptions(searchResults)
stdout.write "Choose video number: "
stdout.styledWrite(fgYellow, "Choose video number: ")
var number: int = parseInt(readLine(stdin))
var player = selectMediaPlayer()
echo "\n", "Playing ", searchResults[number].title.bold.fgMagenta
styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
# Play the video in the media player
# Play the video using the preferred/available media player
discard execProcess(player & " " & searchResults[number].url)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment