Skip to content

Commit

Permalink
Merge pull request #47 from Insprill/feature/customizable-keybinds
Browse files Browse the repository at this point in the history
Add support for customizable keybinds
  • Loading branch information
wildeyedskies authored Oct 19, 2024
2 parents 41a477c + a62b1fd commit 2d5b0da
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 35 deletions.
85 changes: 50 additions & 35 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/viper"
"github.com/wildeyedskies/go-mpv/mpv"
)

Expand Down Expand Up @@ -559,21 +560,20 @@ func (ui *Ui) createBrowserPage(titleFlex *tview.Flex, indexes *[]SubsonicIndex)

// going right from the artist list should focus the album/song list
ui.artistList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRight {
switch keyName(event) {
case keybind("right"):
ui.app.SetFocus(ui.entityList)
return nil
}
switch event.Rune() {
case '/':
case keybind("search"):
ui.search()
return nil
case 'n':
case keybind("searchNext"):
ui.searchNext()
return nil
case 'N':
case keybind("searchPrev"):
ui.searchPrev()
return nil
case 'r':
case keybind("refresh"):
goBackTo := ui.artistList.GetCurrentItem()
// REFRESH artists
indexResponse, err := ui.connection.GetIndexes()
Expand Down Expand Up @@ -630,26 +630,26 @@ func (ui *Ui) createBrowserPage(titleFlex *tview.Flex, indexes *[]SubsonicIndex)
})

ui.entityList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyLeft {
if keyName(event) == keybind("left") {
ui.app.SetFocus(ui.artistList)
return nil
}
if event.Rune() == 'a' {
if keyName(event) == keybind("add") {
ui.handleAddEntityToQueue()
return nil
}
if event.Rune() == 'y' {
if keyName(event) == keybind("star") {
ui.handleToggleEntityStar()
return nil
}
// only makes sense to add to a playlist if there are playlists
if event.Rune() == 'A' && ui.playlistList.GetItemCount() > 0 {
if keyName(event) == keybind("addToPlaylist") && ui.playlistList.GetItemCount() > 0 {
ui.pages.ShowPage("addToPlaylist")
ui.app.SetFocus(ui.addToPlaylistList)
return nil
}
// REFRESH only the artist
if event.Rune() == 'r' {
if keyName(event) == keybind("refresh") {
artistIdx := ui.artistList.GetCurrentItem()
entity := ui.artistIdList[artistIdx]
//ui.logger.Printf("refreshing artist idx %d, entity %s (%s)", artistIdx, entity, ui.connection.directoryCache[entity].Directory.Name)
Expand All @@ -668,10 +668,10 @@ func (ui *Ui) createQueuePage(titleFlex *tview.Flex) *tview.Flex {
AddItem(titleFlex, 1, 0, false).
AddItem(ui.queueList, 0, 1, true)
ui.queueList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDelete || event.Rune() == 'd' {
if event.Key() == tcell.KeyDelete || keyName(event) == keybind("removeFromQueue") {
ui.handleDeleteFromQueue()
return nil
} else if event.Rune() == 'y' {
} else if keyName(event) == keybind("star") {
ui.handleToggleStar()
return nil
}
Expand Down Expand Up @@ -720,30 +720,30 @@ func (ui *Ui) createPlaylistPage(titleFlex *tview.Flex) (*tview.Flex, tview.Prim
})

ui.playlistList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRight {
if keyName(event) == keybind("right") {
ui.app.SetFocus(ui.selectedPlaylist)
return nil
}
if event.Rune() == 'a' {
if keyName(event) == keybind("add") {
ui.handleAddPlaylistToQueue()
return nil
}
if event.Rune() == 'n' {
if keyName(event) == keybind("newPlaylist") {
playlistFlex.AddItem(ui.newPlaylistInput, 0, 1, true)
ui.app.SetFocus(ui.newPlaylistInput)
}
if event.Rune() == 'd' {
if keyName(event) == keybind("deletePlaylist") {
ui.pages.ShowPage("deletePlaylist")
}
return event
})

ui.selectedPlaylist.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyLeft {
if keyName(event) == keybind("left") {
ui.app.SetFocus(ui.playlistList)
return nil
}
if event.Rune() == 'a' {
if keyName(event) == keybind("add") {
ui.handleAddPlaylistSongToQueue()
return nil
}
Expand Down Expand Up @@ -817,33 +817,33 @@ func InitGui(indexes *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection
return event
}

switch event.Rune() {
case '1':
switch keyName(event) {
case keybind("pageBrowser"):
ui.pages.SwitchToPage("browser")
ui.currentPage.SetText("Browser")
case '2':
case keybind("pageQueue"):
ui.pages.SwitchToPage("queue")
ui.currentPage.SetText("Queue")
case '3':
case keybind("pagePlaylists"):
ui.pages.SwitchToPage("playlists")
ui.currentPage.SetText("Playlists")
case '4':
case keybind("pageLog"):
ui.pages.SwitchToPage("log")
ui.currentPage.SetText("Log")
case 'q':
case keybind("quit"):
ui.player.EventChannel <- nil
ui.player.Instance.TerminateDestroy()
ui.app.Stop()
case 's':
case keybind("addRandomSongs"):
ui.handleAddRandomSongs()
case 'D':
case keybind("clearQueue"):
ui.player.Queue = make([]QueueItem, 0)
err := ui.player.Stop()
if err != nil {
ui.connection.Logger.Printf("InitGui: Stop -- %s", err.Error())
}
updateQueueList(ui.player, ui.queueList, ui.starIdList)
case 'p':
case keybind("playPause"):
status, err := ui.player.Pause()
if err != nil {
ui.connection.Logger.Printf("InitGui: Pause -- %s", err.Error())
Expand All @@ -858,28 +858,30 @@ func InitGui(indexes *[]SubsonicIndex, playlists *[]SubsonicPlaylist, connection
ui.startStopStatus.SetText("[::b]stmp: [yellow]paused")
}
return nil
case '-':
case keybind("volumeDown"):
if err := ui.player.AdjustVolume(-5); err != nil {
ui.connection.Logger.Printf("InitGui: AdjustVolume %d -- %s", -5, err.Error())
}
return nil

case '=':
case keybind("volumeUp"):
if err := ui.player.AdjustVolume(5); err != nil {
ui.connection.Logger.Printf("InitGui: AdjustVolume %d -- %s", 5, err.Error())
}
return nil

case '.':
case keybind("seekForward"):
if err := ui.player.Seek(10); err != nil {
ui.connection.Logger.Printf("InitGui: Seek %d -- %s", 10, err.Error())
}
return nil
case ',':
case keybind("seekBack"):
if err := ui.player.Seek(-10); err != nil {
ui.connection.Logger.Printf("InitGui: Seek %d -- %s", -10, err.Error())
}
return nil
case keybind("up"):
return tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone)
case keybind("down"):
return tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone)
}

return event
Expand Down Expand Up @@ -1065,3 +1067,16 @@ func (e SubsonicEntity) getSongTitle() string {

return e.Path[lastSlash+1 : len(e.Path)]
}

func keyName(event *tcell.EventKey) string {
if (event.Key() == tcell.KeyRune) {
return string(event.Rune())
} else {
return event.Name()
}
}

func keybind(path string) string {
return viper.GetString("keys." + path)
}

29 changes: 29 additions & 0 deletions stmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ func readConfig() {
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.config/stmp")
viper.AddConfigPath(".")

// Keybinds
viper.SetDefault("keys.search", "/")
viper.SetDefault("keys.searchNext", "n")
viper.SetDefault("keys.searchPrev", "N")
viper.SetDefault("keys.refresh", "r")
viper.SetDefault("keys.add", "a")
viper.SetDefault("keys.star", "y")
viper.SetDefault("keys.newPlaylist", "a")
viper.SetDefault("keys.addToPlaylist", "A")
viper.SetDefault("keys.deletePlaylist", "d")
viper.SetDefault("keys.removeFromQueue", "d")
viper.SetDefault("keys.pageBrowser", "1")
viper.SetDefault("keys.pageQueue", "2")
viper.SetDefault("keys.pagePlaylists", "3")
viper.SetDefault("keys.pageLog", "4")
viper.SetDefault("keys.quit", "q")
viper.SetDefault("keys.addRandomSongs", "s")
viper.SetDefault("keys.clearQueue", "D")
viper.SetDefault("keys.playPause", "p")
viper.SetDefault("keys.volumeDown", "-")
viper.SetDefault("keys.volumeUp", "=")
viper.SetDefault("keys.seekForward", ".")
viper.SetDefault("keys.seekBack", ",")
viper.SetDefault("keys.up", "Up")
viper.SetDefault("keys.down", "Down")
viper.SetDefault("keys.left", "Left")
viper.SetDefault("keys.right", "Right")

err := viper.ReadInConfig()

if err != nil {
Expand Down

0 comments on commit 2d5b0da

Please sign in to comment.