gomp/main.go

269 lines
6.2 KiB
Go
Raw Normal View History

package main
import (
"github.com/aditya-K2/gomp/ui/notify"
"strconv"
"time"
"github.com/aditya-K2/gomp/render"
"github.com/aditya-K2/gomp/ui"
"github.com/aditya-K2/gomp/client"
"github.com/aditya-K2/gomp/utils"
2021-12-12 20:05:40 +00:00
2021-11-28 18:03:34 +00:00
"github.com/aditya-K2/fuzzy"
"github.com/aditya-K2/gomp/cache"
"github.com/aditya-K2/gomp/config"
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
)
2021-11-15 18:44:00 +00:00
var (
CONN *mpd.Client
UI *ui.Application
Notify *notify.NotificationServer
RENDERER *render.Renderer
Volume int64
Random bool
Repeat bool
ArtistTree map[string]map[string]map[string]string
2021-11-15 18:44:00 +00:00
)
func main() {
config.ReadConfig()
// Connect to MPD server
var mpdConnectionError error
CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if mpdConnectionError != nil {
panic(mpdConnectionError)
}
defer CONN.Close()
ui.GenerateFocusMap()
client.SetConnection(CONN)
ui.SetConnection(CONN)
render.SetConnection(CONN)
cache.SetCacheDir(viper.GetString("CACHE_DIR"))
RENDERER = render.NewRenderer()
ui.SetRenderer(RENDERER)
c, _ := CONN.CurrentSong()
2021-10-24 07:56:10 +00:00
if len(c) != 0 {
2021-12-22 12:24:14 +00:00
RENDERER.Start(c["file"])
2021-10-24 07:56:10 +00:00
} else {
2021-12-22 12:24:14 +00:00
RENDERER.Start("stop")
2021-10-24 07:56:10 +00:00
}
UI = ui.NewApplication()
notify.ConnectUI(UI)
fileMap, err := CONN.GetFiles()
dirTree := client.GenerateDirectoryTree(fileMap)
client.UpdatePlaylist(UI.ExpandedView)
_v, _ := CONN.Status()
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
Random, _ = strconv.ParseBool(_v["random"])
Repeat, _ = strconv.ParseBool(_v["repeat"])
2021-12-22 15:11:48 +00:00
ArtistTree, err = client.GenerateArtistTree()
ArtistTreeContent := utils.ConvertToArray(ArtistTree)
Notify = notify.NewNotificationServer()
2021-12-22 15:11:48 +00:00
Notify.Start()
client.SetNotificationServer(Notify)
render.SetNotificationServer(Notify)
2021-12-22 15:11:48 +00:00
var SearchContentSlice []interface{}
2021-11-13 11:27:54 +00:00
UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
if ui.HasFocus("Playlist") {
client.UpdatePlaylist(UI.ExpandedView)
} else if ui.HasFocus("SearchView") {
2021-12-22 15:11:48 +00:00
client.UpdateSearchView(UI.ExpandedView, SearchContentSlice)
} else if ui.HasFocus("FileBrowser") {
client.Update(dirTree.Children, UI.ExpandedView)
}
2021-11-13 11:27:54 +00:00
return UI.ExpandedView.GetInnerRect()
})
2021-12-22 15:11:48 +00:00
var FuncMap = map[string]func(){
"showChildrenContent": func() {
2021-11-13 11:27:54 +00:00
r, _ := UI.ExpandedView.GetSelection()
if ui.HasFocus("FileBrowser") {
if len(dirTree.Children[r].Children) == 0 {
id, _ := CONN.AddId(dirTree.Children[r].AbsolutePath, -1)
CONN.PlayId(id)
} else {
client.Update(dirTree.Children[r].Children, UI.ExpandedView)
dirTree = &dirTree.Children[r]
}
} else if ui.HasFocus("Playlist") {
CONN.Play(r)
} else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection()
2021-12-22 15:11:48 +00:00
client.AddToPlaylist(SearchContentSlice[r], true)
}
},
"togglePlayBack": func() {
client.TogglePlayBack()
},
"showParentContent": func() {
if ui.HasFocus("FileBrowser") {
if dirTree.Parent != nil {
client.Update(dirTree.Parent.Children, UI.ExpandedView)
dirTree = dirTree.Parent
}
}
},
"nextSong": func() {
CONN.Next()
},
"clearPlaylist": func() {
CONN.Clear()
Notify.Send("Playlist Cleared")
},
"previousSong": func() {
CONN.Previous()
},
"addToPlaylist": func() {
if ui.HasFocus("FileBrowser") {
2021-11-13 11:27:54 +00:00
r, _ := UI.ExpandedView.GetSelection()
CONN.Add(dirTree.Children[r].AbsolutePath)
} else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection()
2021-12-22 15:11:48 +00:00
client.AddToPlaylist(SearchContentSlice[r], false)
}
},
"toggleRandom": func() {
err := CONN.Random(!Random)
if err == nil {
Random = !Random
}
},
"toggleRepeat": func() {
err := CONN.Repeat(!Repeat)
if err == nil {
Repeat = !Repeat
}
},
"decreaseVolume": func() {
if Volume <= 0 {
Volume = 0
} else {
Volume -= 10
}
CONN.SetVolume(int(Volume))
},
"increaseVolume": func() {
if Volume >= 100 {
Volume = 100
} else {
Volume += 10
}
CONN.SetVolume(int(Volume))
},
"navigateToFiles": func() {
ui.SetFocus("FileBrowser")
UI.Navbar.Select(1, 0)
client.Update(dirTree.Children, UI.ExpandedView)
},
"navigateToPlaylist": func() {
ui.SetFocus("Playlist")
UI.Navbar.Select(0, 0)
client.UpdatePlaylist(UI.ExpandedView)
},
"navigateToMostPlayed": func() {
UI.Navbar.Select(2, 0)
},
"navigateToSearch": func() {
ui.SetFocus("SearchView")
UI.Navbar.Select(3, 0)
},
"quit": func() {
UI.App.Stop()
},
"stop": func() {
CONN.Stop()
2021-12-22 15:11:48 +00:00
Notify.Send("Playback Stopped")
},
"updateDB": func() {
_, err = CONN.Update("")
if err != nil {
panic(err)
}
2021-12-22 15:11:48 +00:00
Notify.Send("Database Updated")
},
"deleteSongFromPlaylist": func() {
if ui.HasFocus("Playlist") {
2021-11-13 11:27:54 +00:00
r, _ := UI.ExpandedView.GetSelection()
CONN.Delete(r, -1)
}
},
2021-11-15 11:02:59 +00:00
"FocusSearch": func() {
UI.App.SetFocus(UI.SearchBar)
},
}
2021-12-22 15:11:48 +00:00
config.GenerateKeyMap(FuncMap)
2021-11-15 11:02:59 +00:00
UI.SearchBar.SetAutocompleteFunc(func(c string) []string {
if c != "" && c != " " && c != " " {
2021-11-28 18:03:34 +00:00
_, _, w, _ := UI.SearchBar.GetRect()
2021-12-22 15:11:48 +00:00
matches := fuzzy.Find(c, ArtistTreeContent)
2021-11-15 11:02:59 +00:00
var suggestions []string
2021-11-28 18:03:34 +00:00
for i, match := range matches {
2021-11-15 11:02:59 +00:00
if i == 10 {
break
}
2021-12-12 20:05:40 +00:00
suggestions = append(suggestions, utils.GetFormattedString(match.Str, w-2))
2021-11-15 11:02:59 +00:00
}
return suggestions
} else {
return make([]string, 0)
}
})
2021-11-13 11:27:54 +00:00
UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
2021-12-22 15:11:48 +00:00
FuncMap[val]()
return nil
} else {
return e
}
})
UI.SearchBar.SetDoneFunc(func(e tcell.Key) {
if e == tcell.KeyEnter {
UI.ExpandedView.Select(0, 0)
ui.SetFocus("SearchView")
2021-12-22 15:11:48 +00:00
SearchContentSlice = nil
SearchContentSlice, err = client.GenerateContentSlice(UI.SearchBar.GetText())
if err != nil {
2021-12-22 15:11:48 +00:00
Notify.Send("Could Not Retrieve the Results")
} else {
UI.SearchBar.SetText("")
UI.App.SetFocus(UI.ExpandedView)
UI.Navbar.Select(3, 0)
}
}
if e == tcell.KeyEscape {
ui.FocusMap["SearchView"] = false
UI.App.SetFocus(UI.ExpandedView)
}
})
go func() {
for {
UI.App.Draw()
time.Sleep(time.Second)
}
}()
if err := UI.App.Run(); err != nil {
panic(err)
}
}