Merge pull request #35 from aditya-K2/playlist

Update to Playlist
This commit is contained in:
Aditya Kurdunkar 2022-09-20 19:09:21 +05:30 committed by GitHub
commit c95f194a90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 31 deletions

View File

@ -111,27 +111,31 @@ func PrintArtistTree(a map[string]map[string]map[string]string) {
// Adds All tracks from a specified album to a playlist
func AddAlbum(a map[string]map[string]map[string]string, alb string, artist string) {
clist := Conn.BeginCommandList()
for _, v := range a[artist][alb] {
err := Conn.Add(v)
if err != nil {
notify.Notify.Send("Could Not Add Song : " + v)
}
clist.Add(v)
}
if err := clist.End(); err != nil {
notify.Notify.Send("Could Not Add Album : " + alb)
} else {
notify.Notify.Send("Album Added: " + alb)
}
notify.Notify.Send("Album Added : " + alb)
}
// Adds All tracks from a specified artist to a playlist
func AddArtist(a map[string]map[string]map[string]string, artist string) {
clist := Conn.BeginCommandList()
if val, ok := a[artist]; ok {
for _, v := range val {
for _, path := range v {
err := Conn.Add(path)
if err != nil {
notify.Notify.Send("Could Not Add Song : " + path)
}
clist.Add(path)
}
}
notify.Notify.Send("Artist Added : " + artist)
if err := clist.End(); err != nil {
notify.Notify.Send("Could Not Add Artist : " + artist)
} else {
notify.Notify.Send("Artist Added: " + artist)
}
}
}

25
main.go
View File

@ -22,16 +22,7 @@ import (
func main() {
config.ReadConfig()
var mpdConnectionError error
del := ""
nt := viper.GetString("NETWORK_TYPE")
port := viper.GetString("MPD_PORT")
if nt == "tcp" {
del = ":"
} else if nt == "unix" && port != "" {
port = ""
}
client.Conn, mpdConnectionError = mpd.Dial(nt,
viper.GetString("NETWORK_ADDRESS")+del+port)
client.Conn, mpdConnectionError = mpd.Dial(utils.GetNetwork())
if mpdConnectionError != nil {
utils.Print("RED", "Could Not Connect to MPD Server\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
@ -60,9 +51,6 @@ func main() {
// Generating the Directory Tree for File Navigation.
client.DirTree = client.GenerateDirectoryTree(fileMap)
// Default View upon Opening is of Playlist.
views.PView.Update(ui.Ui.ExpandedView)
var Volume int64
var Random, Repeat bool
var SeekOffset = viper.GetInt("SEEK_OFFSET")
@ -120,7 +108,8 @@ func main() {
// This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses
// the Draw Function
views.SetCurrentView(views.PView)
views.PView.StartWatcher()
views.SetCurrentView(&views.PView)
ui.Ui.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
views.GetCurrentView().Update(ui.Ui.ExpandedView)
return ui.Ui.ExpandedView.GetInnerRect()
@ -150,7 +139,11 @@ func main() {
if err := Conn.Clear(); err != nil {
notify.Notify.Send("Could not Clear the Playlist")
} else {
notify.Notify.Send("Playlist Cleared")
if views.PView.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil {
utils.Print("RED", "Couldn't get the current Playlist.\n")
panic(err)
}
notify.Notify.Send("Playlist Cleared!")
}
},
"previousSong": func() {
@ -197,7 +190,7 @@ func main() {
views.FView.Update(ui.Ui.ExpandedView)
},
"navigateToPlaylist": func() {
views.SetCurrentView(views.PView)
views.SetCurrentView(&views.PView)
ui.Ui.Navbar.Select(0, 0)
views.PView.Update(ui.Ui.ExpandedView)
},

View File

@ -8,6 +8,8 @@ import (
"strings"
"syscall"
"unsafe"
"github.com/spf13/viper"
)
type winsize struct {
@ -174,3 +176,15 @@ func Unique(intSlice []int) []int {
}
return list
}
func GetNetwork() (string, string) {
del := ""
nt := viper.GetString("NETWORK_TYPE")
port := viper.GetString("MPD_PORT")
if nt == "tcp" {
del = ":"
} else if nt == "unix" && port != "" {
port = ""
}
return nt, viper.GetString("NETWORK_ADDRESS") + del + port
}

View File

@ -6,10 +6,12 @@ import (
"github.com/aditya-K2/gomp/ui"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/fhs/gompd/v2/mpd"
"github.com/gdamore/tcell/v2"
)
type PlaylistView struct {
Playlist []mpd.Attrs
}
func (s PlaylistView) GetViewName() string {
@ -47,21 +49,24 @@ func (p PlaylistView) Quit() {
func (p PlaylistView) FocusBuffSearchView() {}
func (p PlaylistView) DeleteSongFromPlaylist() {
func (p *PlaylistView) DeleteSongFromPlaylist() {
UI := ui.Ui
CONN := client.Conn
r, _ := UI.ExpandedView.GetSelection()
if err := CONN.Delete(r, -1); err != nil {
notify.Notify.Send("Could not Remove the Song from Playlist")
} else {
if p.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil {
utils.Print("RED", "Couldn't get the current Playlist.\n")
panic(err)
}
}
}
func (p PlaylistView) Update(inputTable *tview.Table) {
CONN := client.Conn
_playlistAttr, _ := CONN.PlaylistInfo(-1, -1)
inputTable.Clear()
for i, j := range _playlistAttr {
for i, j := range p.Playlist {
_, _, w, _ := inputTable.GetInnerRect()
if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" {
inputTable.SetCell(i, 0,
@ -80,3 +85,38 @@ func (p PlaylistView) Update(inputTable *tview.Table) {
}
}
}
func (p *PlaylistView) StartWatcher() {
var err error
if p.Playlist == nil {
if p.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil {
utils.Print("RED", "Watcher couldn't get the current Playlist.\n")
panic(err)
}
}
nt, addr := utils.GetNetwork()
w, err := mpd.NewWatcher(nt, addr, "", "playlist")
if err != nil {
utils.Print("RED", "Could Not Start Watcher.\n")
utils.Print("GREEN", "Please check your MPD Info in config File.\n")
panic(err)
}
go func() {
for err := range w.Error {
notify.Notify.Send(err.Error())
}
}()
go func() {
for subsystem := range w.Event {
if subsystem == "playlist" {
if p.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil {
utils.Print("RED", "Watcher couldn't get the current Playlist.\n")
panic(err)
}
}
}
}()
}