Skip to content

Commit

Permalink
more track info
Browse files Browse the repository at this point in the history
  • Loading branch information
MarckTomack committed Jul 11, 2020
1 parent 31587d7 commit 492c275
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 39 deletions.
6 changes: 4 additions & 2 deletions app/clients/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (t *TerminalClient) SetSender(sender senders.Sender) {

func (t *TerminalClient) Run(rootDir string) {
ticker := time.NewTicker(time.Second).C
aliveTicker := time.NewTicker(time.Second * 10).C
defer func() {
t.mainTui.Stop()
t.sender.ShutDown()
Expand All @@ -45,12 +46,13 @@ func (t *TerminalClient) Run(rootDir string) {
t.sender.VolumeUp()
case <-t.mainTui.VolumeDown:
t.sender.VolumeDown()
case <-ticker:
case <-aliveTicker:
if !t.sender.IsAlive() {
t.mainTui.Stop()
fmt.Println("aloneMPd is not alive")
return
}
case <-ticker:
info, ok := t.sender.TrackInfo().(senders.StatusResponse)
if ok {
if info.InError {
Expand All @@ -63,7 +65,7 @@ func (t *TerminalClient) Run(rootDir string) {
}
}
t.mainTui.SetProgDur(info.Progress, info.Duration, info.Length)
t.mainTui.SetTrackInfo(info.TrackInfo.Title, info.TrackInfo.Artist, info.TrackInfo.Album)
t.mainTui.SetTrackInfo(info.TrackInfo)
}
t.mainTui.Draw()
case <-t.mainTui.Quit:
Expand Down
26 changes: 9 additions & 17 deletions app/senders/http-sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ import (
)

type StatusResponse struct {
TrackInfo trackInfo
TrackInfo *util.TrackInfo
Progress string `json:"progress"`
Length int `json:"length"`
Duration string `json:"duration"`
IsPlaying bool `json:"isPlaying"`
InError bool `json:"inError"`
}

type trackInfo struct {
Title string `json:"title"`
Artist string `json:"artist"`
Album string `json:"album"`
}

type HttpSender struct {
baseUrl string
logger *util.Logger
Expand Down Expand Up @@ -57,17 +51,15 @@ func (h *HttpSender) NextTrack() {
}
h.logger.Write(fmt.Sprintf("send next track %s", res.Status))
}
func (h *HttpSender) Play(track interface{}) {
trackToPlay, ok := track.(string)
if ok {
req := fmt.Sprintf("%s/command?send=play&track=%s", h.baseUrl, url.QueryEscape(trackToPlay))
res, err := http.Get(req)
if err != nil {
h.logger.Write(fmt.Sprintf("error on play track: %v", err))
return
}
h.logger.Write(fmt.Sprintf("send play track %s", res.Status))
func (h *HttpSender) Play(track string) {
req := fmt.Sprintf("%s/command?send=play&track=%s", h.baseUrl, url.QueryEscape(track))
res, err := http.Get(req)
if err != nil {
h.logger.Write(fmt.Sprintf("error on play track: %v", err))
return
}
h.logger.Write(fmt.Sprintf("send play track %s", res.Status))

}

func (h *HttpSender) PreviousTrack() {
Expand Down
2 changes: 1 addition & 1 deletion app/senders/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Sender interface {
Pause()
VolumeUp()
VolumeDown()
Play(track interface{})
Play(track string)
TrackInfo() interface{}
ShutDown()
Initialize(source string)
Expand Down
31 changes: 24 additions & 7 deletions app/ui/tui/main-tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tui

import (
"fmt"
"log"
"util"

"github.com/gdamore/tcell"
"gitlab.com/tslocum/cview"
Expand Down Expand Up @@ -62,7 +62,7 @@ func NewMainTui() *MainTui {
return t
}

func (t *MainTui) Run() {
func (t *MainTui) Run() error {
t.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter:
Expand Down Expand Up @@ -91,8 +91,9 @@ func (t *MainTui) Run() {
})
err := t.app.Run()
if err != nil {
log.Println(err)
return err
}
return nil
}

func (t *MainTui) Stop() {
Expand All @@ -113,10 +114,26 @@ func (t *MainTui) setCmdText() {
[yellow](Ctrl+C) Quit[yellow]`)
}

func (t *MainTui) SetTrackInfo(title string, artist string, album string) {
text := fmt.Sprintf("[blue]Title: [white] \n%s\n"+
"[blue]Album: [white] \n%s\n"+
"[blue]Artist: [white] \n%s\n", title, artist, album)
func (t *MainTui) SetTrackInfo(info *util.TrackInfo) {
var text string
if info != nil {
text = fmt.Sprintf("[blue]Title:[white] \n%s\n"+
"[blue]Album:[white] \n%s\n"+
"[blue]Artist:[white] \n%s\n"+
"[blue]AlbumArtist:[white] \n%s\n"+
"[blue]Composer:[white] \n%s\n"+
"[blue]Genre:[white] \n%s\n"+
"[blue]Year:[white] \n%d\n", info.Title, info.Artist, info.Album, info.AlbumArtist, info.Composer, info.Genre, info.Year)
} else {
text = "[blue]Title: \n\n" +
"[blue]Album: \n\n" +
"[blue]Artist: \n\n" +
"[blue]AlbumArtist: \n\n" +
"[blue]Composer: \n\n" +
"[blue]Genre: \n\n" +
"[blue]Year: \n\n"
}

t.trackInfo.SetInfo(text)
}

Expand Down
16 changes: 11 additions & 5 deletions daemon/media/file-player.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ func (f *FilePlayer) loadStreamerAndFormat(file string) error {
if err != nil {
trackInfo = nil
}
playerTrackInfo := new(util.TrackInfo)
if trackInfo != nil {
f.informer.info = &TrackInfo{Title: trackInfo.Title(), Album: trackInfo.Album(), Artist: trackInfo.Artist()}
} else {
f.informer.info = &TrackInfo{Title: "", Album: "", Artist: ""}
playerTrackInfo.Title = trackInfo.Title()
playerTrackInfo.Artist = trackInfo.Artist()
playerTrackInfo.Album = trackInfo.Album()
playerTrackInfo.AlbumArtist = trackInfo.AlbumArtist()
playerTrackInfo.Composer = trackInfo.Composer()
playerTrackInfo.Genre = trackInfo.Genre()
playerTrackInfo.Year = trackInfo.Year()
}
f.informer.info = playerTrackInfo

ex := filepath.Ext(f.fileToPlay.Name())
switch ex {
Expand Down Expand Up @@ -279,7 +285,7 @@ type filePlayerInfo struct {
playing bool
paused bool
muted bool
info *TrackInfo
info *util.TrackInfo
length int
dur string
prog string
Expand All @@ -306,7 +312,7 @@ func (f *filePlayerInfo) IsMuted() bool {
return f.muted
}

func (f *filePlayerInfo) TrackInfo() *TrackInfo {
func (f *filePlayerInfo) TrackInfo() *util.TrackInfo {
return f.info
}

Expand Down
10 changes: 3 additions & 7 deletions daemon/media/player-informer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package media

import "util"

type PlayerInformer interface {
CurrentVolume() float64
IsPlaying() bool
IsPaused() bool
IsMuted() bool
TrackInfo() *TrackInfo
TrackInfo() *util.TrackInfo
PlayingTrack() string
TrackList() []string
NextTrack() string
Expand All @@ -15,9 +17,3 @@ type PlayerInformer interface {
Progress() string
InError() bool
}

type TrackInfo struct {
Title string `json:"title"`
Artist string `json:"artist"`
Album string `json:"album"`
}
11 changes: 11 additions & 0 deletions util/trackinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

type TrackInfo struct {
Title string `json:"title"`
Artist string `json:"artist"`
Album string `json:"album"`
AlbumArtist string `json:"albumArtist"`
Composer string `json:"composer"`
Genre string `json:"genre"`
Year int `json:"year"`
}

0 comments on commit 492c275

Please sign in to comment.