-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.go
More file actions
61 lines (52 loc) · 1.32 KB
/
tag.go
File metadata and controls
61 lines (52 loc) · 1.32 KB
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
package main
// The tag tree is used to look up music clips
// using fuzzy matching (so that the name does not
// have to be typed 100% correct on the command line)
// A tag may represent, e.g., an artist, album, clip...
// Some tags, like albums, have child Tags while
// others, like clips, are leaf nodes.
// The leaf nodes point to a file to be played.
import (
"strings"
)
type Tag struct {
fuzzy string // fuzzyfied tag
children []*Tag // children, if any
file string // music file, in case of leaf node
}
func NewTag(tag string) *Tag {
return &Tag{Fuzzy(tag), []*Tag{}, ""}
}
// Get a child by fuzzy tag matching.
// If the child does not exist yet, it is added.
func (this *Tag) Child(tag string) (child *Tag, ok bool) {
fuzzyTag := Fuzzy(tag)
for _, c := range this.children {
if c.fuzzy == fuzzyTag {
child = c
ok = true
return
}
}
child = NewTag(tag)
this.children = append(this.children, child)
return
}
func (this *Tag) String() string {
return this.fuzzy
}
func (this *Tag) Print(indent int) string {
str := spaces(indent) + this.fuzzy + ":" + this.file + "\n"
for _, c := range this.children {
str += c.Print(indent + 1)
}
return str
}
func spaces(howmany int) string {
return " "[:howmany]
}
// Fuzzyfy string
func Fuzzy(str string) string {
str = strings.ToLower(str)
return str
}