From 09297a49748d4570b3f32004ba6e054a91799463 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Tue, 28 Dec 2021 10:32:48 +0530 Subject: [PATCH] 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. ]] --- client/files.go | 24 +++++++++++------------- client/updateView.go | 8 +------- main.go | 2 +- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/client/files.go b/client/files.go index aeadd35..7e2f770 100644 --- a/client/files.go +++ b/client/files.go @@ -3,6 +3,8 @@ package client import ( "fmt" "strings" + + "github.com/fhs/gompd/mpd" ) type FileNode struct { @@ -10,6 +12,7 @@ type FileNode struct { Path string Parent *FileNode AbsolutePath string + Title string } // Source Interface For Fuzzy Searching. @@ -17,12 +20,7 @@ type FileNodes []FileNode func (f FileNodes) String(i int) string { if len(f[i].Children) == 0 { - _s, err := CONN.ListAllInfo(f[i].AbsolutePath) - if err != nil { - NotificationServer.Send(fmt.Sprintf("Could Not Get Information About the Node %s", f[i].Path)) - return f[i].Path - } - return _s[0]["Title"] + return f[i].Title } return f[i].Path } @@ -31,11 +29,11 @@ func (f FileNodes) Len() int { return len(f) } -func (f *FileNode) AddChildren(path string) { +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}) + 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}) + f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + path, Title: title}) } } @@ -44,14 +42,14 @@ func (f *FileNode) AddChildNode(m FileNode) { f.Children = append(f.Children, m) } -func GenerateDirectoryTree(path []string) *FileNode { +func GenerateDirectoryTree(path []mpd.Attrs) *FileNode { var head *FileNode = new(FileNode) var head1 *FileNode = head for i := range path { - sepPaths := strings.Split(path[i], "/") + sepPaths := strings.Split(path[i]["file"], "/") for j := range sepPaths { if len(head.Children) == 0 { - head.AddChildren(sepPaths[j]) + head.AddChildren(sepPaths[j], path[i]["Title"]) head = &(head.Children[len(head.Children)-1]) } else { var headIsChanged = false @@ -63,7 +61,7 @@ func GenerateDirectoryTree(path []string) *FileNode { } } if !headIsChanged { - head.AddChildren(sepPaths[j]) + head.AddChildren(sepPaths[j], path[i]["Title"]) head = &(head.Children[len(head.Children)-1]) } } diff --git a/client/updateView.go b/client/updateView.go index 8a5000b..d3c59b7 100644 --- a/client/updateView.go +++ b/client/updateView.go @@ -1,7 +1,6 @@ package client import ( - "fmt" "strings" "github.com/aditya-K2/fuzzy" @@ -18,12 +17,7 @@ func UpdateBuffSearchView(inputTable *tview.Table, m fuzzy.Matches, f []FileNode if len(f[v.Index].Children) != 0 { inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Path, "#0000ff", "yellow", v.MatchedIndexes)) } else { - _s, err := CONN.ListAllInfo(f[v.Index].AbsolutePath) - if err != nil { - NotificationServer.Send(fmt.Sprintf("Could Not Add %s to the Table", f[v.Index].Path)) - } else { - inputTable.SetCellSimple(k, 0, utils.GetMatchedString(_s[0]["Title"], "#fbff00", "green", v.MatchedIndexes)) - } + inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Title, "#fbff00", "green", v.MatchedIndexes)) } if k == 15 { break diff --git a/main.go b/main.go index 62ae21f..b25aeee 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { // Connecting the Notification Server to the Main UI notify.ConnectUI(UI) - fileMap, err := CONN.GetFiles() + fileMap, err := CONN.ListAllInfo("/") // Generating the Directory Tree for File Navigation. dirTree := client.GenerateDirectoryTree(fileMap)