Use utils.MergeMaps

This commit is contained in:
aditya-K2
2023-04-16 05:58:51 +05:30
parent 4faea6af9b
commit d5d9bbebff
4 changed files with 18 additions and 33 deletions

View File

@@ -132,18 +132,9 @@ func GenerateMappings() map[string]map[Key]string {
} }
} }
} }
mergeMaps := func(maps ...map[Key]string) map[Key]string {
result := make(map[Key]string)
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}
for k := range keys { for k := range keys {
if k != "global" { if k != "global" {
keys[k] = mergeMaps(keys["global"], keys[k]) keys[k] = utils.MergeMaps(keys["global"], keys[k])
} }
} }
return keys return keys

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/aditya-K2/gspt
go 1.19 go 1.19
require ( require (
github.com/aditya-K2/utils v0.0.0-20230324221547-e982ed1e980e github.com/aditya-K2/utils v0.0.0-20230416002345-2f8487d8c111
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/gdamore/tcell/v2 v2.5.3 github.com/gdamore/tcell/v2 v2.5.3
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646

4
go.sum
View File

@@ -49,6 +49,10 @@ github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046 h1:O/r2Sj+8QcMF
github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k= github.com/BurntSushi/xgbutil v0.0.0-20190907113008-ad855c713046/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k=
github.com/aditya-K2/utils v0.0.0-20230324221547-e982ed1e980e h1:blLZ2IHGPfAo2zOEpj4FsuqliHmpLw4u2Zp6GR0VMZ8= github.com/aditya-K2/utils v0.0.0-20230324221547-e982ed1e980e h1:blLZ2IHGPfAo2zOEpj4FsuqliHmpLw4u2Zp6GR0VMZ8=
github.com/aditya-K2/utils v0.0.0-20230324221547-e982ed1e980e/go.mod h1:M5vD+szIYMn9sAmBGSzwTiwfI63qtzCwguujFy8wybI= github.com/aditya-K2/utils v0.0.0-20230324221547-e982ed1e980e/go.mod h1:M5vD+szIYMn9sAmBGSzwTiwfI63qtzCwguujFy8wybI=
github.com/aditya-K2/utils v0.0.0-20230416002208-527506a25c18 h1:du8ANNF3XwAV2yCwUBh08hxQMzefwWTzMVS4WNuDUXQ=
github.com/aditya-K2/utils v0.0.0-20230416002208-527506a25c18/go.mod h1:M5vD+szIYMn9sAmBGSzwTiwfI63qtzCwguujFy8wybI=
github.com/aditya-K2/utils v0.0.0-20230416002345-2f8487d8c111 h1:nRszo2Av5WTgmZa3TX86Ohs3Fi73YOgMEKbmTZ3CJoE=
github.com/aditya-K2/utils v0.0.0-20230416002345-2f8487d8c111/go.mod h1:M5vD+szIYMn9sAmBGSzwTiwfI63qtzCwguujFy8wybI=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=

View File

@@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/aditya-K2/gspt/config" "github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/utils"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@@ -133,19 +134,8 @@ func NewApplication() *Application {
}, nil), }, nil),
} }
generateActions := func(m map[string]*Action) map[string]*Action {
res := make(map[string]*Action)
for k, v := range globalMaps {
res[k] = v
}
for k, v := range m {
res[k] = v
}
return res
}
// Actions // Actions
playlistNav.SetActions(generateActions(map[string]*Action{ playlistNav.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"play_entry": NewAction(playlistNav.PlaySelectEntry, pBar), "play_entry": NewAction(playlistNav.PlaySelectEntry, pBar),
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
r, _ := playlistNav.Table.GetSelection() r, _ := playlistNav.Table.GetSelection()
@@ -155,36 +145,36 @@ func NewApplication() *Application {
return nil return nil
}, nil), }, nil),
})) }))
navMenu.SetActions(generateActions(map[string]*Action{ navMenu.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(navMenu.SelectEntry, nil), "open_entry": NewAction(navMenu.SelectEntry, nil),
})) }))
playlistView.SetActions(generateActions(map[string]*Action{ playlistView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(*tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(*tcell.EventKey) *tcell.EventKey {
playlistView.PlaySelectEntry() playlistView.PlaySelectEntry()
return nil return nil
}, pBar), }, pBar),
})) }))
recentlyPlayedView.SetActions(generateActions(map[string]*Action{ recentlyPlayedView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(recentlyPlayedView.SelectEntry, pBar), "open_entry": NewAction(recentlyPlayedView.SelectEntry, pBar),
})) }))
topTracksView.SetActions(generateActions(map[string]*Action{ topTracksView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { topTracksView.OpenSelectEntry(); return nil }, pBar), "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { topTracksView.OpenSelectEntry(); return nil }, pBar),
"play_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { topTracksView.PlaySelectedEntry(); return nil }, pBar), "play_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { topTracksView.PlaySelectedEntry(); return nil }, pBar),
})) }))
likedSongsView.SetActions(generateActions(map[string]*Action{ likedSongsView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
likedSongsView.OpenEntry() likedSongsView.OpenEntry()
return nil return nil
}, pBar), }, pBar),
})) }))
searchView.SetActions(generateActions(map[string]*Action{})) searchView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{}))
artistsView.SetActions(generateActions(map[string]*Action{ artistsView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
artistsView.OpenArtist() artistsView.OpenArtist()
return nil return nil
}, nil), }, nil),
})) }))
artistView.SetActions(generateActions(map[string]*Action{ artistView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
artistView.OpenEntry() artistView.OpenEntry()
return nil return nil
@@ -194,7 +184,7 @@ func NewApplication() *Application {
return nil return nil
}, pBar), }, pBar),
})) }))
albumsView.SetActions(generateActions(map[string]*Action{ albumsView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
albumsView.OpenAlbum() albumsView.OpenAlbum()
return nil return nil
@@ -204,7 +194,7 @@ func NewApplication() *Application {
return nil return nil
}, pBar), }, pBar),
})) }))
albumView.SetActions(generateActions(map[string]*Action{ albumView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey { "open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
albumView.PlaySelectEntry() albumView.PlaySelectEntry()
return nil return nil