2020-02-29 20:13:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2024-05-05 18:13:57 +00:00
|
|
|
"strings"
|
2020-03-01 19:59:28 +00:00
|
|
|
"time"
|
2020-02-29 20:13:02 +00:00
|
|
|
|
2020-03-01 19:59:28 +00:00
|
|
|
"github.com/acaloiaro/di-tui/app"
|
|
|
|
|
"github.com/acaloiaro/di-tui/config"
|
|
|
|
|
"github.com/acaloiaro/di-tui/context"
|
|
|
|
|
"github.com/acaloiaro/di-tui/difm"
|
2024-01-17 21:08:14 +00:00
|
|
|
"github.com/acaloiaro/di-tui/mpris"
|
2020-03-01 19:59:28 +00:00
|
|
|
"github.com/acaloiaro/di-tui/views"
|
2023-07-08 14:39:12 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2024-01-17 21:08:14 +00:00
|
|
|
"github.com/rivo/tview"
|
2020-11-15 17:05:46 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
"github.com/spf13/viper"
|
2020-02-29 20:13:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ctx *context.AppContext
|
|
|
|
|
|
2025-07-22 21:52:06 +00:00
|
|
|
const VERSION = "1.11.2"
|
2024-10-10 16:02:27 +00:00
|
|
|
|
2020-02-29 20:13:02 +00:00
|
|
|
func main() {
|
2024-05-05 18:13:57 +00:00
|
|
|
ctx = context.CreateAppContext(views.CreateViewContext())
|
|
|
|
|
var err error
|
2024-10-10 16:02:27 +00:00
|
|
|
usernameFlag := pflag.String("username", "", "your di.fm username")
|
|
|
|
|
passwordFlag := pflag.String("password", "", "your di.fm password")
|
|
|
|
|
versionFlag := pflag.Bool("version", false, "print the current di-tui version")
|
|
|
|
|
networkFlag := pflag.String("network", viper.GetString("network.shortname"), "the audioaddict network to connect to")
|
2020-11-15 17:05:46 +00:00
|
|
|
pflag.Parse()
|
|
|
|
|
viper.BindPFlags(pflag.CommandLine)
|
|
|
|
|
|
2024-10-10 16:02:27 +00:00
|
|
|
if *versionFlag {
|
|
|
|
|
fmt.Printf("di-tui %s\n", VERSION)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Network, err = difm.GetNetwork(*networkFlag)
|
2024-05-05 18:13:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
var networks []string
|
|
|
|
|
for network := range difm.Networks {
|
|
|
|
|
networks = append(networks, network)
|
|
|
|
|
}
|
2024-10-10 16:02:27 +00:00
|
|
|
fmt.Printf("Invalid network: %s \nPlease choose from the following: %s\n", *networkFlag, strings.Join(networks, ", "))
|
2024-05-05 18:13:57 +00:00
|
|
|
return
|
|
|
|
|
}
|
2023-06-02 00:04:01 +00:00
|
|
|
|
2024-10-10 16:02:27 +00:00
|
|
|
if *usernameFlag != "" && *passwordFlag != "" {
|
|
|
|
|
err = difm.Authenticate(ctx, *usernameFlag, *passwordFlag)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("unable to authenticate %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-15 17:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-05 18:13:57 +00:00
|
|
|
token := config.GetToken()
|
2020-02-29 20:13:02 +00:00
|
|
|
if token == "" {
|
2020-12-15 00:50:47 +00:00
|
|
|
fmt.Println("Authenticate by running: di-tui --username USER --password PASSWORD\n\n" +
|
|
|
|
|
"Or, visit https://github.com/acaloiaro/di-tui#authenticate for other options.")
|
2020-11-15 17:05:46 +00:00
|
|
|
os.Exit(1)
|
2020-02-29 20:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.DifmToken = token
|
|
|
|
|
|
|
|
|
|
run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run() {
|
2020-03-01 17:40:54 +00:00
|
|
|
configureEventHandling()
|
2020-03-04 23:03:16 +00:00
|
|
|
updateScreenLayout()
|
2023-05-23 22:41:13 +00:00
|
|
|
FetchFavoritesAndChannels()
|
2020-03-01 17:40:54 +00:00
|
|
|
|
2023-07-08 14:39:12 +00:00
|
|
|
ctx.View.Keybindings.Bindings = views.GetKeybindings()
|
|
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
err := ctx.View.App.Run()
|
2020-03-01 17:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
func updateScreenLayout() {
|
|
|
|
|
focusView := ctx.View.App.GetFocus()
|
|
|
|
|
|
2020-03-01 17:40:54 +00:00
|
|
|
main := tview.NewFlex()
|
|
|
|
|
main.SetDirection(tview.FlexRow)
|
|
|
|
|
|
2020-03-01 18:38:51 +00:00
|
|
|
favsAndChannels := tview.NewFlex().
|
|
|
|
|
AddItem(ctx.View.FavoriteList, 0, 1, false).
|
|
|
|
|
AddItem(ctx.View.ChannelList, 0, 2, false).
|
|
|
|
|
SetDirection(tview.FlexRow)
|
|
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
primaryView := tview.NewFlex()
|
|
|
|
|
primaryView.
|
2020-03-01 19:59:28 +00:00
|
|
|
AddItem(favsAndChannels, 30, 0, false).
|
|
|
|
|
AddItem(ctx.View.NowPlaying, 0, 4, false)
|
2020-03-01 17:40:54 +00:00
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
if ctx.ShowStatus {
|
|
|
|
|
main.AddItem(ctx.View.Status, 4, 0, false)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 17:40:54 +00:00
|
|
|
main.
|
2020-03-04 23:03:16 +00:00
|
|
|
AddItem(primaryView, 0, 3, false).
|
2020-03-01 19:59:28 +00:00
|
|
|
AddItem(ctx.View.Keybindings, 4, 0, false)
|
2020-03-01 17:40:54 +00:00
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
if focusView == nil {
|
|
|
|
|
focusView = ctx.View.FavoriteList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.View.App.
|
|
|
|
|
SetRoot(main, true).
|
|
|
|
|
SetFocus(focusView)
|
2020-03-01 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-17 21:08:14 +00:00
|
|
|
// configureEventHandling handles key press events, and regular UI updates such as the currently playing track
|
2020-03-01 17:40:54 +00:00
|
|
|
func configureEventHandling() {
|
2020-02-29 20:13:02 +00:00
|
|
|
ctx.View.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
2024-05-05 18:13:57 +00:00
|
|
|
favoritesEmpty := len(ctx.FavoriteList) == 0
|
|
|
|
|
if favoritesEmpty {
|
|
|
|
|
ctx.View.App.SetFocus(ctx.View.ChannelList)
|
|
|
|
|
}
|
2020-03-01 18:38:51 +00:00
|
|
|
|
2024-05-05 18:13:57 +00:00
|
|
|
focus := ctx.View.App.GetFocus().(*tview.List)
|
2023-10-14 14:57:40 +00:00
|
|
|
switch event.Key() {
|
|
|
|
|
case tcell.KeyEnter:
|
2023-05-23 22:41:13 +00:00
|
|
|
current := focus.GetCurrentItem()
|
2025-09-22 16:20:36 +00:00
|
|
|
if focus != ctx.View.ChannelList && current < len(ctx.FavoriteList) {
|
2023-05-23 22:41:13 +00:00
|
|
|
highlightedFavorite := ctx.FavoriteList[current]
|
|
|
|
|
ctx.HighlightedChannel = difm.FavoriteItemChannel(ctx, highlightedFavorite)
|
2025-09-22 16:20:36 +00:00
|
|
|
} else if current < len(ctx.ChannelList) {
|
2023-10-14 14:57:40 +00:00
|
|
|
ctx.HighlightedChannel = &ctx.ChannelList[current]
|
2023-05-23 22:41:13 +00:00
|
|
|
}
|
2024-01-28 15:16:31 +00:00
|
|
|
app.Play(ctx)
|
2023-10-14 14:57:40 +00:00
|
|
|
case tcell.KeyRune:
|
|
|
|
|
switch event.Rune() {
|
|
|
|
|
case 'c':
|
|
|
|
|
if focus != ctx.View.ChannelList {
|
|
|
|
|
ctx.View.App.SetFocus(ctx.View.ChannelList)
|
|
|
|
|
}
|
|
|
|
|
case 'f':
|
|
|
|
|
if focus != ctx.View.FavoriteList {
|
|
|
|
|
ctx.View.App.SetFocus(ctx.View.FavoriteList)
|
|
|
|
|
}
|
|
|
|
|
case 'q':
|
|
|
|
|
ctx.View.App.Stop()
|
|
|
|
|
case 'j': // scroll down
|
|
|
|
|
current := focus.GetCurrentItem() + 1
|
|
|
|
|
focus.SetCurrentItem(current)
|
|
|
|
|
case 'k': // scroll up
|
|
|
|
|
current := focus.GetCurrentItem()
|
|
|
|
|
if current > 0 {
|
|
|
|
|
focus.SetCurrentItem(current - 1)
|
|
|
|
|
}
|
|
|
|
|
case 'p', 32: // tcell has no constant for the space bar rune (32)
|
|
|
|
|
app.TogglePause(ctx)
|
2020-02-29 20:13:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return event
|
|
|
|
|
})
|
2020-03-01 19:59:28 +00:00
|
|
|
|
|
|
|
|
// keep 'now playing' up to date second-by-second
|
|
|
|
|
go func() {
|
|
|
|
|
c := time.Tick(1 * time.Second)
|
|
|
|
|
for range c {
|
2020-11-15 17:05:46 +00:00
|
|
|
elapsed := time.Since(ctx.View.NowPlaying.Track.StartTime)
|
2020-03-01 19:59:28 +00:00
|
|
|
|
|
|
|
|
// If the current time is past the end of the track, then a new track is playing and the now playing track needs
|
|
|
|
|
// to be refreshed.
|
|
|
|
|
if ctx.View.NowPlaying.Track.Duration > 0 && ctx.View.NowPlaying.Track.Duration < elapsed.Seconds() {
|
2023-10-14 14:57:40 +00:00
|
|
|
app.UpdateNowPlaying(ctx, ctx.CurrentChannel)
|
2020-03-01 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.CurrentChannel != nil && elapsed.Seconds() > 0 {
|
|
|
|
|
ctx.View.NowPlaying.Elapsed = elapsed.Seconds()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.View.App.QueueUpdateDraw(func() {})
|
|
|
|
|
}
|
|
|
|
|
}()
|
2020-03-04 23:03:16 +00:00
|
|
|
|
|
|
|
|
// display the status pane when new status messages arrive
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
status := <-ctx.StatusChannel
|
2025-09-22 16:20:36 +00:00
|
|
|
ctx.ShowStatus = true
|
2020-03-04 23:03:16 +00:00
|
|
|
ctx.View.Status.Message = status.Message
|
|
|
|
|
updateScreenLayout() // add the status pane to the screen
|
|
|
|
|
|
|
|
|
|
<-time.Tick(status.Duration)
|
|
|
|
|
ctx.ShowStatus = false
|
|
|
|
|
updateScreenLayout() // remove the status pane from the screen
|
|
|
|
|
}
|
|
|
|
|
}()
|
2024-01-17 21:08:14 +00:00
|
|
|
|
|
|
|
|
// Start the mpris server for d-bus support
|
|
|
|
|
mpris.Start(ctx)
|
2020-03-01 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 22:41:13 +00:00
|
|
|
func FetchFavoritesAndChannels() {
|
|
|
|
|
ctx.View.ChannelList.Clear()
|
|
|
|
|
ctx.View.FavoriteList.Clear()
|
2020-02-29 20:13:02 +00:00
|
|
|
|
2020-03-04 23:03:16 +00:00
|
|
|
channels := difm.ListChannels(ctx)
|
2024-06-02 17:56:03 +00:00
|
|
|
if len(channels) == 0 {
|
|
|
|
|
ctx.SetStatusMessage("Unable to get the channel list.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 20:13:02 +00:00
|
|
|
for _, chn := range channels {
|
|
|
|
|
ctx.View.ChannelList.AddItem(chn.Name, "", 0, func() {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 18:38:51 +00:00
|
|
|
favorites := difm.ListFavorites(ctx)
|
|
|
|
|
for _, fav := range favorites {
|
2023-10-14 14:57:40 +00:00
|
|
|
ctx.View.FavoriteList.AddItem(fav.Name, "", 0, func() {})
|
2020-03-01 18:38:51 +00:00
|
|
|
}
|
2023-05-23 22:41:13 +00:00
|
|
|
ctx.ChannelList = channels
|
|
|
|
|
ctx.FavoriteList = favorites
|
2024-01-28 15:16:31 +00:00
|
|
|
|
2024-10-09 20:37:59 +00:00
|
|
|
if len(channels) == 0 && len(favorites) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 18:13:57 +00:00
|
|
|
if len(favorites) == 0 {
|
2024-10-09 20:37:59 +00:00
|
|
|
ctx.HighlightedChannel = &channels[0]
|
2024-05-05 18:13:57 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-28 15:16:31 +00:00
|
|
|
// default the highlighted channel to the first favorite; even before users select a channel manually. This way,
|
|
|
|
|
// when di-tui starts and the user presses the "Play" media key, di-tui will start playing the first favorite
|
|
|
|
|
// instead of requiring them to choose the channel to be played
|
|
|
|
|
highlightedFavorite := ctx.FavoriteList[0]
|
|
|
|
|
ctx.HighlightedChannel = difm.FavoriteItemChannel(ctx, highlightedFavorite)
|
2020-02-29 20:13:02 +00:00
|
|
|
}
|