implement delete songs from queue

This commit is contained in:
raziman 2020-06-22 13:18:25 +08:00
parent af1ed144e4
commit 0dd9d3ced4
3 changed files with 33 additions and 2 deletions

View File

@ -44,10 +44,12 @@ type Player struct {
app *tview.Application
}
// add new song to the queue
func (p *Player) Push(song string) {
p.queue = append(p.queue, song)
}
// remove first song from the queue
func (p *Player) Pop() (string, error) {
if len(p.queue) == 0 {
@ -60,6 +62,29 @@ func (p *Player) Pop() (string, error) {
return a, nil
}
// remove song from the queue
func (p *Player) Remove(index int) (string, error) {
if index > len(p.queue) - 1 {
return "", errors.New("Index out of range")
}
removed := p.queue[index]
var rest []string
// check if given index is the last element
if index == len(p.queue)-1 {
rest = []string{}
} else {
rest = p.queue[index+1:]
}
p.queue = append(p.queue[:index], rest...)
return removed, nil
}
func (p *Player) Run() {
first, err := p.Pop()

View File

@ -7,7 +7,7 @@ import (
"github.com/rivo/tview"
)
func Queue() *tview.List {
func Queue(player *Player) *tview.List {
list := tview.NewList().
ShowSecondaryText(false)
@ -34,6 +34,11 @@ func Queue() *tview.List {
next()
case 'k':
prev()
case 'd':
index := list.GetCurrentItem()
player.Remove(index)
list.RemoveItem(index)
}
return nil
@ -49,3 +54,4 @@ func Queue() *tview.List {
return list
}

View File

@ -22,7 +22,7 @@ func start(app *tview.Application) {
player := &Player{}
child3 := PlayingBar(app)
child2 := Queue()
child2 := Queue(player)
child1 := Playlist(child2, child3, player)
player.tree = child1