feat: implement load and save handlers with corresponding tests
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/StevanFreeborn/term-tier/internal/core"
|
||||
"github.com/StevanFreeborn/term-tier/internal/state"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type saveHandler struct {
|
||||
keys []string
|
||||
description string
|
||||
saver core.Saver
|
||||
}
|
||||
|
||||
func NewSaveHandler(saver core.Saver) KeyMsgHandler {
|
||||
return saveHandler{
|
||||
keys: []string{"ctrl+s"},
|
||||
description: "Save tiers",
|
||||
saver: saver,
|
||||
}
|
||||
}
|
||||
|
||||
func (h saveHandler) KeysHandled() []string {
|
||||
return h.keys
|
||||
}
|
||||
|
||||
func (h saveHandler) Description() string {
|
||||
return h.description
|
||||
}
|
||||
|
||||
func (h saveHandler) Match(s state.State, msg tea.KeyMsg) bool {
|
||||
return s.Mode() == state.NavigationMode && slices.Contains(h.keys, msg.String())
|
||||
}
|
||||
|
||||
func (h saveHandler) Handle(s state.State, msg tea.KeyMsg) tea.Cmd {
|
||||
return s.StartInputMode("Save tiers", func(val string, s state.State) tea.Cmd {
|
||||
if err := h.saver.Save(val, s.Save()); err != nil {
|
||||
s.SetStatus("Error: " + err.Error())
|
||||
} else {
|
||||
s.SetStatus("Saved to " + val)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user