2021-10-12 18:30:16 +00:00
|
|
|
// Demo code for the TextView primitive.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-14 16:34:16 +00:00
|
|
|
"log"
|
2021-10-17 17:00:57 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-10-14 16:34:16 +00:00
|
|
|
"github.com/fhs/gompd/mpd"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-10-17 17:00:57 +00:00
|
|
|
"github.com/rivo/tview"
|
2021-10-12 18:30:16 +00:00
|
|
|
)
|
|
|
|
|
2021-10-17 17:00:57 +00:00
|
|
|
var Volume int64
|
|
|
|
var Random bool
|
|
|
|
var Repeat bool
|
2021-10-18 02:21:57 +00:00
|
|
|
var InsidePlaylist bool = true
|
2021-10-17 17:00:57 +00:00
|
|
|
|
|
|
|
func main() {
|
2021-10-14 16:34:16 +00:00
|
|
|
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-10-17 17:00:57 +00:00
|
|
|
var pBar *progressBar = newProgressBar(*conn)
|
2021-10-14 16:34:16 +00:00
|
|
|
expandedView := tview.NewTable()
|
2021-10-17 17:00:57 +00:00
|
|
|
Navbar := tview.NewTable()
|
|
|
|
searchBar := tview.NewTable()
|
|
|
|
|
2021-10-18 02:21:57 +00:00
|
|
|
searchBar.SetBorder(true).SetTitle("Search").SetTitleAlign(tview.AlignLeft)
|
2021-10-17 17:00:57 +00:00
|
|
|
Navbar.SetBorder(true)
|
|
|
|
Navbar.SetSelectable(true, false)
|
2021-10-18 02:21:57 +00:00
|
|
|
Navbar.SetCell(0, 0, tview.NewTableCell("PlayList"))
|
|
|
|
Navbar.SetCell(1, 0, tview.NewTableCell("Files"))
|
2021-10-17 17:00:57 +00:00
|
|
|
Navbar.SetCell(2, 0, tview.NewTableCell("Most Played"))
|
2021-10-14 16:34:16 +00:00
|
|
|
|
|
|
|
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
|
2021-10-17 17:00:57 +00:00
|
|
|
AddItem(searchBar, 0, 1, false).
|
|
|
|
AddItem(Navbar, 0, 7, false)
|
2021-10-12 18:30:16 +00:00
|
|
|
|
2021-10-14 16:34:16 +00:00
|
|
|
sNavExpViewFlex := tview.NewFlex().
|
2021-10-17 17:00:57 +00:00
|
|
|
AddItem(searchNavFlex, 0, 1, false).
|
|
|
|
AddItem(expandedView, 0, 4, false)
|
2021-10-14 16:34:16 +00:00
|
|
|
|
|
|
|
mainFlex := tview.NewFlex().SetDirection(tview.FlexRow).
|
2021-10-17 17:00:57 +00:00
|
|
|
AddItem(sNavExpViewFlex, 0, 8, false).
|
|
|
|
AddItem(pBar.t, 0, 1, false)
|
2021-10-14 16:34:16 +00:00
|
|
|
|
2021-10-17 17:00:57 +00:00
|
|
|
App := tview.NewApplication().SetRoot(mainFlex, true).SetFocus(expandedView)
|
|
|
|
|
|
|
|
expandedView.SetBorderPadding(1, 1, 1, 1).SetBorder(true)
|
2021-10-14 16:34:16 +00:00
|
|
|
expandedView.SetSelectable(true, false)
|
|
|
|
|
2021-10-18 02:21:57 +00:00
|
|
|
fileMap, err := conn.GetFiles()
|
|
|
|
dirTree := generateDirectoryTree(fileMap)
|
2021-10-17 17:00:57 +00:00
|
|
|
|
2021-10-18 02:21:57 +00:00
|
|
|
UpdatePlaylist(*conn, expandedView)
|
2021-10-17 17:00:57 +00:00
|
|
|
|
2021-10-18 02:21:57 +00:00
|
|
|
_v, _ := conn.Status()
|
|
|
|
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
|
|
|
|
Random, _ = strconv.ParseBool(_v["random"])
|
|
|
|
Repeat, _ = strconv.ParseBool(_v["repeat"])
|
2021-10-17 17:00:57 +00:00
|
|
|
|
|
|
|
expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
|
2021-10-18 02:21:57 +00:00
|
|
|
switch e.Rune() {
|
|
|
|
case 108: // L : Key
|
|
|
|
{
|
|
|
|
r, _ := expandedView.GetSelection()
|
|
|
|
if !InsidePlaylist {
|
|
|
|
if len(dirTree.children[r].children) == 0 {
|
|
|
|
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1)
|
|
|
|
conn.PlayId(id)
|
|
|
|
} else {
|
|
|
|
Update(*conn, dirTree.children[r].children, expandedView)
|
|
|
|
dirTree = &dirTree.children[r]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn.Play(r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 112: // P : Key
|
|
|
|
{
|
|
|
|
togglePlayBack(*conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 104: // H : Key
|
|
|
|
{
|
|
|
|
if !InsidePlaylist {
|
|
|
|
if dirTree.parent != nil {
|
|
|
|
Update(*conn, dirTree.parent.children, expandedView)
|
|
|
|
dirTree = dirTree.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 110: // N : Key
|
|
|
|
{
|
|
|
|
conn.Next()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 99: // C : Key
|
|
|
|
{
|
|
|
|
conn.Clear()
|
|
|
|
if InsidePlaylist {
|
|
|
|
UpdatePlaylist(*conn, expandedView)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 78: // Shift - N : Key
|
|
|
|
{
|
|
|
|
conn.Previous()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 97: // A : Key
|
|
|
|
{
|
|
|
|
if !InsidePlaylist {
|
|
|
|
r, _ := expandedView.GetSelection()
|
|
|
|
conn.Add(dirTree.children[r].absolutePath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 122: // Z : Key
|
|
|
|
{
|
|
|
|
err := conn.Random(!Random)
|
|
|
|
if err == nil {
|
|
|
|
Random = !Random
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 114: // R : Key
|
|
|
|
{
|
|
|
|
err := conn.Repeat(!Repeat)
|
|
|
|
if err == nil {
|
|
|
|
Repeat = !Repeat
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 45: // Minus : Key
|
|
|
|
{
|
|
|
|
if Volume <= 0 {
|
|
|
|
Volume = 0
|
|
|
|
} else {
|
|
|
|
Volume -= 10
|
|
|
|
}
|
|
|
|
conn.SetVolume(int(Volume))
|
|
|
|
return nil
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
2021-10-18 02:21:57 +00:00
|
|
|
case 61: // Plus : Key
|
|
|
|
{
|
|
|
|
if Volume >= 100 {
|
|
|
|
Volume = 100
|
|
|
|
} else {
|
|
|
|
Volume += 10
|
|
|
|
}
|
|
|
|
conn.SetVolume(int(Volume))
|
|
|
|
return nil
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
2021-10-18 02:21:57 +00:00
|
|
|
case 50: // 2 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = false
|
|
|
|
Navbar.Select(1, 0)
|
|
|
|
Update(*conn, dirTree.children, expandedView)
|
|
|
|
return nil
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
2021-10-18 02:21:57 +00:00
|
|
|
case 49: // 1 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = true
|
|
|
|
Navbar.Select(0, 0)
|
|
|
|
UpdatePlaylist(*conn, expandedView)
|
|
|
|
return nil
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
2021-10-18 02:21:57 +00:00
|
|
|
case 51: // 3 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = false
|
|
|
|
Navbar.Select(2, 0)
|
|
|
|
return nil
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
2021-10-18 02:21:57 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
return e
|
2021-10-17 17:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
App.Draw()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := App.Run(); err != nil {
|
2021-10-12 18:30:16 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|