-
Notifications
You must be signed in to change notification settings - Fork 23
/
print.go
72 lines (61 loc) · 1.48 KB
/
print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package commands
import (
"fmt"
"strings"
"github.com/ambientsound/pms/api"
"github.com/ambientsound/pms/input/lexer"
)
// Print displays information about the selected song's tags.
type Print struct {
command
api api.API
tags []string
}
func NewPrint(api api.API) Command {
return &Print{
api: api,
tags: make([]string, 0),
}
}
func (cmd *Print) Execute(class int, s string) error {
var err error
switch class {
case lexer.TokenIdentifier:
if len(cmd.tags) > 0 {
return fmt.Errorf("Unexpected '%s', expected END", s)
}
cmd.tags = strings.Split(strings.ToLower(s), ",")
case lexer.TokenEnd:
if len(cmd.tags) == 0 {
return fmt.Errorf("Unexpected END, expected list of tags to print")
}
list := cmd.api.Songlist()
selection := list.Selection()
switch selection.Len() {
case 0:
return fmt.Errorf("Cannot print song tags; no song selected")
case 1:
song := selection.Song(0)
parts := make([]string, 0)
for _, tag := range cmd.tags {
msg := ""
value, ok := song.StringTags[tag]
if ok {
value = strings.ReplaceAll(value, "%", "%%")
msg = fmt.Sprintf("%s: '%s'", tag, value)
} else {
msg = fmt.Sprintf("%s: <MISSING>", tag)
}
parts = append(parts, msg)
}
msg := strings.Join(parts, ", ")
cmd.api.Message(msg)
default:
return fmt.Errorf("Multiple songs selected; cannot print song tags")
}
list.ClearSelection()
default:
return fmt.Errorf("Unknown input '%s', expected END", s)
}
return err
}