From 0118886d5e1bc0eb6ce4b59c873b7d6163f51719 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Fri, 24 Dec 2021 15:37:21 +0530 Subject: [PATCH] chore: Added More Informative Comments --- client/client.go | 31 ++++++++++++------------------ main.go | 25 +++++++++++++++++++++++- render/render.go | 49 ++++++++++++++++++------------------------------ ui/focus.go | 3 +++ 4 files changed, 57 insertions(+), 51 deletions(-) diff --git a/client/client.go b/client/client.go index b677c79..fece67c 100644 --- a/client/client.go +++ b/client/client.go @@ -3,6 +3,7 @@ package client import ( "errors" "fmt" + "github.com/fhs/gompd/mpd" "strings" @@ -54,11 +55,9 @@ func UpdatePlaylist(inputTable *tview.Table) { } } -/* - The GenerateContentSlice returns a slice of the content to be displayed on the Search View. The Slice is generated - because the random nature of maps as they return values randomly hence the draw function keeps changing the order - in which the results. -*/ +// The GenerateContentSlice returns a slice of the content to be displayed on the Search View. The Slice is generated +// because the random nature of maps as they return values randomly hence the draw function keeps changing the order +// in which the results appear. func GenerateContentSlice(selectedSuggestion string) ([]interface{}, error) { var ContentSlice []interface{} if strings.TrimRight(selectedSuggestion, " ") == "" { @@ -100,11 +99,9 @@ func GenerateContentSlice(selectedSuggestion string) ([]interface{}, error) { return ContentSlice, nil } -/* - UpdateSearchView as the name suggests Updates the Search View the idea is to basically keep a fourth option called - Search in the Navigation bar which will render things from a global ContentSlice at least in the context of the main - function this will also help in persisting the Search Results. -*/ +// UpdateSearchView as the name suggests Updates the Search View the idea is to basically keep a fourth option called +// Search in the Navigation bar which will render things from a global ContentSlice at least in the context of the main +// function this will also help in persisting the Search Results. func UpdateSearchView(inputTable *tview.Table, c []interface{}) { inputTable.Clear() _, _, width, _ := inputTable.GetInnerRect() @@ -166,6 +163,8 @@ func Update(f []FileNode, inputTable *tview.Table) { } } +// GenerateArtistTree Artist Tree is a map of Artist to their Album Map +// Album Tree is a map of the tracks in that particular album. func GenerateArtistTree() (map[string]map[string]map[string]string, error) { ArtistTree = make(map[string]map[string]map[string]string) AllInfo, err := CONN.ListAllInfo("/") @@ -199,9 +198,7 @@ func PrintArtistTree(a map[string]map[string]map[string]string) { } } -/* - Adds All tracks from a specified album to a playlist -*/ +// Adds All tracks from a specified album to a playlist func AddAlbum(a map[string]map[string]map[string]string, alb string, artist string) { for _, v := range a[artist][alb] { err := CONN.Add(v) @@ -212,9 +209,7 @@ func AddAlbum(a map[string]map[string]map[string]string, alb string, artist stri NotificationServer.Send("Album Added : " + alb) } -/* - Adds All tracks from a specified artist to a playlist -*/ +// Adds All tracks from a specified artist to a playlist func AddArtist(a map[string]map[string]map[string]string, artist string) { if val, ok := a[artist]; ok { for _, v := range val { @@ -229,9 +224,7 @@ func AddArtist(a map[string]map[string]map[string]string, artist string) { } } -/* - Adds Specified Track to the Playlist -*/ +// Adds Specified Track to the Playlist func AddTitle(a map[string]map[string]map[string]string, artist, alb, track string, addAndPlay bool) { if addAndPlay { id, err := CONN.AddId(a[artist][alb][track], -1) diff --git a/main.go b/main.go index aba3124..5ca3447 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ import ( func main() { config.ReadConfig() - // Connect to MPD server var mpdConnectionError error CONN, mpdConnectionError := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) if mpdConnectionError != nil { @@ -37,8 +36,11 @@ func main() { render.SetConnection(CONN) cache.SetCacheDir(viper.GetString("CACHE_DIR")) + Renderer := render.NewRenderer() + // Connecting the Renderer to the Main UI ui.ConnectRenderer(Renderer) + c, _ := CONN.CurrentSong() if len(c) != 0 { Renderer.Start(c["file"]) @@ -47,27 +49,41 @@ func main() { } UI := ui.NewApplication() + + // Connecting the Notification Server to the Main UI notify.ConnectUI(UI) fileMap, err := CONN.GetFiles() + + // Generating the Directory Tree for File Navigation. dirTree := client.GenerateDirectoryTree(fileMap) + // Default View upon Opening is of Playlist. client.UpdatePlaylist(UI.ExpandedView) _v, _ := CONN.Status() + // Setting Volume, Random and Repeat Values Volume, _ := strconv.ParseInt(_v["volume"], 10, 64) Random, _ := strconv.ParseBool(_v["random"]) Repeat, _ := strconv.ParseBool(_v["repeat"]) ArtistTree, err := client.GenerateArtistTree() + + // Used for Fuzzy Searching ArtistTreeContent := utils.ConvertToArray(ArtistTree) + Notify := notify.NewNotificationServer() Notify.Start() + + // Connecting Notification Server to Client and Rendering Module so that they can send Notifications client.SetNotificationServer(Notify) render.SetNotificationServer(Notify) + // This is the Slice that is used to Display Content in the SearchView var SearchContentSlice []interface{} + // This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses + // the Draw Function UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { if ui.HasFocus("Playlist") { client.UpdatePlaylist(UI.ExpandedView) @@ -79,6 +95,9 @@ func main() { return UI.ExpandedView.GetInnerRect() }) + // Function Maps is used For Mapping Keys According to the Value mapped to the Key the respective Function is called + // For e.g. in the config if the User Maps T to togglePlayBack then whenever in the input handler the T is received + // the respective function in this case togglePlayBack is called. var FuncMap = map[string]func(){ "showChildrenContent": func() { r, _ := UI.ExpandedView.GetSelection() @@ -197,6 +216,9 @@ func main() { }, } + // Generating the Key Map Based on the Function Map Here Basically the Values will be flipped + // In the config if togglePlayBack is mapped to [ T , P, SPACE ] then here Basically we will receive a map + // for each event T, P, SPACE mapped to the same function togglePlayBack config.GenerateKeyMap(FuncMap) UI.SearchBar.SetAutocompleteFunc(func(c string) []string { @@ -216,6 +238,7 @@ func main() { } }) + // Input Handler UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { if val, ok := config.KEY_MAP[int(e.Rune())]; ok { FuncMap[val]() diff --git a/render/render.go b/render/render.go index 744352a..0515437 100644 --- a/render/render.go +++ b/render/render.go @@ -1,11 +1,12 @@ package render import ( - "github.com/aditya-K2/gomp/ui" - "github.com/fhs/gompd/mpd" "image" "os" + "github.com/aditya-K2/gomp/ui" + "github.com/fhs/gompd/mpd" + "github.com/aditya-K2/gomp/cache" "github.com/aditya-K2/gomp/utils" "github.com/nfnt/resize" @@ -14,8 +15,8 @@ import ( ) var ( - CONN *mpd.Client - Notify interface { Send(string) } + CONN *mpd.Client + Notify interface{ Send(string) } ) func SetConnection(c *mpd.Client) { @@ -26,18 +27,14 @@ func SetNotificationServer(n interface{ Send(string) }) { Notify = n } -/* - Renderer is just a channel on which we will send the Path to the song whose - Image is to be Rendered. This channel is passed to the OpenImage which in turn is called - by the Start() function as a go routine. -*/ +// Renderer is just a channel on which we will send the Path to the song whose +// Image is to be Rendered. This channel is passed to the OpenImage which in turn is called +// by the Start() function as a go routine. type Renderer struct { c chan string } -/* - Returns a new Renderer with a string channel -*/ +// Returns a new Renderer with a string channel func NewRenderer() *Renderer { c := make(chan string) return &Renderer{ @@ -45,21 +42,15 @@ func NewRenderer() *Renderer { } } -/* - Send Image Path to Renderer -*/ +// Send Image Path to Renderer func (self *Renderer) Send(path string) { self.c <- path } -/* - - Go Routine that will Be Called and will listen on the channel c - for changes and on getting a string over the channel will open the Image and - keep listening again. This will keep the image blocked ( i.e no need to use time.Sleep() etc. ) - and saves resources too. - -*/ +// Go Routine that will Be Called and will listen on the channel c +// for changes and on getting a string over the channel will open the Image and +// keep listening again. This will keep the image blocked ( i.e no need to use time.Sleep() etc. ) +// and saves resources too. func OpenImage(path string, c chan string) { fw, fh := utils.GetFontWidth() var im *ueberzug.Image @@ -79,18 +70,14 @@ func OpenImage(path string, c chan string) { } } -/* - Initialises the Renderer and calls the go routine OpenImage and passes the channel - as argument. -*/ +// Initialises the Renderer and calls the go routine OpenImage and passes the channel +// as argument. func (self *Renderer) Start(path string) { go OpenImage(path, self.c) } -/* - This Function returns the path to the image that is to be rendered it checks first for the image in the cache - else it adds the image to the cache and then extracts it and renders it. -*/ +// This Function returns the path to the image that is to be rendered it checks first for the image in the cache +// else it adds the image to the cache and then extracts it and renders it. func GetImagePath(path string) string { a, err := CONN.ListInfo(path) var extractedImage string diff --git a/ui/focus.go b/ui/focus.go index 1906a63..b71c377 100644 --- a/ui/focus.go +++ b/ui/focus.go @@ -1,5 +1,8 @@ package ui +// The Focus Map Helps to keep track of which UI Element Currently Has the Focus It can be queried to get the Current +// UI Element with Focus and also can set UI Focus keep in mind that it isn't Focus Map that is Responsible to change +// the Focus that is Done through the Update Function of UI.ExpandedView */ var FocusMap map[string]bool func GenerateFocusMap() {