2021-10-12 12:30:16 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-17 11:00:57 -06:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-11-28 11:03:34 -07:00
|
|
|
"github.com/aditya-K2/fuzzy"
|
2021-11-20 09:17:29 -07:00
|
|
|
"github.com/aditya-K2/goMP/cache"
|
2021-11-11 09:27:01 -07:00
|
|
|
"github.com/aditya-K2/goMP/config"
|
2021-10-14 10:34:16 -06:00
|
|
|
"github.com/fhs/gompd/mpd"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-11-05 01:10:46 -06:00
|
|
|
"github.com/spf13/viper"
|
2021-10-12 12:30:16 -06:00
|
|
|
)
|
|
|
|
|
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
|
2021-11-16 08:23:22 -07:00
|
|
|
InsideSearchView bool = false
|
|
|
|
ARTIST_TREE map[string]map[string]map[string]string
|
2021-11-15 11:44:00 -07:00
|
|
|
)
|
2021-10-17 11:00:57 -06:00
|
|
|
|
|
|
|
func main() {
|
2021-11-11 09:27:01 -07:00
|
|
|
config.ReadConfig()
|
2021-10-14 10:34:16 -06:00
|
|
|
// Connect to MPD server
|
2021-11-12 23:02:00 -07:00
|
|
|
var mpdConnectionError error
|
|
|
|
CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
|
|
|
|
if mpdConnectionError != nil {
|
|
|
|
panic(mpdConnectionError)
|
2021-10-14 10:34:16 -06:00
|
|
|
}
|
2021-11-12 23:02:00 -07:00
|
|
|
defer CONN.Close()
|
2021-11-20 09:17:29 -07:00
|
|
|
cache.SetCacheDir(viper.GetString("CACHE_DIR"))
|
2021-11-26 09:32:20 -07:00
|
|
|
cache.SetDefaultPath(viper.GetString("DEFAULT_IMAGE_PATH"))
|
2021-11-20 09:17:29 -07:00
|
|
|
cache.LoadCache(viper.GetString("CACHE_FILE"))
|
2021-10-24 01:56:10 -06:00
|
|
|
r := newRenderer()
|
2021-11-12 23:02:00 -07:00
|
|
|
c, _ := CONN.CurrentSong()
|
2021-10-24 01:56:10 -06:00
|
|
|
if len(c) != 0 {
|
2021-11-12 23:02:00 -07:00
|
|
|
r.Start(c["file"])
|
2021-10-24 01:56:10 -06:00
|
|
|
} else {
|
|
|
|
r.Start("stop")
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:23:59 -07:00
|
|
|
UI = newApplication(r)
|
2021-10-14 10:34:16 -06:00
|
|
|
|
2021-11-12 23:02:00 -07:00
|
|
|
fileMap, err := CONN.GetFiles()
|
2021-10-17 20:21:57 -06:00
|
|
|
dirTree := generateDirectoryTree(fileMap)
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-11-13 04:27:54 -07:00
|
|
|
UpdatePlaylist(UI.ExpandedView)
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-11-12 23:02:00 -07:00
|
|
|
_v, _ := CONN.Status()
|
2021-10-17 20:21:57 -06:00
|
|
|
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
|
|
|
|
Random, _ = strconv.ParseBool(_v["random"])
|
|
|
|
Repeat, _ = strconv.ParseBool(_v["repeat"])
|
2021-10-17 11:00:57 -06:00
|
|
|
|
2021-11-16 08:23:22 -07:00
|
|
|
ARTIST_TREE, err = GenerateArtistTree()
|
2021-11-28 11:03:34 -07:00
|
|
|
ARTIST_TREE_CONTENT := ConvertToArray()
|
2021-11-16 08:23:22 -07:00
|
|
|
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) {
|
2021-10-18 00:48:51 -06:00
|
|
|
if InsidePlaylist {
|
2021-11-13 04:27:54 -07:00
|
|
|
UpdatePlaylist(UI.ExpandedView)
|
2021-11-16 08:23:22 -07:00
|
|
|
} else if InsideSearchView {
|
|
|
|
UpdateSearchView(UI.ExpandedView, SEARCH_CONTENT_SLICE)
|
2021-10-18 00:48:51 -06:00
|
|
|
} else {
|
2021-11-13 04:27:54 -07:00
|
|
|
Update(dirTree.children, UI.ExpandedView)
|
2021-10-18 00:48:51 -06:00
|
|
|
}
|
2021-11-13 04:27:54 -07:00
|
|
|
return UI.ExpandedView.GetInnerRect()
|
2021-10-18 00:48:51 -06:00
|
|
|
})
|
|
|
|
|
2021-11-12 01:36:51 -07:00
|
|
|
var FUNC_MAP = map[string]func(){
|
2021-11-11 09:27:01 -07:00
|
|
|
"showChildrenContent": func() {
|
2021-11-13 04:27:54 -07:00
|
|
|
r, _ := UI.ExpandedView.GetSelection()
|
2021-11-16 08:23:22 -07:00
|
|
|
if !InsidePlaylist && !InsideSearchView {
|
2021-11-11 09:27:01 -07:00
|
|
|
if len(dirTree.children[r].children) == 0 {
|
2021-11-12 23:02:00 -07:00
|
|
|
id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1)
|
|
|
|
CONN.PlayId(id)
|
2021-11-11 09:27:01 -07:00
|
|
|
} else {
|
2021-11-13 04:27:54 -07:00
|
|
|
Update(dirTree.children[r].children, UI.ExpandedView)
|
2021-11-11 09:27:01 -07:00
|
|
|
dirTree = &dirTree.children[r]
|
|
|
|
}
|
2021-11-16 08:23:22 -07:00
|
|
|
} else if InsidePlaylist {
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Play(r)
|
2021-11-16 08:23:22 -07:00
|
|
|
} else if InsideSearchView {
|
|
|
|
r, _ := UI.ExpandedView.GetSelection()
|
|
|
|
AddToPlaylist(SEARCH_CONTENT_SLICE[r], true)
|
2021-11-11 09:27:01 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"togglePlayBack": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
togglePlayBack()
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"showParentContent": func() {
|
2021-11-16 08:23:22 -07:00
|
|
|
if !InsidePlaylist && !InsideSearchView {
|
2021-11-11 09:27:01 -07:00
|
|
|
if dirTree.parent != nil {
|
2021-11-13 04:27:54 -07:00
|
|
|
Update(dirTree.parent.children, UI.ExpandedView)
|
2021-11-11 09:27:01 -07:00
|
|
|
dirTree = dirTree.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nextSong": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Next()
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"clearPlaylist": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Clear()
|
2021-11-13 23:32:36 -07:00
|
|
|
NOTIFICATION_SERVER.Send("PlayList Cleared")
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"previousSong": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Previous()
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"addToPlaylist": func() {
|
2021-11-16 08:23:22 -07:00
|
|
|
if !InsidePlaylist && !InsideSearchView {
|
2021-11-13 04:27:54 -07:00
|
|
|
r, _ := UI.ExpandedView.GetSelection()
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Add(dirTree.children[r].absolutePath)
|
2021-11-16 08:23:22 -07:00
|
|
|
} else if InsideSearchView {
|
|
|
|
r, _ := UI.ExpandedView.GetSelection()
|
|
|
|
AddToPlaylist(SEARCH_CONTENT_SLICE[r], false)
|
2021-11-11 09:27:01 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"toggleRandom": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
err := CONN.Random(!Random)
|
2021-11-11 09:27:01 -07:00
|
|
|
if err == nil {
|
|
|
|
Random = !Random
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"toggleRepeat": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
err := CONN.Repeat(!Repeat)
|
2021-11-11 09:27:01 -07:00
|
|
|
if err == nil {
|
|
|
|
Repeat = !Repeat
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"decreaseVolume": func() {
|
|
|
|
if Volume <= 0 {
|
|
|
|
Volume = 0
|
|
|
|
} else {
|
|
|
|
Volume -= 10
|
|
|
|
}
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.SetVolume(int(Volume))
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"increaseVolume": func() {
|
|
|
|
if Volume >= 100 {
|
|
|
|
Volume = 100
|
|
|
|
} else {
|
|
|
|
Volume += 10
|
|
|
|
}
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.SetVolume(int(Volume))
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"navigateToFiles": func() {
|
|
|
|
InsidePlaylist = false
|
2021-11-16 08:23:22 -07:00
|
|
|
InsideSearchView = false
|
2021-11-11 09:27:01 -07:00
|
|
|
UI.Navbar.Select(1, 0)
|
2021-11-13 04:27:54 -07:00
|
|
|
Update(dirTree.children, UI.ExpandedView)
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"navigateToPlaylist": func() {
|
|
|
|
InsidePlaylist = true
|
2021-11-16 08:23:22 -07:00
|
|
|
InsideSearchView = false
|
2021-11-11 09:27:01 -07:00
|
|
|
UI.Navbar.Select(0, 0)
|
2021-11-13 04:27:54 -07:00
|
|
|
UpdatePlaylist(UI.ExpandedView)
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"navigateToMostPlayed": func() {
|
2021-11-16 08:23:22 -07:00
|
|
|
InsideSearchView = false
|
2021-11-11 09:27:01 -07:00
|
|
|
InsidePlaylist = false
|
|
|
|
UI.Navbar.Select(2, 0)
|
|
|
|
},
|
2021-11-16 08:23:22 -07:00
|
|
|
"navigateToSearch": func() {
|
|
|
|
InsideSearchView = true
|
|
|
|
InsidePlaylist = false
|
|
|
|
UI.Navbar.Select(3, 0)
|
|
|
|
},
|
2021-11-11 09:27:01 -07:00
|
|
|
"quit": func() {
|
|
|
|
UI.App.Stop()
|
|
|
|
},
|
|
|
|
"stop": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Stop()
|
2021-11-13 23:32:36 -07:00
|
|
|
NOTIFICATION_SERVER.Send("Playback Stopped")
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"updateDB": func() {
|
2021-11-12 23:02:00 -07:00
|
|
|
_, err = CONN.Update("")
|
2021-11-11 09:27:01 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-13 23:32:36 -07:00
|
|
|
NOTIFICATION_SERVER.Send("Database Updated")
|
2021-11-11 09:27:01 -07:00
|
|
|
},
|
|
|
|
"deleteSongFromPlaylist": func() {
|
|
|
|
if InsidePlaylist {
|
2021-11-13 04:27:54 -07:00
|
|
|
r, _ := UI.ExpandedView.GetSelection()
|
2021-11-12 23:02:00 -07:00
|
|
|
CONN.Delete(r, -1)
|
2021-11-11 09:27:01 -07:00
|
|
|
}
|
|
|
|
},
|
2021-11-15 04:02:59 -07:00
|
|
|
"FocusSearch": func() {
|
|
|
|
UI.App.SetFocus(UI.SearchBar)
|
|
|
|
},
|
2021-11-11 09:27:01 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:36:51 -07:00
|
|
|
config.GenerateKeyMap(FUNC_MAP)
|
2021-11-16 08:23:22 -07:00
|
|
|
|
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-11-28 11:03:34 -07:00
|
|
|
suggestions = append(suggestions, getFormattedString(match.Str, w-2))
|
2021-11-15 04:02:59 -07:00
|
|
|
}
|
|
|
|
return suggestions
|
|
|
|
} else {
|
|
|
|
return make([]string, 0)
|
|
|
|
}
|
|
|
|
})
|
2021-11-16 08:23:22 -07:00
|
|
|
|
2021-11-13 04:27:54 -07:00
|
|
|
UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
|
2021-11-11 11:00:40 -07:00
|
|
|
if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
|
2021-11-12 01:36:51 -07:00
|
|
|
FUNC_MAP[val]()
|
2021-11-11 11:00:40 -07:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return e
|
2021-10-17 11:00:57 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-11-16 08:23:22 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-17 11:00:57 -06:00
|
|
|
go func() {
|
|
|
|
for {
|
2021-10-17 20:43:21 -06:00
|
|
|
UI.App.Draw()
|
2021-10-17 11:00:57 -06:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-17 20:43:21 -06:00
|
|
|
if err := UI.App.Run(); err != nil {
|
2021-10-12 12:30:16 -06:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-20 09:17:29 -07:00
|
|
|
cache.WriteCache(viper.GetString("CACHE_FILE"))
|
2021-10-12 12:30:16 -06:00
|
|
|
}
|