gomp/main.go

351 lines
9.5 KiB
Go
Raw Normal View History

package main
import (
"strconv"
"time"
"github.com/aditya-K2/gomp/cache"
"github.com/aditya-K2/gomp/client"
"github.com/aditya-K2/gomp/config"
2022-08-28 23:34:12 -06:00
"github.com/aditya-K2/gomp/notify"
"github.com/aditya-K2/gomp/render"
"github.com/aditya-K2/gomp/ui"
"github.com/aditya-K2/gomp/utils"
2022-08-31 12:12:15 -06:00
"github.com/aditya-K2/gomp/views"
2021-12-12 13:05:40 -07:00
2021-11-28 11:03:34 -07:00
"github.com/aditya-K2/fuzzy"
2022-09-08 13:16:38 -06:00
"github.com/fhs/gompd/v2/mpd"
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
)
func main() {
config.ReadConfig()
var mpdConnectionError error
2022-09-14 09:12:28 -06:00
client.Conn, mpdConnectionError = mpd.Dial(utils.GetNetwork())
if mpdConnectionError != nil {
2021-12-31 03:18:58 -07:00
utils.Print("RED", "Could Not Connect to MPD Server\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(mpdConnectionError)
}
2022-09-08 14:03:13 -06:00
Conn := client.Conn
defer Conn.Close()
2022-09-08 14:03:13 -06:00
ui.SetConnection(Conn)
cache.SetCacheDir(viper.GetString("CACHE_DIR"))
2021-12-24 03:07:21 -07:00
render.Rendr = render.NewRenderer()
2021-12-24 03:07:21 -07:00
// Connecting the Renderer to the Main UI
ui.ConnectRenderer(render.Rendr)
2021-12-24 03:07:21 -07:00
ui.Ui = ui.NewApplication()
2021-12-24 03:07:21 -07:00
2022-09-08 14:03:13 -06:00
fileMap, err := Conn.ListAllInfo("/")
2021-12-31 03:18:58 -07:00
if err != nil {
utils.Print("RED", "Could Not Generate the File Map\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(err)
}
2021-12-24 03:07:21 -07:00
// Generating the Directory Tree for File Navigation.
client.DirTree = client.GenerateDirectoryTree(fileMap)
2021-12-31 03:18:58 -07:00
var Volume int64
var Random, Repeat bool
2022-09-08 14:01:51 -06:00
var SeekOffset = viper.GetInt("SEEK_OFFSET")
var SeekFunc = func(back bool) {
if status, err := Conn.Status(); err != nil {
notify.Notify.Send("Could not get MPD Status")
} else {
if status["state"] == "play" {
var stime time.Duration
if back {
stime = -1 * time.Second * time.Duration(SeekOffset)
} else {
stime = time.Second * time.Duration(SeekOffset)
}
if err := Conn.SeekCur(stime, true); err != nil {
notify.Notify.Send("Could Not Seek Forward in the Song")
}
}
}
}
2021-12-31 03:18:58 -07:00
2022-09-08 14:03:13 -06:00
if _v, err := Conn.Status(); err != nil {
2021-12-31 03:18:58 -07:00
utils.Print("RED", "Could Not Get the MPD Status\n")
panic(err)
} else {
// 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()
2021-12-31 03:18:58 -07:00
if err != nil {
utils.Print("RED", "Could Not Generate the ArtistTree\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(err)
}
2021-12-24 03:07:21 -07:00
// Used for Fuzzy Searching
2021-12-22 08:11:48 -07:00
ArtistTreeContent := utils.ConvertToArray(ArtistTree)
2021-12-24 03:07:21 -07:00
notify.Notify = notify.NewNotificationServer()
notify.Notify.Start()
2021-12-24 03:07:21 -07:00
2022-09-08 14:03:13 -06:00
if c, err := Conn.CurrentSong(); err != nil {
2022-08-03 16:02:58 -06:00
utils.Print("RED", "Could Not Retrieve the Current Song\n")
panic(err)
} else {
if len(c) != 0 {
render.Rendr.Start(c["file"])
2022-08-03 16:02:58 -06:00
} else {
render.Rendr.Start("stop")
2022-08-03 16:02:58 -06:00
}
}
2021-12-24 03:07:21 -07:00
// This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses
// the Draw Function
views.PView.StartWatcher()
views.SetCurrentView(&views.PView)
ui.Ui.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
views.GetCurrentView().Update(ui.Ui.ExpandedView)
return ui.Ui.ExpandedView.GetInnerRect()
})
2021-12-24 03:07:21 -07:00
// 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.
2021-12-22 08:11:48 -07:00
var FuncMap = map[string]func(){
"showChildrenContent": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().ShowChildrenContent()
},
"togglePlayBack": func() {
2021-12-31 03:18:58 -07:00
if err := client.TogglePlayBack(); err != nil {
notify.Notify.Send("Could not Toggle Play Back")
2021-12-31 03:18:58 -07:00
}
},
"showParentContent": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().ShowParentContent()
},
"nextSong": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Next(); err != nil {
notify.Notify.Send("Could not Select the Next Song")
2021-12-31 03:18:58 -07:00
}
},
"clearPlaylist": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Clear(); err != nil {
notify.Notify.Send("Could not Clear the Playlist")
2021-12-31 03:18:58 -07:00
} else {
if views.PView.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil {
utils.Print("RED", "Couldn't get the current Playlist.\n")
panic(err)
}
notify.Notify.Send("Playlist Cleared!")
2021-12-31 03:18:58 -07:00
}
},
"previousSong": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Previous(); err != nil {
notify.Notify.Send("Could Not Select the Previous Song")
2021-12-31 03:18:58 -07:00
}
},
"addToPlaylist": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().AddToPlaylist()
},
"toggleRandom": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Random(!Random); err == nil {
Random = !Random
}
},
"toggleRepeat": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Repeat(!Repeat); err == nil {
Repeat = !Repeat
}
},
"decreaseVolume": func() {
if Volume <= 0 {
Volume = 0
} else {
Volume -= 10
}
2022-09-08 14:03:13 -06:00
if err := Conn.SetVolume(int(Volume)); err != nil {
notify.Notify.Send("Could Not Decrease the Volume")
2021-12-31 03:18:58 -07:00
}
},
"increaseVolume": func() {
if Volume >= 100 {
Volume = 100
} else {
Volume += 10
}
2022-09-08 14:03:13 -06:00
if err := Conn.SetVolume(int(Volume)); err != nil {
notify.Notify.Send("Could Not Increase the Volume")
2021-12-31 03:18:58 -07:00
}
},
"navigateToFiles": func() {
2022-08-31 12:12:15 -06:00
views.SetCurrentView(views.FView)
ui.Ui.Navbar.Select(1, 0)
views.FView.Update(ui.Ui.ExpandedView)
},
"navigateToPlaylist": func() {
views.SetCurrentView(&views.PView)
ui.Ui.Navbar.Select(0, 0)
views.PView.Update(ui.Ui.ExpandedView)
},
"navigateToMostPlayed": func() {
ui.Ui.Navbar.Select(2, 0)
},
"navigateToSearch": func() {
2022-08-31 12:12:15 -06:00
views.SetCurrentView(views.SView)
ui.Ui.Navbar.Select(3, 0)
views.SView.Update(ui.Ui.ExpandedView)
},
"quit": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().Quit()
},
"stop": func() {
2022-09-08 14:03:13 -06:00
if err := Conn.Stop(); err != nil {
notify.Notify.Send("Could not Stop the Playback")
2021-12-31 03:18:58 -07:00
} else {
notify.Notify.Send("Playback Stopped")
2021-12-31 03:18:58 -07:00
}
},
"updateDB": func() {
2022-09-08 14:03:13 -06:00
_, err = Conn.Update("")
if err != nil {
notify.Notify.Send("Could Not Update the Database")
2021-12-31 03:18:58 -07:00
} else {
notify.Notify.Send("Database Updated")
}
},
"deleteSongFromPlaylist": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().DeleteSongFromPlaylist()
},
2021-11-15 04:02:59 -07:00
"FocusSearch": func() {
ui.Ui.App.SetFocus(ui.Ui.SearchBar)
2021-11-15 04:02:59 -07:00
},
"FocusBuffSearch": func() {
2022-08-31 12:12:15 -06:00
views.GetCurrentView().FocusBuffSearchView()
},
2022-09-08 14:01:51 -06:00
"SeekForward": func() {
SeekFunc(false)
2022-09-08 14:01:51 -06:00
},
"SeekBackward": func() {
SeekFunc(true)
2022-09-08 14:01:51 -06:00
},
}
2021-12-24 03:07:21 -07:00
// 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
2021-12-22 08:11:48 -07:00
config.GenerateKeyMap(FuncMap)
ui.Ui.SearchBar.SetAutocompleteFunc(func(c string) []string {
2022-08-31 12:12:15 -06:00
if views.GetCurrentView().GetViewName() == "BuffSearchView" {
return nil
} else {
if c != "" && c != " " && c != " " {
_, _, w, _ := ui.Ui.SearchBar.GetRect()
matches := fuzzy.Find(c, ArtistTreeContent)
var suggestions []string
for i, match := range matches {
if i == 10 {
break
}
suggestions = append(suggestions, utils.GetFormattedString(match.Str, w-2))
2021-11-15 04:02:59 -07:00
}
return suggestions
} else {
return make([]string, 0)
2021-11-15 04:02:59 -07:00
}
}
})
2021-12-24 03:07:21 -07:00
// Input Handler
ui.Ui.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
2021-12-22 08:11:48 -07:00
FuncMap[val]()
return nil
} else {
2022-08-31 12:12:15 -06:00
if views.GetCurrentView().GetViewName() == "PlaylistView" {
if e.Rune() == 'j' || e.Rune() == 'k' {
2022-09-08 14:03:13 -06:00
if p, err := Conn.PlaylistInfo(-1, -1); err != nil {
notify.Notify.Send("Error Getting PlaylistInfo")
} else {
if len(p) == 0 {
notify.Notify.Send("Empty Playlist")
return nil
}
}
}
} else if views.GetCurrentView().GetViewName() == "SearchView" {
if e.Rune() == 'j' || e.Rune() == 'k' {
if client.SearchContentSlice == nil || len(client.SearchContentSlice) == 0 {
notify.Notify.Send("No Search Results")
return nil
}
}
}
return e
}
})
ui.Ui.SearchBar.SetDoneFunc(func(e tcell.Key) {
if e == tcell.KeyEnter {
ui.Ui.ExpandedView.Select(0, 0)
2022-08-31 12:12:15 -06:00
if views.GetCurrentView().GetViewName() == "BuffSearchView" {
ui.Ui.App.SetFocus(ui.Ui.ExpandedView)
} else {
2022-08-31 12:12:15 -06:00
views.SetCurrentView(views.SView)
client.SearchContentSlice = nil
client.SearchContentSlice, err = client.GenerateContentSlice(ui.Ui.SearchBar.GetText())
if err != nil {
notify.Notify.Send("Could Not Retrieve the Results")
} else {
ui.Ui.SearchBar.SetText("")
ui.Ui.App.SetFocus(ui.Ui.ExpandedView)
ui.Ui.Navbar.Select(3, 0)
}
}
}
if e == tcell.KeyEscape {
2022-09-06 23:57:20 -06:00
if views.GetCurrentView().GetViewName() == "BuffSearchView" {
client.Matches = nil
}
ui.Ui.SearchBar.SetText("")
ui.Ui.App.SetFocus(ui.Ui.ExpandedView)
}
})
ui.Ui.ExpandedView.SetDoneFunc(func(e tcell.Key) {
if e == tcell.KeyEscape {
2022-08-31 12:12:15 -06:00
if views.GetCurrentView().GetViewName() == "BuffSearchView" {
views.SetCurrentView(views.FView)
ui.Ui.SearchBar.SetText("")
client.Matches = nil
}
}
})
ui.Ui.SearchBar.SetChangedFunc(func(text string) {
2022-08-31 12:12:15 -06:00
if views.GetCurrentView().GetViewName() == "BuffSearchView" {
var f client.FileNodes = client.DirTree.Children
client.Matches = fuzzy.FindFrom(text, f)
views.BuffSView.Update(ui.Ui.ExpandedView)
}
})
go func() {
for {
2022-10-07 15:21:22 -06:00
ui.Ui.App.Draw()
time.Sleep(time.Second)
}
}()
if err := ui.Ui.App.Run(); err != nil {
panic(err)
}
}