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
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{}

View File

@ -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

View File

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

View File

@ -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 {