From e4ed3bbab78d39b4f5993b882b6163df04e381f5 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 11:32:00 +0530 Subject: [PATCH] Rename conn -> CONN ( Also made it Global ) Following Changes have been made: 1. conn -> CONN 2. CONN is now a global as a lot of functions were requiring it and passing them as a parameter didn't seem intuitive. 3. In progressBar.go now we are sending only the short path instead of absolute path. --- App.go | 5 ++-- client.go | 19 ++++++-------- main.go | 67 +++++++++++++++++++++++++------------------------- progressBar.go | 18 ++++++-------- 4 files changed, 52 insertions(+), 57 deletions(-) diff --git a/App.go b/App.go index 9da54e8..0ef4492 100644 --- a/App.go +++ b/App.go @@ -1,7 +1,6 @@ package main import ( - "github.com/fhs/gompd/mpd" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -16,9 +15,9 @@ type Application struct { pBar *progressBar } -func newApplication(conn mpd.Client, r *Renderer) *Application { +func newApplication(r *Renderer) *Application { - var pBar *progressBar = newProgressBar(conn, r) + var pBar *progressBar = newProgressBar(r) expandedView := tview.NewTable() Navbar := tview.NewTable() searchBar := tview.NewTable() diff --git a/client.go b/client.go index e262c0d..3fc1cb2 100644 --- a/client.go +++ b/client.go @@ -3,10 +3,7 @@ package main import ( "strings" - "github.com/fhs/gompd/mpd" - // "github.com/gdamore/tcell/v2" "github.com/rivo/tview" - // "fmt" ) func getFormattedString(s string, width int) string { @@ -19,18 +16,18 @@ func getFormattedString(s string, width int) string { return s } -func togglePlayBack(connection mpd.Client) error { - status, err := connection.Status() +func togglePlayBack() error { + status, err := CONN.Status() if status["state"] == "play" && err == nil { - connection.Pause(true) + CONN.Pause(true) } else if status["state"] == "pause" && err == nil { - connection.Play(-1) + CONN.Play(-1) } return err } -func UpdatePlaylist(conn mpd.Client, inputTable *tview.Table) { - _playlistAttr, _ := conn.PlaylistInfo(-1, -1) +func UpdatePlaylist(inputTable *tview.Table) { + _playlistAttr, _ := CONN.PlaylistInfo(-1, -1) inputTable.Clear() for i, j := range _playlistAttr { @@ -55,11 +52,11 @@ func join(stringSlice []string) string { return _s } -func Update(conn mpd.Client, f []FileNode, inputTable *tview.Table) { +func Update(f []FileNode, inputTable *tview.Table) { inputTable.Clear() for i, j := range f { if len(j.children) == 0 { - _songAttributes, err := conn.ListAllInfo(j.absolutePath) + _songAttributes, err := CONN.ListAllInfo(j.absolutePath) if err == nil && _songAttributes[0]["Title"] != "" { _, _, w, _ := inputTable.GetInnerRect() inputTable.SetCell(i, 0, diff --git a/main.go b/main.go index 3d06d48..b23014d 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "log" "strconv" "time" @@ -11,6 +10,7 @@ import ( "github.com/spf13/viper" ) +var CONN *mpd.Client var Volume int64 var Random bool var Repeat bool @@ -19,37 +19,38 @@ var InsidePlaylist bool = true func main() { config.ReadConfig() // Connect to MPD server - conn, err := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) - if err != nil { - log.Fatalln(err) + var mpdConnectionError error + CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) + if mpdConnectionError != nil { + panic(mpdConnectionError) } - defer conn.Close() + defer CONN.Close() r := newRenderer() - c, _ := conn.CurrentSong() + c, _ := CONN.CurrentSong() if len(c) != 0 { - r.Start(viper.GetString("MUSIC_DIRECTORY") + c["file"]) + r.Start(c["file"]) } else { r.Start("stop") } - UI := newApplication(*conn, r) + UI := newApplication(r) - fileMap, err := conn.GetFiles() + fileMap, err := CONN.GetFiles() dirTree := generateDirectoryTree(fileMap) - UpdatePlaylist(*conn, UI.expandedView) + UpdatePlaylist(UI.expandedView) - _v, _ := conn.Status() + _v, _ := CONN.Status() Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) Random, _ = strconv.ParseBool(_v["random"]) Repeat, _ = strconv.ParseBool(_v["repeat"]) UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { if InsidePlaylist { - UpdatePlaylist(*conn, UI.expandedView) + UpdatePlaylist(UI.expandedView) } else { - Update(*conn, dirTree.children, UI.expandedView) + Update(dirTree.children, UI.expandedView) } return UI.expandedView.GetInnerRect() }) @@ -59,53 +60,53 @@ func main() { r, _ := UI.expandedView.GetSelection() if !InsidePlaylist { if len(dirTree.children[r].children) == 0 { - id, _ := conn.AddId(dirTree.children[r].absolutePath, -1) - conn.PlayId(id) + id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1) + CONN.PlayId(id) } else { - Update(*conn, dirTree.children[r].children, UI.expandedView) + Update(dirTree.children[r].children, UI.expandedView) dirTree = &dirTree.children[r] } } else { - conn.Play(r) + CONN.Play(r) } }, "togglePlayBack": func() { - togglePlayBack(*conn) + togglePlayBack() }, "showParentContent": func() { if !InsidePlaylist { if dirTree.parent != nil { - Update(*conn, dirTree.parent.children, UI.expandedView) + Update(dirTree.parent.children, UI.expandedView) dirTree = dirTree.parent } } }, "nextSong": func() { - conn.Next() + CONN.Next() }, "clearPlaylist": func() { - conn.Clear() + CONN.Clear() if InsidePlaylist { - UpdatePlaylist(*conn, UI.expandedView) + UpdatePlaylist(UI.expandedView) } }, "previousSong": func() { - conn.Previous() + CONN.Previous() }, "addToPlaylist": func() { if !InsidePlaylist { r, _ := UI.expandedView.GetSelection() - conn.Add(dirTree.children[r].absolutePath) + CONN.Add(dirTree.children[r].absolutePath) } }, "toggleRandom": func() { - err := conn.Random(!Random) + err := CONN.Random(!Random) if err == nil { Random = !Random } }, "toggleRepeat": func() { - err := conn.Repeat(!Repeat) + err := CONN.Repeat(!Repeat) if err == nil { Repeat = !Repeat } @@ -116,7 +117,7 @@ func main() { } else { Volume -= 10 } - conn.SetVolume(int(Volume)) + CONN.SetVolume(int(Volume)) }, "increaseVolume": func() { if Volume >= 100 { @@ -124,17 +125,17 @@ func main() { } else { Volume += 10 } - conn.SetVolume(int(Volume)) + CONN.SetVolume(int(Volume)) }, "navigateToFiles": func() { InsidePlaylist = false UI.Navbar.Select(1, 0) - Update(*conn, dirTree.children, UI.expandedView) + Update(dirTree.children, UI.expandedView) }, "navigateToPlaylist": func() { InsidePlaylist = true UI.Navbar.Select(0, 0) - UpdatePlaylist(*conn, UI.expandedView) + UpdatePlaylist(UI.expandedView) }, "navigateToMostPlayed": func() { InsidePlaylist = false @@ -144,10 +145,10 @@ func main() { UI.App.Stop() }, "stop": func() { - conn.Stop() + CONN.Stop() }, "updateDB": func() { - _, err = conn.Update("") + _, err = CONN.Update("") if err != nil { panic(err) } @@ -155,7 +156,7 @@ func main() { "deleteSongFromPlaylist": func() { if InsidePlaylist { r, _ := UI.expandedView.GetSelection() - conn.Delete(r, -1) + CONN.Delete(r, -1) } }, } diff --git a/progressBar.go b/progressBar.go index 55953ad..b371ca0 100644 --- a/progressBar.go +++ b/progressBar.go @@ -4,10 +4,8 @@ import ( "fmt" "strconv" - "github.com/fhs/gompd/mpd" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" - "github.com/spf13/viper" ) var CurrentSong string @@ -26,7 +24,7 @@ type progressBar struct { // This Function returns a progressBar with a table of two rows // the First row will contain information about the current Song // and the Second one will contain the progressBar -func newProgressBar(conn mpd.Client, r *Renderer) *progressBar { +func newProgressBar(r *Renderer) *progressBar { p := progressBar{} a := tview.NewTable(). @@ -37,8 +35,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar { a.SetBorder(true) a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { - p.updateTitle(conn, r) - p.updateProgress(conn) + p.updateTitle(r) + p.updateProgress() return p.t.GetInnerRect() }) @@ -48,8 +46,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar { return &p } -func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) { - _currentAttributes, err := conn.CurrentSong() +func (s *progressBar) updateTitle(r *Renderer) { + _currentAttributes, err := CONN.CurrentSong() if err == nil { song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n" s.t.GetCell(0, 0).Text = song @@ -57,15 +55,15 @@ func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) { CurrentSong = "" r.Send("stop") } else if song != CurrentSong && len(_currentAttributes) != 0 { - r.Send(viper.GetString("music_directory") + _currentAttributes["file"]) + r.Send(_currentAttributes["file"]) CurrentSong = song } // fmt.Println(len(_currentAttributes)) } } -func (s *progressBar) updateProgress(conn mpd.Client) { - _status, err := conn.Status() +func (s *progressBar) updateProgress() { + _status, err := CONN.Status() _, _, _width, _ := s.t.GetInnerRect() el, err1 := strconv.ParseFloat(_status["elapsed"], 8) du, err := strconv.ParseFloat(_status["duration"], 8)