From b7f7c9cebd80bc5eb50ce33bcd79daaca8f45f94 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Wed, 16 Mar 2022 02:48:53 +0530 Subject: [PATCH] Minor Formatting Changes and better error handling --- render/lastfm.go | 9 ++++++--- render/render.go | 35 ++++++++++++++++++++--------------- ui/app.go | 9 +++++++-- utils/utils.go | 18 ++++-------------- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/render/lastfm.go b/render/lastfm.go index 4185114..02ed927 100644 --- a/render/lastfm.go +++ b/render/lastfm.go @@ -39,8 +39,11 @@ func downloadImage(url string, imagePath string) (string, error) { if err == nil { b, err := os.Create(imagePath) if err == nil { - b.Write(v) - return imagePath, nil + if _, err := b.Write(v); err == nil { + return imagePath, nil + } else { + return "", errors.New("could Not Write Image") + } } else { b.Close() return "", err @@ -49,5 +52,5 @@ func downloadImage(url string, imagePath string) (string, error) { return "", err } } - return "", errors.New("Image Not Received") + return "", errors.New("image Not Received") } diff --git a/render/render.go b/render/render.go index 0515437..5ecff0c 100644 --- a/render/render.go +++ b/render/render.go @@ -27,14 +27,14 @@ 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 -// by the Start() function as a go routine. +// 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 +// by the Start() function as a go routine. type Renderer struct { c chan string } -// Returns a new Renderer with a string channel +// NewRenderer Returns a new Renderer with a string channel func NewRenderer() *Renderer { c := make(chan string) return &Renderer{ @@ -43,13 +43,13 @@ func NewRenderer() *Renderer { } // Send Image Path to Renderer -func (self *Renderer) Send(path string) { - self.c <- path +func (r *Renderer) Send(path string) { + r.c <- path } -// Go Routine that will Be Called and will listen on the channel c +// OpenImage Go Routine that will Be Called and will listen on the channel c // for changes and on getting a string over the channel will open the Image and -// keep listening again. This will keep the image blocked ( i.e no need to use time.Sleep() etc. ) +// keep listening again. This will keep the image blocked ( i.e. no need to use time.Sleep() etc. ) // and saves resources too. func OpenImage(path string, c chan string) { fw, fh := utils.GetFontWidth() @@ -57,7 +57,9 @@ func OpenImage(path string, c chan string) { if path != "stop" { 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")) + im, _ = ueberzug.NewImage(img2, + int(float32(ui.ImgX)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), + int(float32(ui.ImgY)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y")) } d := <-c if im != nil { @@ -70,13 +72,14 @@ func OpenImage(path string, c chan string) { } } -// Initialises the Renderer and calls the go routine OpenImage and passes the channel +// Start 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) +func (r *Renderer) Start(path string) { + go OpenImage(path, r.c) } -// This Function returns the path to the image that is to be rendered it checks first for the image in the cache +// GetImagePath 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 { a, err := CONN.ListInfo(path) @@ -88,7 +91,8 @@ func GetImagePath(path string) string { imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"]) absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path 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) if err == nil { Notify.Send("Image From LastFM") @@ -116,7 +120,8 @@ func GetImg(uri string) (image.Image, error) { } fw, fh := utils.GetFontWidth() img = resize.Resize( - uint(float32(ui.IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(ui.IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))), + uint(float32(ui.ImgW)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), + uint(float32(ui.ImgH)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))), img, resize.Bilinear, ) diff --git a/ui/app.go b/ui/app.go index 633b1e0..dcc7d45 100644 --- a/ui/app.go +++ b/ui/app.go @@ -5,7 +5,12 @@ import ( "github.com/gdamore/tcell/v2" ) -var IMG_X, IMG_Y, IMG_W, IMG_H int +var ( + ImgY int + ImgW int + ImgH int + ImgX int +) type Application struct { App *tview.Application @@ -26,7 +31,7 @@ func NewApplication() *Application { imagePreviewer := tview.NewBox() imagePreviewer.SetBorder(true) imagePreviewer.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { - IMG_X, IMG_Y, IMG_W, IMG_H = imagePreviewer.GetRect() + ImgX, ImgY, ImgW, ImgH = imagePreviewer.GetRect() return imagePreviewer.GetInnerRect() }) diff --git a/utils/utils.go b/utils/utils.go index 51554d2..1bf3f30 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -30,8 +30,8 @@ func GetWidth() *winsize { func GetFontWidth() (float32, float32) { g := GetWidth() - fw := (float32(g.Xpixel) / float32(g.Col)) - fh := (float32(g.Ypixel) / float32(g.Row)) + fw := float32(g.Xpixel) / float32(g.Col) + fh := float32(g.Ypixel) / float32(g.Row) return fw, fh } @@ -109,19 +109,9 @@ func Copy(sourceImage, destinationImage string) error { } } -func Join(stringSlice []string) string { - var _s string = stringSlice[0] - for i := 1; i < len(stringSlice); i++ { - if _s != "" { - _s += ("/" + stringSlice[i]) - } - } - return _s -} - func GetFormattedString(s string, width int) string { if len(s) < width { - s += strings.Repeat(" ", (width - len(s))) + s += strings.Repeat(" ", width-len(s)) } else { s = s[:(width - 2)] s += " " @@ -172,7 +162,7 @@ func GetMatchedString(a []int, s, color string) string { func Unique(intSlice []int) []int { keys := make(map[int]bool) - list := []int{} + var list []int for _, entry := range intSlice { if _, exists := keys[entry]; !exists { keys[entry] = true