gomp/main.go

260 lines
6.0 KiB
Go
Raw Normal View History

package main
import (
"strconv"
"time"
2021-12-12 13:05:40 -07:00
"github.com/aditya-K2/goMP/utils"
2021-11-28 11:03:34 -07: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 11:44:00 -07:00
var (
CONN *mpd.Client
UI *Application
NOTIFICATION_SERVER *NotificationServer
Volume int64
Random bool
Repeat bool
InsidePlaylist bool = true
InsideSearchView bool = false
ARTIST_TREE map[string]map[string]map[string]string
2021-11-15 11:44:00 -07: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()
cache.SetCacheDir(viper.GetString("CACHE_DIR"))
2021-10-24 01:56:10 -06:00
r := newRenderer()
c, _ := CONN.CurrentSong()
2021-10-24 01:56:10 -06:00
if len(c) != 0 {
r.Start(c["file"])
2021-10-24 01:56:10 -06:00
} else {
r.Start("stop")
}
UI = newApplication(r)
fileMap, err := CONN.GetFiles()
dirTree := generateDirectoryTree(fileMap)
2021-11-13 04:27:54 -07:00
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()
2021-12-12 13:05:40 -07:00
ARTIST_TREE_CONTENT := utils.ConvertToArray(ARTIST_TREE)
NOTIFICATION_SERVER = NewNotificationServer()
NOTIFICATION_SERVER.Start()
var SEARCH_CONTENT_SLICE []interface{}
2021-11-13 04:27:54 -07:00
UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
if InsidePlaylist {
2021-11-13 04:27:54 -07:00
UpdatePlaylist(UI.ExpandedView)
} else if InsideSearchView {
UpdateSearchView(UI.ExpandedView, SEARCH_CONTENT_SLICE)
} else {
2021-11-13 04:27:54 -07:00
Update(dirTree.children, UI.ExpandedView)
}
2021-11-13 04:27:54 -07:00
return UI.ExpandedView.GetInnerRect()
})
var FUNC_MAP = map[string]func(){
"showChildrenContent": func() {
2021-11-13 04:27:54 -07:00
r, _ := UI.ExpandedView.GetSelection()
if !InsidePlaylist && !InsideSearchView {
if len(dirTree.children[r].children) == 0 {
id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1)
CONN.PlayId(id)
} else {
2021-11-13 04:27:54 -07:00
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)
}
},
"togglePlayBack": func() {
togglePlayBack()
},
"showParentContent": func() {
if !InsidePlaylist && !InsideSearchView {
if dirTree.parent != nil {
2021-11-13 04:27:54 -07:00
Update(dirTree.parent.children, UI.ExpandedView)
dirTree = dirTree.parent
}
}
},
"nextSong": func() {
CONN.Next()
},
"clearPlaylist": func() {
CONN.Clear()
NOTIFICATION_SERVER.Send("PlayList Cleared")
},
"previousSong": func() {
CONN.Previous()
},
"addToPlaylist": func() {
if !InsidePlaylist && !InsideSearchView {
2021-11-13 04:27:54 -07:00
r, _ := UI.ExpandedView.GetSelection()
CONN.Add(dirTree.children[r].absolutePath)
} else if InsideSearchView {
r, _ := UI.ExpandedView.GetSelection()
AddToPlaylist(SEARCH_CONTENT_SLICE[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() {
InsidePlaylist = false
InsideSearchView = false
UI.Navbar.Select(1, 0)
2021-11-13 04:27:54 -07:00
Update(dirTree.children, UI.ExpandedView)
},
"navigateToPlaylist": func() {
InsidePlaylist = true
InsideSearchView = false
UI.Navbar.Select(0, 0)
2021-11-13 04:27:54 -07:00
UpdatePlaylist(UI.ExpandedView)
},
"navigateToMostPlayed": func() {
InsideSearchView = false
InsidePlaylist = false
UI.Navbar.Select(2, 0)
},
"navigateToSearch": func() {
InsideSearchView = true
InsidePlaylist = false
UI.Navbar.Select(3, 0)
},
"quit": func() {
UI.App.Stop()
},
"stop": func() {
CONN.Stop()
NOTIFICATION_SERVER.Send("Playback Stopped")
},
"updateDB": func() {
_, err = CONN.Update("")
if err != nil {
panic(err)
}
NOTIFICATION_SERVER.Send("Database Updated")
},
"deleteSongFromPlaylist": func() {
if InsidePlaylist {
2021-11-13 04:27:54 -07:00
r, _ := UI.ExpandedView.GetSelection()
CONN.Delete(r, -1)
}
},
2021-11-15 04:02:59 -07:00
"FocusSearch": func() {
UI.App.SetFocus(UI.SearchBar)
},
}
config.GenerateKeyMap(FUNC_MAP)
2021-11-15 04:02:59 -07:00
UI.SearchBar.SetAutocompleteFunc(func(c string) []string {
if c != "" && c != " " && c != " " {
2021-11-28 11:03:34 -07:00
_, _, w, _ := UI.SearchBar.GetRect()
matches := fuzzy.Find(c, ARTIST_TREE_CONTENT)
2021-11-15 04:02:59 -07:00
var suggestions []string
2021-11-28 11:03:34 -07:00
for i, match := range matches {
2021-11-15 04:02:59 -07:00
if i == 10 {
break
}
2021-12-12 13:05:40 -07:00
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-13 04:27:54 -07:00
UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
FUNC_MAP[val]()
return nil
} else {
return e
}
})
UI.SearchBar.SetDoneFunc(func(e tcell.Key) {
if e == tcell.KeyEnter {
UI.ExpandedView.Select(0, 0)
InsideSearchView = true
InsidePlaylist = false
SEARCH_CONTENT_SLICE = nil
SEARCH_CONTENT_SLICE, err = GenerateContentSlice(UI.SearchBar.GetText())
if err != nil {
NOTIFICATION_SERVER.Send("Could Not Retrieve the Results")
} else {
UI.SearchBar.SetText("")
UI.App.SetFocus(UI.ExpandedView)
UI.Navbar.Select(3, 0)
}
}
if e == tcell.KeyEscape {
InsideSearchView = 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)
}
}