diff --git a/client/client.go b/client/client.go index 56ee8b4..adbdca2 100644 --- a/client/client.go +++ b/client/client.go @@ -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) + } } } diff --git a/main.go b/main.go index bf8b76c..1008e53 100644 --- a/main.go +++ b/main.go @@ -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) }, diff --git a/utils/utils.go b/utils/utils.go index 772096d..3cf31c3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 +} diff --git a/views/playlistview.go b/views/playlistview.go index 8166994..97880bc 100644 --- a/views/playlistview.go +++ b/views/playlistview.go @@ -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) + } + } + } + }() +}