Commit 45210cfa authored by Jonny Schäfer's avatar Jonny Schäfer
Browse files

add list entry highlighting

parent a703cfd9
Pipeline #288 skipped
......@@ -72,8 +72,10 @@ func cliStatusError(status string) {
fmt.Println("\x1b[1;41;37m" + status + "\x1b[0m")
}
// cliList writes a table to the screen
func cliTable(columns ...[]string) {
// cliList writes a table to the screen. The line can be highlighted by
// passing the related line number. If highlight < 0 no line will be
// highlighted.
func cliTable(highlight int, columns ...[]string) {
cliClearDown()
space := make([]int, len(columns))
......@@ -92,7 +94,12 @@ func cliTable(columns ...[]string) {
for i := 0; i < lines; i++ {
for n, c := range columns {
fmt.Printf("\x1b[%vm", ((n+1)%2)*34)
if i == highlight {
fmt.Printf("\x1b[%vm", 31) // red line
} else {
fmt.Printf("\x1b[%vm", ((n+1)%2)*34) // blue columns
}
if i < len(c) {
fmt.Print(c[i] + strings.Repeat(" ", 1+space[n]-cliRealCount(c[i]))) // not good
} else {
......
......@@ -119,7 +119,7 @@ func logicSearchPage(title, page string) {
if len(list) == 0 {
cliStatusError("no results for the given query")
}
logicList(list)
logicList(-1, list)
}
// logicNumber selects the given list number
......@@ -140,7 +140,7 @@ func logicNumber(command string) {
fallthrough
case logicWindowSearch:
if sel < len(logicVarVideos) {
logicPlay(splits[0], player, logicVarVideos[sel]) // non blocking
logicPlay(player, sel) // non blocking
} else {
cliStatusError("error: video id not in list")
}
......@@ -196,7 +196,7 @@ func logicSub(none string) {
logicVarUsers = users
cliStatus("list of subscriptions")
cliTable(id[0], us[0], id[1], us[1], id[2], us[2], id[3], us[3])
cliTable(-1, id[0], us[0], id[1], us[1], id[2], us[2], id[3], us[3])
}
// logicSubAdd adds the given user to the subscription list
......@@ -258,7 +258,7 @@ func logicUserPage(user, page string) {
cliStatus("page " + page + " | user: " + user)
logicVarWindow = logicWindowUser
logicVarUser = user
logicList(videoListUser(user))
logicList(-1, videoListUser(user))
}
// logicNew shows a list of new videos
......@@ -300,12 +300,11 @@ func logicNew(none string) {
videos = videos[0:30] // TODO add multiple pages
}
logicVarVideos = videos
cliHome()
fmt.Println()
cliStatus("newest videos")
logicList(videos)
logicList(-1, videos)
}
// logicSet sets the key to the given value. Format: "key value"
......@@ -349,10 +348,13 @@ func logicQuit(none string) {
os.Exit(0)
}
// logicList shows a video list
func logicList(videos []*videoContainer) {
cliClearDown()
// logicList shows a video list and highlights the line with the given
// line number. If hightlight < 0 no line will be highlighted.
// The global video list logicVarVideos will be set by this function.
func logicList(highlight int, videos []*videoContainer) {
logicVarVideos = videos
cliClearDown()
id, titles, urls, durations, users, dates, views := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
for i, v := range videos {
id = append(id, strconv.Itoa(i))
......@@ -363,7 +365,7 @@ func logicList(videos []*videoContainer) {
dates = append(dates, v.date)
views = append(views, v.views)
}
cliTable(id, titles, durations, users, views, dates)
cliTable(highlight, id, titles, durations, users, views, dates)
}
// logicGetPlayer gets the default video player
......@@ -381,14 +383,16 @@ func logicSetPlayer(player string) {
cliClearDown()
}
// logicPlay plays the video in the given player
func logicPlay(info, player string, video *videoContainer) {
// logicPlay plays the video with the given video id in the player
func logicPlay(player string, id int) {
video := logicVarVideos[id]
cmd := append(strings.Split(player, " "), "https://www.youtube.com/watch?v="+video.url)
exe := exec.Command(cmd[0], cmd[1:]...)
if err := exe.Start(); err != nil {
cliStatusError(fmt.Sprint("playback error: ", err))
} else {
cliStatus("[" + info + "][" + player + "] " + video.title)
cliStatus("[" + player + "] " + video.title)
logicList(id, logicVarVideos)
}
go exe.Wait()
}
Supports Markdown
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