gomp/client/files.go
aditya-K2 09297a4974 Adding A new Field to the FileNode -> Title
Previously During Searching the Connection with mpd client was utilised
to get the title for the files this was slowing the search with large
number of files. Now after adding the Title field to the struct the
Field is accessed instead of querying the server this has lead to faster
searches.

[[ Please Note the Title of a Folder would be the last accessed
track from the FileMap. ]]
2021-12-28 10:32:48 +05:30

87 lines
1.9 KiB
Go

package client
import (
"fmt"
"strings"
"github.com/fhs/gompd/mpd"
)
type FileNode struct {
Children []FileNode
Path string
Parent *FileNode
AbsolutePath string
Title 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) {
if f.Path != "" {
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + "/" + path, Title: title})
} else {
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + path, Title: title})
}
}
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"])
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"])
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)
}
}
}