From fdd390a7312122646643693beb27e75a05388769 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Tue, 13 Sep 2022 22:04:39 +0530 Subject: [PATCH 1/3] Fixing Slow down of Playlists mentioned @ #20 As mentioned at https://github.com/aditya-K2/gomp/issues/20#issuecomment-1142443829 The Slow Down was caused due to constant calls to the MPD Server for playlist info. Using Watcher to handle playlist event changes. Also when In SearchView upon adding Artist/Album due to constant change in playlists there was a slow down. Hence using CommandList for that. --- client/client.go | 24 +++++++++++-------- main.go | 4 +--- views/playlistview.go | 55 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 17 deletions(-) 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..2147a95 100644 --- a/main.go +++ b/main.go @@ -60,9 +60,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,6 +117,7 @@ func main() { // This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses // the Draw Function + 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) diff --git a/views/playlistview.go b/views/playlistview.go index 8166994..9af3cb1 100644 --- a/views/playlistview.go +++ b/views/playlistview.go @@ -6,10 +6,13 @@ 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" + "github.com/spf13/viper" ) type PlaylistView struct { + Playlist []mpd.Attrs } func (s PlaylistView) GetViewName() string { @@ -57,11 +60,12 @@ func (p PlaylistView) DeleteSongFromPlaylist() { } func (p PlaylistView) Update(inputTable *tview.Table) { - CONN := client.Conn - _playlistAttr, _ := CONN.PlaylistInfo(-1, -1) - inputTable.Clear() - for i, j := range _playlistAttr { + cplaylist := make([]mpd.Attrs, len(p.Playlist)) + for k, v := range p.Playlist { + cplaylist[k] = v + } + for i, j := range cplaylist { _, _, w, _ := inputTable.GetInnerRect() if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" { inputTable.SetCell(i, 0, @@ -80,3 +84,46 @@ 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) + } + } + del := "" + nt := viper.GetString("NETWORK_TYPE") + port := viper.GetString("MPD_PORT") + if nt == "tcp" { + del = ":" + } else if nt == "unix" && port != "" { + port = "" + } + + w, err := mpd.NewWatcher(nt, + viper.GetString("NETWORK_ADDRESS")+del+port, "", "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) + } + } + } + }() +} From 503701691b3f9e10bef93361eef7e51822b82cf7 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Wed, 14 Sep 2022 19:51:06 +0530 Subject: [PATCH 2/3] Change deleteSongFromPlaylist and clearPlaylist --- main.go | 10 +++++++--- views/playlistview.go | 14 ++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 2147a95..4b12019 100644 --- a/main.go +++ b/main.go @@ -118,7 +118,7 @@ func main() { // This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses // the Draw Function views.PView.StartWatcher() - views.SetCurrentView(views.PView) + 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() @@ -148,7 +148,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() { @@ -195,7 +199,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/views/playlistview.go b/views/playlistview.go index 9af3cb1..9aab0d4 100644 --- a/views/playlistview.go +++ b/views/playlistview.go @@ -50,22 +50,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) { inputTable.Clear() - cplaylist := make([]mpd.Attrs, len(p.Playlist)) - for k, v := range p.Playlist { - cplaylist[k] = v - } - for i, j := range cplaylist { + for i, j := range p.Playlist { _, _, w, _ := inputTable.GetInnerRect() if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" { inputTable.SetCell(i, 0, From 3effda228b6412bde9cf775acd717556e97485d6 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Wed, 14 Sep 2022 20:42:28 +0530 Subject: [PATCH 3/3] Using utils.GetNetwork --- main.go | 11 +---------- utils/utils.go | 14 ++++++++++++++ views/playlistview.go | 13 ++----------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 4b12019..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") 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 9aab0d4..97880bc 100644 --- a/views/playlistview.go +++ b/views/playlistview.go @@ -8,7 +8,6 @@ import ( "github.com/aditya-K2/tview" "github.com/fhs/gompd/v2/mpd" "github.com/gdamore/tcell/v2" - "github.com/spf13/viper" ) type PlaylistView struct { @@ -95,17 +94,9 @@ func (p *PlaylistView) StartWatcher() { panic(err) } } - del := "" - nt := viper.GetString("NETWORK_TYPE") - port := viper.GetString("MPD_PORT") - if nt == "tcp" { - del = ":" - } else if nt == "unix" && port != "" { - port = "" - } - w, err := mpd.NewWatcher(nt, - viper.GetString("NETWORK_ADDRESS")+del+port, "", "playlist") + 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")