diff --git a/client.go b/client/client.go similarity index 83% rename from client.go rename to client/client.go index da8c3fe..b677c79 100644 --- a/client.go +++ b/client/client.go @@ -1,8 +1,10 @@ -package main +package client import ( "errors" "fmt" + "github.com/fhs/gompd/mpd" + "strings" "github.com/aditya-K2/gomp/utils" @@ -10,10 +12,23 @@ import ( ) var ( + CONN *mpd.Client + ArtistTree map[string]map[string]map[string]string + NotificationServer interface { + Send(string) + } WHITE_AND_BOLD string = "[#ffffff::b]" ) -func togglePlayBack() error { +func SetConnection(c *mpd.Client) { + CONN = c +} + +func SetNotificationServer(n interface{ Send(string) }) { + NotificationServer = n +} + +func TogglePlayBack() error { status, err := CONN.Status() if status["state"] == "play" && err == nil { CONN.Pause(true) @@ -47,24 +62,24 @@ func UpdatePlaylist(inputTable *tview.Table) { func GenerateContentSlice(selectedSuggestion string) ([]interface{}, error) { var ContentSlice []interface{} if strings.TrimRight(selectedSuggestion, " ") == "" { - NOTIFICATION_SERVER.Send("Empty Search!") + NotificationServer.Send("Empty Search!") return nil, errors.New("empty Search String Provided") } - if _, ok := ARTIST_TREE[selectedSuggestion]; ok { + if _, ok := ArtistTree[selectedSuggestion]; ok { ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artists :") ContentSlice = append(ContentSlice, selectedSuggestion) ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artist Albums :") - for albumName := range ARTIST_TREE[selectedSuggestion] { + for albumName := range ArtistTree[selectedSuggestion] { ContentSlice = append(ContentSlice, [2]string{albumName, selectedSuggestion}) } ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artist Tracks :") - for albumName, trackList := range ARTIST_TREE[selectedSuggestion] { + for albumName, trackList := range ArtistTree[selectedSuggestion] { for track := range trackList { ContentSlice = append(ContentSlice, [3]string{track, selectedSuggestion, albumName}) } } } - if aMap := QueryArtistTreeForAlbums(ARTIST_TREE, selectedSuggestion); len(aMap) != 0 { + if aMap := QueryArtistTreeForAlbums(ArtistTree, selectedSuggestion); len(aMap) != 0 { ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Albums :") for mSlice := range aMap { ContentSlice = append(ContentSlice, mSlice) @@ -76,7 +91,7 @@ func GenerateContentSlice(selectedSuggestion string) ([]interface{}, error) { } } } - if tMap := QueryArtistTreeForTracks(ARTIST_TREE, selectedSuggestion); len(tMap) != 0 { + if tMap := QueryArtistTreeForTracks(ArtistTree, selectedSuggestion); len(tMap) != 0 { ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Tracks :") for mSlice := range tMap { ContentSlice = append(ContentSlice, mSlice) @@ -122,8 +137,8 @@ func UpdateSearchView(inputTable *tview.Table, c []interface{}) { func Update(f []FileNode, inputTable *tview.Table) { inputTable.Clear() for i, j := range f { - if len(j.children) == 0 { - _songAttributes, err := CONN.ListAllInfo(j.absolutePath) + if len(j.Children) == 0 { + _songAttributes, err := CONN.ListAllInfo(j.AbsolutePath) if err == nil && _songAttributes[0]["Title"] != "" { _, _, w, _ := inputTable.GetInnerRect() inputTable.SetCell(i, 0, @@ -140,19 +155,19 @@ func Update(f []FileNode, inputTable *tview.Table) { } else if _songAttributes[0]["Title"] == "" { inputTable.SetCell(i, 0, - tview.NewTableCell("[blue]"+j.path). + tview.NewTableCell("[blue]"+j.Path). SetAlign(tview.AlignLeft)) } } else { inputTable.SetCell(i, 0, - tview.NewTableCell("[yellow::b]"+j.path). + tview.NewTableCell("[yellow::b]"+j.Path). SetAlign(tview.AlignLeft)) } } } func GenerateArtistTree() (map[string]map[string]map[string]string, error) { - ArtistTree := make(map[string]map[string]map[string]string) + ArtistTree = make(map[string]map[string]map[string]string) AllInfo, err := CONN.ListAllInfo("/") if err == nil { for _, i := range AllInfo { @@ -191,10 +206,10 @@ func AddAlbum(a map[string]map[string]map[string]string, alb string, artist stri for _, v := range a[artist][alb] { err := CONN.Add(v) if err != nil { - NOTIFICATION_SERVER.Send("Could Not Add Song : " + v) + NotificationServer.Send("Could Not Add Song : " + v) } } - NOTIFICATION_SERVER.Send("Album Added : " + alb) + NotificationServer.Send("Album Added : " + alb) } /* @@ -206,11 +221,11 @@ func AddArtist(a map[string]map[string]map[string]string, artist string) { for _, path := range v { err := CONN.Add(path) if err != nil { - NOTIFICATION_SERVER.Send("Could Not Add Song : " + path) + NotificationServer.Send("Could Not Add Song : " + path) } } } - NOTIFICATION_SERVER.Send("Artist Added : " + artist) + NotificationServer.Send("Artist Added : " + artist) } } @@ -222,18 +237,18 @@ func AddTitle(a map[string]map[string]map[string]string, artist, alb, track stri id, err := CONN.AddId(a[artist][alb][track], -1) CONN.PlayId(id) if err != nil { - NOTIFICATION_SERVER.Send("Could Not Add Track : " + track) + NotificationServer.Send("Could Not Add Track : " + track) } } else { err := CONN.Add(a[artist][alb][track]) if err != nil { - NOTIFICATION_SERVER.Send("Could Not Add Track : " + track) + NotificationServer.Send("Could Not Add Track : " + track) } } - NOTIFICATION_SERVER.Send("Track Added : " + track) + NotificationServer.Send("Track Added : " + track) } -/* Querys the Artist Tree for a track and returns a TrackMap (i.e [3]string{artist, album, track} -> path) which will help us +/* Querys the Artist Tree for a track and returns a TrackMap (i.e [3]string{artist, album, track} -> Path) which will help us to add tracks to the playlist */ func QueryArtistTreeForTracks(a map[string]map[string]map[string]string, track string) map[[3]string]string { TrackMap := make(map[[3]string]string) @@ -249,7 +264,7 @@ func QueryArtistTreeForTracks(a map[string]map[string]map[string]string, track s return TrackMap } -/* Querys the Artist Tree for an album and returns a AlbumMap (i.e [3]string{artist, album } ->[]path of songs in the album) +/* Querys the Artist Tree for an album and returns a AlbumMap (i.e [3]string{artist, album } ->[]Path of songs in the album) which will help us to add all album tracks to the playlist */ func QueryArtistTreeForAlbums(a map[string]map[string]map[string]string, album string) map[[2]string][][2]string { AlbumMap := make(map[[2]string][][2]string) @@ -272,17 +287,17 @@ func AddToPlaylist(a interface{}, addAndPlay bool) { case [3]string: { b := a.([3]string) - AddTitle(ARTIST_TREE, b[1], b[2], b[0], addAndPlay) + AddTitle(ArtistTree, b[1], b[2], b[0], addAndPlay) } case [2]string: { b := a.([2]string) - AddAlbum(ARTIST_TREE, b[0], b[1]) + AddAlbum(ArtistTree, b[0], b[1]) } case string: { b := a.(string) - AddArtist(ARTIST_TREE, b) + AddArtist(ArtistTree, b) } } } diff --git a/client/fileBrowser.go b/client/fileBrowser.go new file mode 100644 index 0000000..71cac40 --- /dev/null +++ b/client/fileBrowser.go @@ -0,0 +1,69 @@ +package client + +import ( + "fmt" + "strings" +) + +type FileNode struct { + Children []FileNode + Path string + Parent *FileNode + AbsolutePath string +} + +func (f *FileNode) AddChildren(path string) { + if f.Path != "" { + f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + "/" + path}) + } 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 []string) *FileNode { + var head *FileNode = new(FileNode) + var head1 *FileNode = head + for i := range path { + sepPaths := strings.Split(path[i], "/") + for j := range sepPaths { + if len(head.Children) == 0 { + head.AddChildren(sepPaths[j]) + 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]) + 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) + } + } +} diff --git a/fileBrowser.go b/fileBrowser.go deleted file mode 100644 index fa1eebc..0000000 --- a/fileBrowser.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import ( - "fmt" - "strings" -) - -type FileNode struct { - children []FileNode - path string - parent *FileNode - absolutePath string -} - -func (f *FileNode) addChildren(path string) { - if f.path != "" { - f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f, absolutePath: f.absolutePath + "/" + path}) - } 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 []string) *FileNode { - var head *FileNode = new(FileNode) - var head1 *FileNode = head - for i := range path { - sepPaths := strings.Split(path[i], "/") - for j := range sepPaths { - if len(head.children) == 0 { - head.addChildren(sepPaths[j]) - 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]) - 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) - } - } -} diff --git a/main.go b/main.go index 69eb553..7c1b602 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "strconv" "time" + "github.com/aditya-K2/gomp/client" "github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/fuzzy" @@ -48,29 +49,31 @@ func main() { UI = newApplication() fileMap, err := CONN.GetFiles() - dirTree := generateDirectoryTree(fileMap) + dirTree := client.GenerateDirectoryTree(fileMap) - UpdatePlaylist(UI.ExpandedView) + client.SetConnection(CONN) + client.UpdatePlaylist(UI.ExpandedView) _v, _ := CONN.Status() Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) Random, _ = strconv.ParseBool(_v["random"]) Repeat, _ = strconv.ParseBool(_v["repeat"]) - ARTIST_TREE, err = GenerateArtistTree() + ARTIST_TREE, err = client.GenerateArtistTree() ARTIST_TREE_CONTENT := utils.ConvertToArray(ARTIST_TREE) NOTIFICATION_SERVER = NewNotificationServer() NOTIFICATION_SERVER.Start() + client.SetNotificationServer(NOTIFICATION_SERVER) var SEARCH_CONTENT_SLICE []interface{} UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { if InsidePlaylist { - UpdatePlaylist(UI.ExpandedView) + client.UpdatePlaylist(UI.ExpandedView) } else if InsideSearchView { - UpdateSearchView(UI.ExpandedView, SEARCH_CONTENT_SLICE) + client.UpdateSearchView(UI.ExpandedView, SEARCH_CONTENT_SLICE) } else { - Update(dirTree.children, UI.ExpandedView) + client.Update(dirTree.Children, UI.ExpandedView) } return UI.ExpandedView.GetInnerRect() }) @@ -79,28 +82,28 @@ func main() { "showChildrenContent": func() { r, _ := UI.ExpandedView.GetSelection() if !InsidePlaylist && !InsideSearchView { - if len(dirTree.children[r].children) == 0 { - id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1) + if len(dirTree.Children[r].Children) == 0 { + id, _ := CONN.AddId(dirTree.Children[r].AbsolutePath, -1) CONN.PlayId(id) } else { - Update(dirTree.children[r].children, UI.ExpandedView) - dirTree = &dirTree.children[r] + client.Update(dirTree.Children[r].Children, UI.ExpandedView) + dirTree = &dirTree.Children[r] } } else if InsidePlaylist { CONN.Play(r) } else if InsideSearchView { r, _ := UI.ExpandedView.GetSelection() - AddToPlaylist(SEARCH_CONTENT_SLICE[r], true) + client.AddToPlaylist(SEARCH_CONTENT_SLICE[r], true) } }, "togglePlayBack": func() { - togglePlayBack() + client.TogglePlayBack() }, "showParentContent": func() { if !InsidePlaylist && !InsideSearchView { - if dirTree.parent != nil { - Update(dirTree.parent.children, UI.ExpandedView) - dirTree = dirTree.parent + if dirTree.Parent != nil { + client.Update(dirTree.Parent.Children, UI.ExpandedView) + dirTree = dirTree.Parent } } }, @@ -117,10 +120,10 @@ func main() { "addToPlaylist": func() { if !InsidePlaylist && !InsideSearchView { r, _ := UI.ExpandedView.GetSelection() - CONN.Add(dirTree.children[r].absolutePath) + CONN.Add(dirTree.Children[r].AbsolutePath) } else if InsideSearchView { r, _ := UI.ExpandedView.GetSelection() - AddToPlaylist(SEARCH_CONTENT_SLICE[r], false) + client.AddToPlaylist(SEARCH_CONTENT_SLICE[r], false) } }, "toggleRandom": func() { @@ -155,13 +158,13 @@ func main() { InsidePlaylist = false InsideSearchView = false UI.Navbar.Select(1, 0) - Update(dirTree.children, UI.ExpandedView) + client.Update(dirTree.Children, UI.ExpandedView) }, "navigateToPlaylist": func() { InsidePlaylist = true InsideSearchView = false UI.Navbar.Select(0, 0) - UpdatePlaylist(UI.ExpandedView) + client.UpdatePlaylist(UI.ExpandedView) }, "navigateToMostPlayed": func() { InsideSearchView = false @@ -232,7 +235,7 @@ func main() { InsideSearchView = true InsidePlaylist = false SEARCH_CONTENT_SLICE = nil - SEARCH_CONTENT_SLICE, err = GenerateContentSlice(UI.SearchBar.GetText()) + SEARCH_CONTENT_SLICE, err = client.GenerateContentSlice(UI.SearchBar.GetText()) if err != nil { NOTIFICATION_SERVER.Send("Could Not Retrieve the Results") } else {