Use "variadic" arguments in SendNotification

This commit is contained in:
aditya-K2
2023-05-01 00:31:38 +05:30
parent 94cb46c274
commit ef57b4597b
5 changed files with 12 additions and 17 deletions

View File

@@ -110,14 +110,14 @@ func (c *CoverArt) RefreshState() {
// Open the Image
uimg, err := getImg(file)
if err != nil {
SendNotification(fmt.Sprintf("Error Rendering Image: %s", err.Error()))
SendNotification("Error Rendering Image: %s", err.Error())
return
}
im, err := ueberzug.NewImage(uimg,
int(ImgX*fw)+config.Config.AdditionalPaddingX,
int(ImgY*fh)+config.Config.AdditionalPaddingY)
if err != nil {
SendNotification(fmt.Sprintf("Error Rendering Image: %s", err.Error()))
SendNotification("Error Rendering Image: %s", err.Error())
return
}
c.image = im

View File

@@ -1,6 +1,7 @@
package ui
import (
"fmt"
"sync"
"time"
@@ -153,13 +154,13 @@ func notify(n *notification) {
}()
}
func SendNotification(text string) {
SendNotificationWithTimer(text, time.Second)
func SendNotification(format string, args ...any) {
SendNotificationWithTimer(time.Second, fmt.Sprintf(format, args...))
}
func SendNotificationWithTimer(text string, t time.Duration) {
func SendNotificationWithTimer(t time.Duration, format string, args ...any) {
go func() {
c <- newNotificationWithTimer(text, t)
c <- newNotificationWithTimer(fmt.Sprintf(format, args...), t)
}()
}
@@ -167,10 +168,10 @@ func SendNotificationWithTimer(text string, t time.Duration) {
// an update message isn't sent over the channel that it returns. The message
// string received over the channel is then displayed for half a second and the
// notification is then removed.
func SendNotificationWithChan(text string) chan string {
func SendNotificationWithChan(format string, args ...any) chan string {
close := make(chan string)
go func() {
c <- newNotificationWithChan(text, close)
c <- newNotificationWithChan(fmt.Sprintf(format, args...), close)
}()
return close
}

View File

@@ -1,8 +1,6 @@
package ui
import (
"fmt"
"github.com/aditya-K2/gspt/spt"
"github.com/gdamore/tcell/v2"
"github.com/zmb3/spotify/v2"
@@ -35,7 +33,7 @@ func (a *AlbumView) Content() func() [][]Content {
if a.currentAlbumID != nil {
if a.currentFullAlbum == nil {
msg := SendNotificationWithChan(fmt.Sprintf("Loading %s....", a.currentAlbumName))
msg := SendNotificationWithChan("Loading %s....", a.currentAlbumName)
al, ch := spt.GetAlbum(*a.currentAlbumID)
go func() {
err := <-ch

View File

@@ -1,8 +1,6 @@
package ui
import (
"fmt"
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/gspt/spt"
"github.com/gdamore/tcell/v2"
@@ -108,7 +106,7 @@ func addToPlaylist(tracks []spotify.ID) {
if len(tracks) > 1 {
s = "s"
}
SendNotification(fmt.Sprintf("Added %d track%s to %s", len(tracks), s, sp.Name))
SendNotification("Added %d track%s to %s", len(tracks), s, sp.Name)
}
})
}

View File

@@ -1,8 +1,6 @@
package ui
import (
"fmt"
"github.com/aditya-K2/gspt/spt"
"github.com/gdamore/tcell/v2"
"github.com/zmb3/spotify/v2"
@@ -33,7 +31,7 @@ func (p *PlaylistView) Content() func() [][]Content {
c := make([][]Content, 0)
if p.currentPlaylist != nil {
if p.currentUserFullPlaylist == nil {
msg := SendNotificationWithChan(fmt.Sprintf("Loading %s....", p.currentPlaylist.Name))
msg := SendNotificationWithChan("Loading %s....", p.currentPlaylist.Name)
pf, ch := spt.GetPlaylist(p.currentPlaylist.ID)
go func() {
err := <-ch