-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.go
104 lines (94 loc) · 2.18 KB
/
files.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package client
import (
"fmt"
"strings"
"github.com/fhs/gompd/v2/mpd"
)
type FileNode struct {
Children []FileNode
Path string
Parent *FileNode
AbsolutePath string
Title string
Artist string
Album string
}
// Source Interface For Fuzzy Searching.
type FileNodes []FileNode
func (f FileNodes) String(i int) string {
if len(f[i].Children) == 0 {
return f[i].Title
}
return f[i].Path
}
func (f FileNodes) Len() int {
return len(f)
}
func (f *FileNode) AddChildren(
path string, title string, artist string, album string) {
if f.Path != "" {
f.Children = append(f.Children,
FileNode{
Children: make([]FileNode, 0),
Path: path,
Parent: f,
AbsolutePath: f.AbsolutePath + "/" + path,
Title: title,
Artist: artist,
Album: album})
} else {
f.Children = append(f.Children,
FileNode{
Children: make([]FileNode, 0),
Path: path,
Parent: f,
AbsolutePath: f.AbsolutePath + path})
}
}
func (f *FileNode) AddChildNode(m FileNode) {
m.Parent = f
f.Children = append(f.Children, m)
}
func GenerateDirectoryTree(path []mpd.Attrs) *FileNode {
var head *FileNode = new(FileNode)
var head1 *FileNode = head
for i := range path {
sepPaths := strings.Split(path[i]["file"], "/")
for j := range sepPaths {
if len(head.Children) == 0 {
head.AddChildren(sepPaths[j], path[i]["Title"],
path[i]["Artist"], path[i]["Album"])
head = &(head.Children[len(head.Children)-1])
} else {
var headIsChanged = false
for k := range head.Children {
if head.Children[k].Path == sepPaths[j] {
head = &(head.Children[k])
headIsChanged = true
break
}
}
if !headIsChanged {
head.AddChildren(sepPaths[j], path[i]["Title"],
path[i]["Artist"], path[i]["Album"])
head = &(head.Children[len(head.Children)-1])
}
}
}
head = head1
}
return head
}
func (f FileNode) Print(count int) {
if len(f.Children) == 0 {
return
} else {
for i := range f.Children {
for j := 0; j < count; j++ {
fmt.Print("---")
}
fmt.Println(f.Children[i].AbsolutePath)
f.Children[i].Print(count + 1)
}
}
}