From f56eb1cf1f5136e984624fe004a5f740b759f47c Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Thu, 23 Dec 2021 20:20:27 +0530 Subject: [PATCH] Removing Globals Globals are no longer needed instead I am connecting each part with ( what were previously ) globals. Also Renaming SetRenderer to ConnectRenderer which describes the name more precisely --- main.go | 36 +++++++++++++----------------------- ui/progressBar.go | 5 +++-- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 0773b2f..aba3124 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/aditya-K2/gomp/ui/notify" "strconv" "time" + "github.com/aditya-K2/gomp/ui/notify" + "github.com/aditya-K2/gomp/render" "github.com/aditya-K2/gomp/ui" @@ -19,22 +20,11 @@ import ( "github.com/spf13/viper" ) -var ( - CONN *mpd.Client - UI *ui.Application - Notify *notify.NotificationServer - RENDERER *render.Renderer - Volume int64 - Random bool - Repeat bool - ArtistTree map[string]map[string]map[string]string -) - func main() { config.ReadConfig() // Connect to MPD server var mpdConnectionError error - CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) + CONN, mpdConnectionError := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) if mpdConnectionError != nil { panic(mpdConnectionError) } @@ -47,16 +37,16 @@ func main() { render.SetConnection(CONN) cache.SetCacheDir(viper.GetString("CACHE_DIR")) - RENDERER = render.NewRenderer() - ui.SetRenderer(RENDERER) + Renderer := render.NewRenderer() + ui.ConnectRenderer(Renderer) c, _ := CONN.CurrentSong() if len(c) != 0 { - RENDERER.Start(c["file"]) + Renderer.Start(c["file"]) } else { - RENDERER.Start("stop") + Renderer.Start("stop") } - UI = ui.NewApplication() + UI := ui.NewApplication() notify.ConnectUI(UI) fileMap, err := CONN.GetFiles() @@ -65,13 +55,13 @@ func main() { client.UpdatePlaylist(UI.ExpandedView) _v, _ := CONN.Status() - Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) - Random, _ = strconv.ParseBool(_v["random"]) - Repeat, _ = strconv.ParseBool(_v["repeat"]) + Volume, _ := strconv.ParseInt(_v["volume"], 10, 64) + Random, _ := strconv.ParseBool(_v["random"]) + Repeat, _ := strconv.ParseBool(_v["repeat"]) - ArtistTree, err = client.GenerateArtistTree() + ArtistTree, err := client.GenerateArtistTree() ArtistTreeContent := utils.ConvertToArray(ArtistTree) - Notify = notify.NewNotificationServer() + Notify := notify.NewNotificationServer() Notify.Start() client.SetNotificationServer(Notify) render.SetNotificationServer(Notify) diff --git a/ui/progressBar.go b/ui/progressBar.go index 7b824a2..a7d2558 100644 --- a/ui/progressBar.go +++ b/ui/progressBar.go @@ -2,9 +2,10 @@ package ui import ( "fmt" - "github.com/fhs/gompd/mpd" "strconv" + "github.com/fhs/gompd/mpd" + "github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/tview" @@ -21,7 +22,7 @@ func SetConnection(c *mpd.Client) { CONN = c } -func SetRenderer(r interface{ Send(string) }) { +func ConnectRenderer(r interface{ Send(string) }) { RENDERER = r }