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