From 53d24c5781df11ea22889536f467d9531f76a9f0 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 12 Mar 2023 22:25:58 +0400 Subject: [PATCH] Store current song in a separate variable. A base for most listened song functionality. --- internal/radio/icecast.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/radio/icecast.go b/internal/radio/icecast.go index 967c481..fc57a46 100644 --- a/internal/radio/icecast.go +++ b/internal/radio/icecast.go @@ -22,6 +22,7 @@ const ( ) var ( + currentlyPlaying Song lastPlayedCache []Song lastPlayedCacheMutex sync.Mutex ) @@ -129,12 +130,10 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) { lines := bytes.Split(buf, []byte("\n")) - if len(lines) < 2 { + if len(lines) < 1 { return nil, nil } - lines = lines[:len(lines)] - if len(lines) > n { lines = lines[len(lines)-n:] } @@ -190,10 +189,15 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) { if mask&syscall.IN_MODIFY > 0 { lastPlayedCacheMutex.Lock() if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 { - if len(lastPlayedCache) == n { - lastPlayedCache = append(lastPlayedCache[1:], songs...) + if currentlyPlaying.Time == "" { + currentlyPlaying = songs[0] } else { - lastPlayedCache = append(lastPlayedCache, songs...) + if len(lastPlayedCache) == n { + lastPlayedCache = append(lastPlayedCache[1:], currentlyPlaying) + } else { + lastPlayedCache = append(lastPlayedCache, currentlyPlaying) + } + currentlyPlaying = songs[0] } } lastPlayedCacheMutex.Unlock()