Add toggle_playback mapping

This commit is contained in:
aditya-K2
2023-04-16 12:58:35 +05:30
parent d69719389c
commit 93f8bd1c65
4 changed files with 31 additions and 1 deletions

View File

@@ -95,6 +95,7 @@ func GenerateMappings() map[string]map[Key]string {
{R: '2'}: "focus_playlists",
{R: '3'}: "focus_main_view",
{R: '?'}: "focus_search",
{R: ' '}: "toggle_playback",
},
"playlist_nav": {
{K: tcell.KeyEnter}: "open_entry",

View File

@@ -72,6 +72,11 @@ var (
"pause": tcell.KeyPause,
"backtab": tcell.KeyBacktab,
}
runeMap = map[rune]bool{
'?': true,
'!': true,
' ': true,
}
)
func (k *Key) Rune() rune {
@@ -88,7 +93,7 @@ func NewKey(s string) Key {
if (a[0] >= 'A' && a[0] <= 'Z') ||
(a[0] >= 'a' && a[0] <= 'z') ||
(a[0] >= '0' && a[0] <= '9') ||
(a[0] == '?' || a[0] == '!') {
(runeMap[a[0]]) {
return Key{R: a[0]}
}
}

View File

@@ -33,3 +33,20 @@ func PlayContext(context *spotify.URI) error {
PlaybackContext: context,
})
}
func TogglePlayback() error {
p, err := Client.PlayerCurrentlyPlaying(ctx())
if err != nil {
return err
}
if p.Playing {
if err := Client.Pause(ctx()); err != nil {
return err
}
} else {
if err := Client.Play(ctx()); err != nil {
return err
}
}
return nil
}

View File

@@ -4,6 +4,7 @@ import (
"time"
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/gspt/spt"
"github.com/aditya-K2/utils"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@@ -116,6 +117,12 @@ func NewApplication() *Application {
Ui.App.SetFocus(searchbar)
return nil
}, nil),
"toggle_playback": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
if err := spt.TogglePlayback(); err != nil {
SendNotification(err.Error())
}
return nil
}, pBar),
"choose_device": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
OpenDeviceMenu()
return nil