Add AudioPlayer
provider
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
abf78da998
commit
a6e71fe527
3 changed files with 107 additions and 1 deletions
|
@ -51,6 +51,7 @@ func run() error {
|
|||
}
|
||||
|
||||
blocks := []i3bar.BlockGenerator{
|
||||
providers.NewAudioPlayer(32),
|
||||
providers.NewIPAddress("wlp0s20f3"),
|
||||
providers.NewWiFi("wlp0s20f3", 75),
|
||||
providers.NewBattery("BAT0", 80, 30, 20),
|
||||
|
|
104
internal/providers/audioPlayer.go
Normal file
104
internal/providers/audioPlayer.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package providers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/codemicro/bar/internal/i3bar"
|
||||
)
|
||||
|
||||
const (
|
||||
musicNoteString = "♪"
|
||||
playerctlExecutable = "playerctl"
|
||||
|
||||
playerStatusStopped = "Stopped"
|
||||
playerStatusPlaying = "Playing"
|
||||
playerStatusPaused = "Paused"
|
||||
playerStatusUnknown = "Unknown"
|
||||
)
|
||||
|
||||
type AudioPlayer struct {
|
||||
ShowTextOnPause bool
|
||||
MaxLabelLen int
|
||||
}
|
||||
|
||||
func NewAudioPlayer(maxLabelLength int) *AudioPlayer {
|
||||
return &AudioPlayer{
|
||||
MaxLabelLen: maxLabelLength,
|
||||
}
|
||||
}
|
||||
|
||||
type playingAudioInfo struct {
|
||||
Track string
|
||||
Artist string
|
||||
Album string
|
||||
Status string
|
||||
}
|
||||
|
||||
func (g *AudioPlayer) getInfo() (*playingAudioInfo, error) {
|
||||
rawMetadataOutput, err := runCommand(playerctlExecutable, "metadata")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := new(playingAudioInfo)
|
||||
|
||||
lines := strings.Split(string(rawMetadataOutput), "\n")
|
||||
for _, line := range lines {
|
||||
splitLine := strings.Fields(line)
|
||||
|
||||
if len(splitLine) < 3 {
|
||||
continue
|
||||
}
|
||||
|
||||
var (
|
||||
// application = splitLine[0]
|
||||
fieldName = splitLine[1]
|
||||
data = strings.Join(splitLine[2:], " ")
|
||||
)
|
||||
|
||||
switch strings.ToLower(fieldName) {
|
||||
case "xesam:artist":
|
||||
info.Artist = data
|
||||
case "xesam:title":
|
||||
info.Track = data
|
||||
case "xesam:album":
|
||||
info.Album = data
|
||||
}
|
||||
}
|
||||
|
||||
rawStatusOutput, err := runCommand(playerctlExecutable, "status")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if x := string(rawStatusOutput); !(x == playerStatusStopped || x == playerStatusPlaying || x == playerStatusPaused) {
|
||||
info.Status = playerStatusUnknown
|
||||
} else {
|
||||
info.Status = x
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (g *AudioPlayer) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
|
||||
info, err := g.getInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b := new(i3bar.Block)
|
||||
b.Name = "audioPlayer"
|
||||
|
||||
b.FullText = musicNoteString
|
||||
|
||||
if info.Status == playerStatusPlaying || (info.Status == playerStatusPaused && g.ShowTextOnPause) {
|
||||
x := fmt.Sprintf("%s - %s", info.Track, info.Artist)
|
||||
if len(x) > g.MaxLabelLen {
|
||||
x = x[:g.MaxLabelLen] + "..."
|
||||
}
|
||||
b.FullText += " " + x
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package providers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
@ -12,5 +13,5 @@ func runCommand(program string, args ...string) ([]byte, error) {
|
|||
if err != nil {
|
||||
err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(append([]string{program}, args...), " "), err)
|
||||
}
|
||||
return out, err
|
||||
return bytes.TrimSpace(out), err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue