Merge pull request #45 from acaloiaro/push-ssypyxtzkwuq

Network / Favorite name cleanup
This commit is contained in:
Adriano Caloiaro 2025-11-28 11:03:09 -08:00 committed by GitHub
commit 3c80d43b9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 19 deletions

View file

@ -10,10 +10,11 @@ import (
)
// CreateAppContext creates the application context
func CreateAppContext(view *views.ViewContext) *AppContext {
func CreateAppContext(vc *views.ViewContext) *AppContext {
ctx := &AppContext{
View: view,
View: vc,
StatusChannel: make(chan components.StatusMessage, 10),
Network: vc.Network,
}
return ctx

View file

@ -10,6 +10,7 @@ import (
"log"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
@ -196,15 +197,23 @@ func ListFavorites(ctx *context.AppContext) (favorites []components.FavoriteItem
}
sec := "playlist"
// Playlist files prefix favorites with: 'NETWORKNAME - ', which needs to be removed
networkNamePrefix := fmt.Sprintf("%s - ", strings.ToUpper(ctx.Network.Name))
numEntries := cfg.Section(sec).Key("NumberOfEntries").MustInt(0)
for i := 0; i < numEntries; i++ {
// di.fm's PLS keys begin at 1
k := i + 1
origFavoriteName := cfg.Section(sec).Key(fmt.Sprintf("Title%d", k)).String()
favoriteName := strings.Replace(origFavoriteName, networkNamePrefix, "", 1)
favorites = append(favorites, components.FavoriteItem{
Name: cfg.Section(sec).Key(fmt.Sprintf("Title%d", k)).String(),
Name: favoriteName,
PlaylistURL: cfg.Section(sec).Key(fmt.Sprintf("File%d", k)).String(),
})
}
slices.SortFunc(favorites, func(a components.FavoriteItem, b components.FavoriteItem) int {
return strings.Compare(a.Name, b.Name)
})
return
}
@ -265,11 +274,8 @@ func Stream(url string, ctx *context.AppContext) {
// FavoriteItemChannel identifies the ChannelItem that corresponds with a FavoriteItem
func FavoriteItemChannel(ctx *context.AppContext, favorite components.FavoriteItem) (channel *components.ChannelItem) {
// favorites are prefixed with "<NETWORK-NAME> - <CHANNEL NAME>", e.g. "DI.fm - Liquid Trap"
// shave it off before comparing
prefix := fmt.Sprintf("%s - ", ctx.Network.Name)
for _, chn := range ctx.ChannelList {
if chn.Name == favorite.Name[len(prefix):len(favorite.Name)] {
if chn.Name == favorite.Name {
channel = &chn
return
}

View file

@ -23,7 +23,6 @@ var ctx *context.AppContext
const VERSION = "1.11.4"
func main() {
ctx = context.CreateAppContext(views.CreateViewContext())
var err error
usernameFlag := pflag.String("username", "", "your di.fm username")
passwordFlag := pflag.String("password", "", "your di.fm password")
@ -36,7 +35,7 @@ func main() {
fmt.Printf("di-tui %s\n", VERSION)
return
}
ctx.Network, err = difm.GetNetwork(*networkFlag)
network, err := difm.GetNetwork(*networkFlag)
if err != nil {
var networks []string
for network := range difm.Networks {
@ -61,6 +60,7 @@ func main() {
os.Exit(1)
}
ctx = context.CreateAppContext(views.CreateViewContext(network))
ctx.DifmToken = token
run()
@ -213,8 +213,8 @@ func FetchFavoritesAndChannels() {
}
favorites := difm.ListFavorites(ctx)
for _, fav := range favorites {
ctx.View.FavoriteList.AddItem(fav.Name, "", 0, func() {})
for i, favorite := range favorites {
ctx.View.FavoriteList.InsertItem(i, favorite.Name, "", 0, func() {})
}
ctx.ChannelList = channels
ctx.FavoriteList = favorites

View file

@ -20,6 +20,14 @@ var (
secondaryTextColor tcell.Color
)
const (
channelListTitle = " Channels "
favoritesTitle = " Favorites "
keyboardControlsTitle = " Keyboard Controls "
nowPlayingTitle = " Now Playing"
statusTitle = " Status "
)
func init() {
if config.HasTheme() {
os.Setenv("COLORTERM", "24bit")
@ -47,6 +55,7 @@ type ViewContext struct {
ChannelList *tview.List
FavoriteList *tview.List
Keybindings *KeybindingView
Network *components.Network
NowPlaying *NowPlayingView
Status *StatusView
}
@ -70,6 +79,7 @@ type NowPlayingView struct {
Art string
Channel *components.ChannelItem
Elapsed float64
Network *components.Network
Track components.Track
}
@ -80,13 +90,14 @@ type StatusView struct {
}
// CreateViewContext creates the primary application view of di-tui
func CreateViewContext() *ViewContext {
func CreateViewContext(network *components.Network) *ViewContext {
return &ViewContext{
App: tview.NewApplication(),
ChannelList: createChannelList(),
FavoriteList: createFavoriteList(),
Keybindings: createKeybindings(),
NowPlaying: createNowPlaying(),
Network: network,
NowPlaying: createNowPlaying(network),
Status: createStatusView(),
}
}
@ -172,7 +183,7 @@ func createChannelList() *tview.List {
list.
ShowSecondaryText(false).
SetBorder(true).
SetTitle(" Channels ").
SetTitle(channelListTitle).
SetTitleColor(primaryTextColor).
SetBorderColor(primaryColor)
@ -188,7 +199,7 @@ func createFavoriteList() *tview.List {
ShowSecondaryText(false).
SetBorder(true).
SetBorderColor(primaryColor).
SetTitle(" Favorites ").
SetTitle(favoritesTitle).
SetTitleColor(primaryTextColor)
list.Box.SetBackgroundColor(backgroundColor)
@ -203,21 +214,21 @@ func createKeybindings() *KeybindingView {
SetBorder(true).
SetBorderColor(primaryColor).
SetTitleColor(primaryTextColor).
SetTitle(" Keyboard Controls ")
SetTitle(keyboardControlsTitle)
kbv.SetBorderColor(primaryColor)
return kbv
}
func createNowPlaying() *NowPlayingView {
func createNowPlaying(network *components.Network) *NowPlayingView {
np := &NowPlayingView{
Box: tview.NewBox(),
Channel: &components.ChannelItem{},
Elapsed: 0.0,
}
np.SetTitle(" Now Playing ").
np.SetTitle(fmt.Sprintf("%s | %s ", nowPlayingTitle, network.Name)).
SetBorder(true).
SetBorderColor(primaryColor).
SetTitleColor(primaryTextColor)
@ -230,7 +241,7 @@ func createStatusView() *StatusView {
Box: tview.NewBox(),
Message: "Ready to Play",
}
sv.SetTitle(" Status ")
sv.SetTitle(statusTitle)
sv.SetTitleColor(primaryTextColor)
sv.SetBorder(true)
sv.SetBorderColor(primaryColor)