moving lastfm.go render.go imageUtils.go to the new render package

This commit is contained in:
aditya-K2 2021-12-22 21:19:37 +05:30
parent 6b7fff82b7
commit 6390039ea9
4 changed files with 45 additions and 23 deletions

21
main.go
View File

@ -1,10 +1,12 @@
package main package main
import ( import (
"github.com/aditya-K2/gomp/ui"
"strconv" "strconv"
"time" "time"
"github.com/aditya-K2/gomp/render"
"github.com/aditya-K2/gomp/ui"
"github.com/aditya-K2/gomp/client" "github.com/aditya-K2/gomp/client"
"github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/gomp/utils"
@ -19,8 +21,8 @@ import (
var ( var (
CONN *mpd.Client CONN *mpd.Client
UI *ui.Application UI *ui.Application
Notify *NotificationServer Notify *ui.NotificationServer
RENDERER *Renderer RENDERER *render.Renderer
Volume int64 Volume int64
Random bool Random bool
Repeat bool Repeat bool
@ -38,8 +40,13 @@ func main() {
panic(mpdConnectionError) panic(mpdConnectionError)
} }
defer CONN.Close() defer CONN.Close()
client.SetConnection(CONN)
ui.SetConnection(CONN)
render.SetConnection(CONN)
cache.SetCacheDir(viper.GetString("CACHE_DIR")) cache.SetCacheDir(viper.GetString("CACHE_DIR"))
RENDERER = newRenderer() RENDERER = render.NewRenderer()
ui.SetRenderer(RENDERER) ui.SetRenderer(RENDERER)
c, _ := CONN.CurrentSong() c, _ := CONN.CurrentSong()
if len(c) != 0 { if len(c) != 0 {
@ -49,12 +56,11 @@ func main() {
} }
UI = ui.NewApplication() UI = ui.NewApplication()
ui.ConnectUI(UI)
fileMap, err := CONN.GetFiles() fileMap, err := CONN.GetFiles()
dirTree := client.GenerateDirectoryTree(fileMap) dirTree := client.GenerateDirectoryTree(fileMap)
client.SetConnection(CONN)
ui.SetConnection(CONN)
client.UpdatePlaylist(UI.ExpandedView) client.UpdatePlaylist(UI.ExpandedView)
_v, _ := CONN.Status() _v, _ := CONN.Status()
@ -64,9 +70,10 @@ func main() {
ArtistTree, err = client.GenerateArtistTree() ArtistTree, err = client.GenerateArtistTree()
ArtistTreeContent := utils.ConvertToArray(ArtistTree) ArtistTreeContent := utils.ConvertToArray(ArtistTree)
Notify = NewNotificationServer() Notify = ui.NewNotificationServer()
Notify.Start() Notify.Start()
client.SetNotificationServer(Notify) client.SetNotificationServer(Notify)
render.SetNotificationServer(Notify)
var SearchContentSlice []interface{} var SearchContentSlice []interface{}

View File

@ -1,6 +1,7 @@
package utils package render
import ( import (
"github.com/aditya-K2/gomp/utils"
"os" "os"
"strings" "strings"
@ -67,17 +68,17 @@ func ExtractImageFromFile(uri string, imagePath string) string {
if strings.HasSuffix(uri, ".mp3") { if strings.HasSuffix(uri, ".mp3") {
imagePath := GetMp3Image(uri, imagePath) imagePath := GetMp3Image(uri, imagePath)
if imagePath == "" { if imagePath == "" {
Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
} else if strings.HasSuffix(uri, ".flac") { } else if strings.HasSuffix(uri, ".flac") {
imagePath := GetFlacImage(uri, imagePath) imagePath := GetFlacImage(uri, imagePath)
if imagePath == "" { if imagePath == "" {
Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
} else { } else {
Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
return imagePath return imagePath

View File

@ -1,4 +1,4 @@
package main package render
import ( import (
"errors" "errors"

View File

@ -1,7 +1,8 @@
package main package render
import ( import (
"github.com/aditya-K2/gomp/ui" "github.com/aditya-K2/gomp/ui"
"github.com/fhs/gompd/mpd"
"image" "image"
"os" "os"
@ -12,9 +13,22 @@ import (
"gitlab.com/diamondburned/ueberzug-go" "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 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. by the Start() function as a go routine.
*/ */
type Renderer struct { type Renderer struct {
@ -24,7 +38,7 @@ type Renderer struct {
/* /*
Returns a new Renderer with a string channel Returns a new Renderer with a string channel
*/ */
func newRenderer() *Renderer { func NewRenderer() *Renderer {
c := make(chan string) c := make(chan string)
return &Renderer{ return &Renderer{
c: c, c: c,
@ -46,11 +60,11 @@ func (self *Renderer) Send(path string) {
and saves resources too. and saves resources too.
*/ */
func openImage(path string, c chan string) { func OpenImage(path string, c chan string) {
fw, fh := utils.GetFontWidth() fw, fh := utils.GetFontWidth()
var im *ueberzug.Image var im *ueberzug.Image
if path != "stop" { if path != "stop" {
extractedImage := getImagePath(path) extractedImage := GetImagePath(path)
img2, _ := GetImg(extractedImage) 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")) 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() im.Clear()
} }
if d != "stop" { if d != "stop" {
openImage(d, c) OpenImage(d, c)
} else { } 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. as argument.
*/ */
func (self *Renderer) Start(path string) { 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 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. 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) a, err := CONN.ListInfo(path)
var extractedImage string var extractedImage string
if err == nil && len(a) != 0 { if err == nil && len(a) != 0 {
@ -86,7 +100,7 @@ func getImagePath(path string) string {
} else { } else {
imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"]) imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"])
absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path 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" { 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) downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath)
if err == nil { if err == nil {