mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-26 13:49:21 +08:00
implement delete songs from queue
This commit is contained in:
parent
af1ed144e4
commit
0dd9d3ced4
25
player.go
25
player.go
@ -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()
|
||||
|
8
queue.go
8
queue.go
@ -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
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user