2021-10-12 12:30:16 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-14 10:34:16 -06:00
|
|
|
"log"
|
2021-10-17 11:00:57 -06:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-10-14 10:34:16 -06:00
|
|
|
"github.com/fhs/gompd/mpd"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-10-12 12:30:16 -06:00
|
|
|
)
|
|
|
|
|
2021-10-17 11:00:57 -06:00
|
|
|
var Volume int64
|
|
|
|
var Random bool
|
|
|
|
var Repeat bool
|
2021-10-17 20:21:57 -06:00
|
|
|
var InsidePlaylist bool = true
|
2021-10-17 11:00:57 -06:00
|
|
|
|
|
|
|
func main() {
|
2021-10-14 10:34:16 -06:00
|
|
|
|
|
|
|
// Connect to MPD server
|
|
|
|
conn, err := mpd.Dial("tcp", "localhost:6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-10-24 01:56:10 -06:00
|
|
|
r := newRenderer()
|
|
|
|
c, _ := conn.CurrentSong()
|
|
|
|
if len(c) != 0 {
|
|
|
|
r.Start(DBDIR + c["file"])
|
|
|
|
} else {
|
|
|
|
r.Start("stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
UI := newApplication(*conn, r)
|
2021-10-14 10:34:16 -06:00
|
|
|
|
2021-10-17 20:21:57 -06:00
|
|
|
fileMap, err := conn.GetFiles()
|
|
|
|
dirTree := generateDirectoryTree(fileMap)
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-10-17 20:43:21 -06:00
|
|
|
UpdatePlaylist(*conn, UI.expandedView)
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-10-17 20:21:57 -06:00
|
|
|
_v, _ := conn.Status()
|
|
|
|
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
|
|
|
|
Random, _ = strconv.ParseBool(_v["random"])
|
|
|
|
Repeat, _ = strconv.ParseBool(_v["repeat"])
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-10-18 00:48:51 -06:00
|
|
|
UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
|
|
|
|
if InsidePlaylist {
|
|
|
|
UpdatePlaylist(*conn, UI.expandedView)
|
|
|
|
} else {
|
|
|
|
Update(*conn, dirTree.children, UI.expandedView)
|
|
|
|
}
|
|
|
|
return UI.expandedView.GetInnerRect()
|
|
|
|
})
|
|
|
|
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
|
2021-10-17 20:21:57 -06:00
|
|
|
switch e.Rune() {
|
|
|
|
case 108: // L : Key
|
|
|
|
{
|
2021-10-17 20:43:21 -06:00
|
|
|
r, _ := UI.expandedView.GetSelection()
|
2021-10-17 20:21:57 -06:00
|
|
|
if !InsidePlaylist {
|
|
|
|
if len(dirTree.children[r].children) == 0 {
|
|
|
|
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1)
|
|
|
|
conn.PlayId(id)
|
|
|
|
} else {
|
2021-10-17 20:43:21 -06:00
|
|
|
Update(*conn, dirTree.children[r].children, UI.expandedView)
|
2021-10-17 20:21:57 -06:00
|
|
|
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 {
|
2021-10-17 20:43:21 -06:00
|
|
|
Update(*conn, dirTree.parent.children, UI.expandedView)
|
2021-10-17 20:21:57 -06:00
|
|
|
dirTree = dirTree.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 110: // N : Key
|
|
|
|
{
|
|
|
|
conn.Next()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 99: // C : Key
|
|
|
|
{
|
|
|
|
conn.Clear()
|
|
|
|
if InsidePlaylist {
|
2021-10-17 20:43:21 -06:00
|
|
|
UpdatePlaylist(*conn, UI.expandedView)
|
2021-10-17 20:21:57 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 78: // Shift - N : Key
|
|
|
|
{
|
|
|
|
conn.Previous()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 97: // A : Key
|
|
|
|
{
|
|
|
|
if !InsidePlaylist {
|
2021-10-17 20:43:21 -06:00
|
|
|
r, _ := UI.expandedView.GetSelection()
|
2021-10-17 20:21:57 -06:00
|
|
|
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 11:00:57 -06:00
|
|
|
}
|
2021-10-17 20:21:57 -06:00
|
|
|
case 61: // Plus : Key
|
|
|
|
{
|
|
|
|
if Volume >= 100 {
|
|
|
|
Volume = 100
|
|
|
|
} else {
|
|
|
|
Volume += 10
|
|
|
|
}
|
|
|
|
conn.SetVolume(int(Volume))
|
|
|
|
return nil
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
2021-10-17 20:21:57 -06:00
|
|
|
case 50: // 2 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = false
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.Navbar.Select(1, 0)
|
|
|
|
Update(*conn, dirTree.children, UI.expandedView)
|
2021-10-17 20:21:57 -06:00
|
|
|
return nil
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
2021-10-17 20:21:57 -06:00
|
|
|
case 49: // 1 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = true
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.Navbar.Select(0, 0)
|
|
|
|
UpdatePlaylist(*conn, UI.expandedView)
|
2021-10-17 20:21:57 -06:00
|
|
|
return nil
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
2021-10-17 20:21:57 -06:00
|
|
|
case 51: // 3 : Key
|
|
|
|
{
|
|
|
|
InsidePlaylist = false
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.Navbar.Select(2, 0)
|
2021-10-17 20:21:57 -06:00
|
|
|
return nil
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
2021-10-23 14:03:20 -06:00
|
|
|
case 113: // q : Key
|
|
|
|
{
|
|
|
|
UI.App.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-24 01:56:10 -06:00
|
|
|
case 115:
|
|
|
|
{
|
|
|
|
conn.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-17 20:21:57 -06:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
return e
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.App.Draw()
|
2021-10-17 11:00:57 -06:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-17 20:43:21 -06:00
|
|
|
if err := UI.App.Run(); err != nil {
|
2021-10-12 12:30:16 -06:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|