2021-12-22 07:26:57 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-12-27 22:02:48 -07:00
|
|
|
|
|
|
|
"github.com/fhs/gompd/mpd"
|
2021-12-22 07:26:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileNode struct {
|
2021-12-25 11:48:23 -07:00
|
|
|
Children []FileNode
|
2021-12-22 07:26:57 -07:00
|
|
|
Path string
|
|
|
|
Parent *FileNode
|
|
|
|
AbsolutePath string
|
2021-12-27 22:02:48 -07:00
|
|
|
Title string
|
2021-12-22 07:26:57 -07:00
|
|
|
}
|
|
|
|
|
2021-12-25 11:48:23 -07:00
|
|
|
// Source Interface For Fuzzy Searching.
|
|
|
|
type FileNodes []FileNode
|
|
|
|
|
|
|
|
func (f FileNodes) String(i int) string {
|
|
|
|
if len(f[i].Children) == 0 {
|
2021-12-27 22:02:48 -07:00
|
|
|
return f[i].Title
|
2021-12-25 11:48:23 -07:00
|
|
|
}
|
|
|
|
return f[i].Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileNodes) Len() int {
|
|
|
|
return len(f)
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:02:48 -07:00
|
|
|
func (f *FileNode) AddChildren(path string, title string) {
|
2021-12-22 07:26:57 -07:00
|
|
|
if f.Path != "" {
|
2021-12-27 22:02:48 -07:00
|
|
|
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + "/" + path, Title: title})
|
2021-12-22 07:26:57 -07:00
|
|
|
} else {
|
2021-12-27 22:02:48 -07:00
|
|
|
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + path, Title: title})
|
2021-12-22 07:26:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileNode) AddChildNode(m FileNode) {
|
|
|
|
m.Parent = f
|
|
|
|
f.Children = append(f.Children, m)
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:02:48 -07:00
|
|
|
func GenerateDirectoryTree(path []mpd.Attrs) *FileNode {
|
2021-12-22 07:26:57 -07:00
|
|
|
var head *FileNode = new(FileNode)
|
|
|
|
var head1 *FileNode = head
|
|
|
|
for i := range path {
|
2021-12-27 22:02:48 -07:00
|
|
|
sepPaths := strings.Split(path[i]["file"], "/")
|
2021-12-22 07:26:57 -07:00
|
|
|
for j := range sepPaths {
|
|
|
|
if len(head.Children) == 0 {
|
2021-12-27 22:02:48 -07:00
|
|
|
head.AddChildren(sepPaths[j], path[i]["Title"])
|
2021-12-22 07:26:57 -07:00
|
|
|
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 {
|
2021-12-27 22:02:48 -07:00
|
|
|
head.AddChildren(sepPaths[j], path[i]["Title"])
|
2021-12-22 07:26:57 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|