From 6390039ea9e915a804e06b97d2d8eeee9a63ab1e Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Wed, 22 Dec 2021 21:19:37 +0530 Subject: [PATCH] moving lastfm.go render.go imageUtils.go to the new render package --- main.go | 21 ++++++++++++------- {utils => render}/imageUtils.go | 9 +++++---- lastfm.go => render/lastfm.go | 2 +- render.go => render/render.go | 36 +++++++++++++++++++++++---------- 4 files changed, 45 insertions(+), 23 deletions(-) rename {utils => render}/imageUtils.go (88%) rename lastfm.go => render/lastfm.go (98%) rename render.go => render/render.go (81%) diff --git a/main.go b/main.go index 2f23608..32f9c91 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( - "github.com/aditya-K2/gomp/ui" "strconv" "time" + "github.com/aditya-K2/gomp/render" + "github.com/aditya-K2/gomp/ui" + "github.com/aditya-K2/gomp/client" "github.com/aditya-K2/gomp/utils" @@ -19,8 +21,8 @@ import ( var ( CONN *mpd.Client UI *ui.Application - Notify *NotificationServer - RENDERER *Renderer + Notify *ui.NotificationServer + RENDERER *render.Renderer Volume int64 Random bool Repeat bool @@ -38,8 +40,13 @@ func main() { panic(mpdConnectionError) } defer CONN.Close() + + client.SetConnection(CONN) + ui.SetConnection(CONN) + render.SetConnection(CONN) + cache.SetCacheDir(viper.GetString("CACHE_DIR")) - RENDERER = newRenderer() + RENDERER = render.NewRenderer() ui.SetRenderer(RENDERER) c, _ := CONN.CurrentSong() if len(c) != 0 { @@ -49,12 +56,11 @@ func main() { } UI = ui.NewApplication() + ui.ConnectUI(UI) fileMap, err := CONN.GetFiles() dirTree := client.GenerateDirectoryTree(fileMap) - client.SetConnection(CONN) - ui.SetConnection(CONN) client.UpdatePlaylist(UI.ExpandedView) _v, _ := CONN.Status() @@ -64,9 +70,10 @@ func main() { ArtistTree, err = client.GenerateArtistTree() ArtistTreeContent := utils.ConvertToArray(ArtistTree) - Notify = NewNotificationServer() + Notify = ui.NewNotificationServer() Notify.Start() client.SetNotificationServer(Notify) + render.SetNotificationServer(Notify) var SearchContentSlice []interface{} diff --git a/utils/imageUtils.go b/render/imageUtils.go similarity index 88% rename from utils/imageUtils.go rename to render/imageUtils.go index ce3dfee..8c802c2 100644 --- a/utils/imageUtils.go +++ b/render/imageUtils.go @@ -1,6 +1,7 @@ -package utils +package render import ( + "github.com/aditya-K2/gomp/utils" "os" "strings" @@ -67,17 +68,17 @@ func ExtractImageFromFile(uri string, imagePath string) string { if strings.HasSuffix(uri, ".mp3") { imagePath := GetMp3Image(uri, imagePath) if imagePath == "" { - Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) + utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) return viper.GetString("DEFAULT_IMAGE_PATH") } } else if strings.HasSuffix(uri, ".flac") { imagePath := GetFlacImage(uri, imagePath) if imagePath == "" { - Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) + utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) return viper.GetString("DEFAULT_IMAGE_PATH") } } else { - Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) + utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) return viper.GetString("DEFAULT_IMAGE_PATH") } return imagePath diff --git a/lastfm.go b/render/lastfm.go similarity index 98% rename from lastfm.go rename to render/lastfm.go index c1e9b86..4185114 100644 --- a/lastfm.go +++ b/render/lastfm.go @@ -1,4 +1,4 @@ -package main +package render import ( "errors" diff --git a/render.go b/render/render.go similarity index 81% rename from render.go rename to render/render.go index 4cbe828..744352a 100644 --- a/render.go +++ b/render/render.go @@ -1,7 +1,8 @@ -package main +package render import ( "github.com/aditya-K2/gomp/ui" + "github.com/fhs/gompd/mpd" "image" "os" @@ -12,9 +13,22 @@ import ( "gitlab.com/diamondburned/ueberzug-go" ) +var ( + CONN *mpd.Client + Notify interface { Send(string) } +) + +func SetConnection(c *mpd.Client) { + CONN = c +} + +func SetNotificationServer(n interface{ Send(string) }) { + Notify = n +} + /* Renderer is just a channel on which we will send the Path to the song whose - Image is to be Rendered. This channel is passed to the openImage which in turn is called + Image is to be Rendered. This channel is passed to the OpenImage which in turn is called by the Start() function as a go routine. */ type Renderer struct { @@ -24,7 +38,7 @@ type Renderer struct { /* Returns a new Renderer with a string channel */ -func newRenderer() *Renderer { +func NewRenderer() *Renderer { c := make(chan string) return &Renderer{ c: c, @@ -46,11 +60,11 @@ func (self *Renderer) Send(path string) { and saves resources too. */ -func openImage(path string, c chan string) { +func OpenImage(path string, c chan string) { fw, fh := utils.GetFontWidth() var im *ueberzug.Image if path != "stop" { - extractedImage := getImagePath(path) + extractedImage := GetImagePath(path) img2, _ := GetImg(extractedImage) im, _ = ueberzug.NewImage(img2, int(float32(ui.IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(ui.IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y")) } @@ -59,25 +73,25 @@ func openImage(path string, c chan string) { im.Clear() } if d != "stop" { - openImage(d, c) + OpenImage(d, c) } else { - openImage("stop", c) + OpenImage("stop", c) } } /* - Initialises the Renderer and calls the go routine openImage and passes the channel + Initialises the Renderer and calls the go routine OpenImage and passes the channel as argument. */ func (self *Renderer) Start(path string) { - go openImage(path, self.c) + go OpenImage(path, self.c) } /* This Function returns the path to the image that is to be rendered it checks first for the image in the cache else it adds the image to the cache and then extracts it and renders it. */ -func getImagePath(path string) string { +func GetImagePath(path string) string { a, err := CONN.ListInfo(path) var extractedImage string if err == nil && len(a) != 0 { @@ -86,7 +100,7 @@ func getImagePath(path string) string { } else { imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"]) absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path - extractedImage = utils.ExtractImageFromFile(absPath, imagePath) + extractedImage = ExtractImageFromFile(absPath, imagePath) if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" { downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath) if err == nil {