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.
This commit is contained in:
aditya-K2 2021-11-13 11:32:00 +05:30
parent c19a053126
commit e4ed3bbab7
4 changed files with 52 additions and 57 deletions

5
App.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -16,9 +15,9 @@ type Application struct {
pBar *progressBar 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() expandedView := tview.NewTable()
Navbar := tview.NewTable() Navbar := tview.NewTable()
searchBar := tview.NewTable() searchBar := tview.NewTable()

View File

@ -3,10 +3,7 @@ package main
import ( import (
"strings" "strings"
"github.com/fhs/gompd/mpd"
// "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
// "fmt"
) )
func getFormattedString(s string, width int) string { func getFormattedString(s string, width int) string {
@ -19,18 +16,18 @@ func getFormattedString(s string, width int) string {
return s return s
} }
func togglePlayBack(connection mpd.Client) error { func togglePlayBack() error {
status, err := connection.Status() status, err := CONN.Status()
if status["state"] == "play" && err == nil { if status["state"] == "play" && err == nil {
connection.Pause(true) CONN.Pause(true)
} else if status["state"] == "pause" && err == nil { } else if status["state"] == "pause" && err == nil {
connection.Play(-1) CONN.Play(-1)
} }
return err return err
} }
func UpdatePlaylist(conn mpd.Client, inputTable *tview.Table) { func UpdatePlaylist(inputTable *tview.Table) {
_playlistAttr, _ := conn.PlaylistInfo(-1, -1) _playlistAttr, _ := CONN.PlaylistInfo(-1, -1)
inputTable.Clear() inputTable.Clear()
for i, j := range _playlistAttr { for i, j := range _playlistAttr {
@ -55,11 +52,11 @@ func join(stringSlice []string) string {
return _s return _s
} }
func Update(conn mpd.Client, f []FileNode, inputTable *tview.Table) { func Update(f []FileNode, inputTable *tview.Table) {
inputTable.Clear() inputTable.Clear()
for i, j := range f { for i, j := range f {
if len(j.children) == 0 { if len(j.children) == 0 {
_songAttributes, err := conn.ListAllInfo(j.absolutePath) _songAttributes, err := CONN.ListAllInfo(j.absolutePath)
if err == nil && _songAttributes[0]["Title"] != "" { if err == nil && _songAttributes[0]["Title"] != "" {
_, _, w, _ := inputTable.GetInnerRect() _, _, w, _ := inputTable.GetInnerRect()
inputTable.SetCell(i, 0, inputTable.SetCell(i, 0,

67
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"log"
"strconv" "strconv"
"time" "time"
@ -11,6 +10,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var CONN *mpd.Client
var Volume int64 var Volume int64
var Random bool var Random bool
var Repeat bool var Repeat bool
@ -19,37 +19,38 @@ var InsidePlaylist bool = true
func main() { func main() {
config.ReadConfig() config.ReadConfig()
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) var mpdConnectionError error
if err != nil { CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
log.Fatalln(err) if mpdConnectionError != nil {
panic(mpdConnectionError)
} }
defer conn.Close() defer CONN.Close()
r := newRenderer() r := newRenderer()
c, _ := conn.CurrentSong() c, _ := CONN.CurrentSong()
if len(c) != 0 { if len(c) != 0 {
r.Start(viper.GetString("MUSIC_DIRECTORY") + c["file"]) r.Start(c["file"])
} else { } else {
r.Start("stop") r.Start("stop")
} }
UI := newApplication(*conn, r) UI := newApplication(r)
fileMap, err := conn.GetFiles() fileMap, err := CONN.GetFiles()
dirTree := generateDirectoryTree(fileMap) dirTree := generateDirectoryTree(fileMap)
UpdatePlaylist(*conn, UI.expandedView) UpdatePlaylist(UI.expandedView)
_v, _ := conn.Status() _v, _ := CONN.Status()
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
Random, _ = strconv.ParseBool(_v["random"]) Random, _ = strconv.ParseBool(_v["random"])
Repeat, _ = strconv.ParseBool(_v["repeat"]) Repeat, _ = strconv.ParseBool(_v["repeat"])
UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
if InsidePlaylist { if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView) UpdatePlaylist(UI.expandedView)
} else { } else {
Update(*conn, dirTree.children, UI.expandedView) Update(dirTree.children, UI.expandedView)
} }
return UI.expandedView.GetInnerRect() return UI.expandedView.GetInnerRect()
}) })
@ -59,53 +60,53 @@ func main() {
r, _ := UI.expandedView.GetSelection() r, _ := UI.expandedView.GetSelection()
if !InsidePlaylist { if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 { if len(dirTree.children[r].children) == 0 {
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1) id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1)
conn.PlayId(id) CONN.PlayId(id)
} else { } else {
Update(*conn, dirTree.children[r].children, UI.expandedView) Update(dirTree.children[r].children, UI.expandedView)
dirTree = &dirTree.children[r] dirTree = &dirTree.children[r]
} }
} else { } else {
conn.Play(r) CONN.Play(r)
} }
}, },
"togglePlayBack": func() { "togglePlayBack": func() {
togglePlayBack(*conn) togglePlayBack()
}, },
"showParentContent": func() { "showParentContent": func() {
if !InsidePlaylist { if !InsidePlaylist {
if dirTree.parent != nil { if dirTree.parent != nil {
Update(*conn, dirTree.parent.children, UI.expandedView) Update(dirTree.parent.children, UI.expandedView)
dirTree = dirTree.parent dirTree = dirTree.parent
} }
} }
}, },
"nextSong": func() { "nextSong": func() {
conn.Next() CONN.Next()
}, },
"clearPlaylist": func() { "clearPlaylist": func() {
conn.Clear() CONN.Clear()
if InsidePlaylist { if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView) UpdatePlaylist(UI.expandedView)
} }
}, },
"previousSong": func() { "previousSong": func() {
conn.Previous() CONN.Previous()
}, },
"addToPlaylist": func() { "addToPlaylist": func() {
if !InsidePlaylist { if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection() r, _ := UI.expandedView.GetSelection()
conn.Add(dirTree.children[r].absolutePath) CONN.Add(dirTree.children[r].absolutePath)
} }
}, },
"toggleRandom": func() { "toggleRandom": func() {
err := conn.Random(!Random) err := CONN.Random(!Random)
if err == nil { if err == nil {
Random = !Random Random = !Random
} }
}, },
"toggleRepeat": func() { "toggleRepeat": func() {
err := conn.Repeat(!Repeat) err := CONN.Repeat(!Repeat)
if err == nil { if err == nil {
Repeat = !Repeat Repeat = !Repeat
} }
@ -116,7 +117,7 @@ func main() {
} else { } else {
Volume -= 10 Volume -= 10
} }
conn.SetVolume(int(Volume)) CONN.SetVolume(int(Volume))
}, },
"increaseVolume": func() { "increaseVolume": func() {
if Volume >= 100 { if Volume >= 100 {
@ -124,17 +125,17 @@ func main() {
} else { } else {
Volume += 10 Volume += 10
} }
conn.SetVolume(int(Volume)) CONN.SetVolume(int(Volume))
}, },
"navigateToFiles": func() { "navigateToFiles": func() {
InsidePlaylist = false InsidePlaylist = false
UI.Navbar.Select(1, 0) UI.Navbar.Select(1, 0)
Update(*conn, dirTree.children, UI.expandedView) Update(dirTree.children, UI.expandedView)
}, },
"navigateToPlaylist": func() { "navigateToPlaylist": func() {
InsidePlaylist = true InsidePlaylist = true
UI.Navbar.Select(0, 0) UI.Navbar.Select(0, 0)
UpdatePlaylist(*conn, UI.expandedView) UpdatePlaylist(UI.expandedView)
}, },
"navigateToMostPlayed": func() { "navigateToMostPlayed": func() {
InsidePlaylist = false InsidePlaylist = false
@ -144,10 +145,10 @@ func main() {
UI.App.Stop() UI.App.Stop()
}, },
"stop": func() { "stop": func() {
conn.Stop() CONN.Stop()
}, },
"updateDB": func() { "updateDB": func() {
_, err = conn.Update("") _, err = CONN.Update("")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -155,7 +156,7 @@ func main() {
"deleteSongFromPlaylist": func() { "deleteSongFromPlaylist": func() {
if InsidePlaylist { if InsidePlaylist {
r, _ := UI.expandedView.GetSelection() r, _ := UI.expandedView.GetSelection()
conn.Delete(r, -1) CONN.Delete(r, -1)
} }
}, },
} }

View File

@ -4,10 +4,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/spf13/viper"
) )
var CurrentSong string var CurrentSong string
@ -26,7 +24,7 @@ type progressBar struct {
// This Function returns a progressBar with a table of two rows // This Function returns a progressBar with a table of two rows
// the First row will contain information about the current Song // the First row will contain information about the current Song
// and the Second one will contain the progressBar // and the Second one will contain the progressBar
func newProgressBar(conn mpd.Client, r *Renderer) *progressBar { func newProgressBar(r *Renderer) *progressBar {
p := progressBar{} p := progressBar{}
a := tview.NewTable(). a := tview.NewTable().
@ -37,8 +35,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
a.SetBorder(true) a.SetBorder(true)
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
p.updateTitle(conn, r) p.updateTitle(r)
p.updateProgress(conn) p.updateProgress()
return p.t.GetInnerRect() return p.t.GetInnerRect()
}) })
@ -48,8 +46,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
return &p return &p
} }
func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) { func (s *progressBar) updateTitle(r *Renderer) {
_currentAttributes, err := conn.CurrentSong() _currentAttributes, err := CONN.CurrentSong()
if err == nil { if err == nil {
song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n" song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
s.t.GetCell(0, 0).Text = song s.t.GetCell(0, 0).Text = song
@ -57,15 +55,15 @@ func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
CurrentSong = "" CurrentSong = ""
r.Send("stop") r.Send("stop")
} else if song != CurrentSong && len(_currentAttributes) != 0 { } else if song != CurrentSong && len(_currentAttributes) != 0 {
r.Send(viper.GetString("music_directory") + _currentAttributes["file"]) r.Send(_currentAttributes["file"])
CurrentSong = song CurrentSong = song
} }
// fmt.Println(len(_currentAttributes)) // fmt.Println(len(_currentAttributes))
} }
} }
func (s *progressBar) updateProgress(conn mpd.Client) { func (s *progressBar) updateProgress() {
_status, err := conn.Status() _status, err := CONN.Status()
_, _, _width, _ := s.t.GetInnerRect() _, _, _width, _ := s.t.GetInnerRect()
el, err1 := strconv.ParseFloat(_status["elapsed"], 8) el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
du, err := strconv.ParseFloat(_status["duration"], 8) du, err := strconv.ParseFloat(_status["duration"], 8)