✨ Rate current track
This commit is contained in:
parent
f0b04b7c05
commit
83dc41cb8e
4 changed files with 83 additions and 9 deletions
58
cmd/current-rate.go
Normal file
58
cmd/current-rate.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"codeberg.org/danjones000/mpc-extra/mpd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// rateCmd represents the rate command
|
||||
var rateCmd = &cobra.Command{
|
||||
Use: "rate [rating]",
|
||||
Short: "Rates the current playing song: 0-10",
|
||||
/*
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
*/
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return errors.New("Missing rating")
|
||||
}
|
||||
num, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if num < 0 || num > 10 {
|
||||
return errors.New("Rating must be between zero and ten")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
curr, _ := mpd.GetCurrent()
|
||||
curr.AddSticker("rating", args[0])
|
||||
curr.PrintAll(true)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
currentCmd.AddCommand(rateCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// rateCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// rateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
|
@ -11,13 +11,15 @@ import (
|
|||
// currentCmd represents the current command
|
||||
var currentCmd = &cobra.Command{
|
||||
Use: "current",
|
||||
Short: "A brief description of your command",
|
||||
Short: "Shows information about the currently playing song",
|
||||
/*
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
*/
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
curr, _ := mpd.GetCurrent()
|
||||
curr.PrintAll(true)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ import (
|
|||
|
||||
// stickerCmd represents the sticker command
|
||||
var stickerCmd = &cobra.Command{
|
||||
Use: "sticker",
|
||||
Use: "sticker [sticker-name]",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
|
|
|||
17
mpd/song.go
17
mpd/song.go
|
|
@ -10,6 +10,7 @@ type Song struct {
|
|||
Path string
|
||||
Attrs mpd.Attrs
|
||||
stickers []mpd.Sticker
|
||||
// @todo refactor as a map so there's no dupes
|
||||
}
|
||||
|
||||
func newSong(attrs mpd.Attrs) *Song {
|
||||
|
|
@ -52,8 +53,8 @@ func (s *Song) GetSticker(name string) string {
|
|||
return sticker.Name == name
|
||||
})
|
||||
if ret != nil && len(ret) > 0 {
|
||||
first := ret[0]
|
||||
return first.Value
|
||||
last := ret[len(ret)-1]
|
||||
return last.Value
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,3 +71,15 @@ func (s *Song) GetSticker(name string) string {
|
|||
|
||||
return found.Value
|
||||
}
|
||||
|
||||
func (s *Song) AddSticker(name, value string) error {
|
||||
ret := conn.StickerSet(s.Path, name, value)
|
||||
if ret == nil {
|
||||
if s.stickers == nil || len(s.stickers) == 0 {
|
||||
s.GetStickers()
|
||||
} else {
|
||||
s.stickers = append(s.stickers, mpd.Sticker{Name: name, Value: value})
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue