lmcli/pkg/tui/shared/shared.go

70 lines
1.2 KiB
Go

package shared
import (
tea "github.com/charmbracelet/bubbletea"
)
// An analogue to tea.Model with support for checking if the model has been
// initialized before
type ViewModel interface {
Init() tea.Cmd
Update(tea.Msg) (ViewModel, tea.Cmd)
View() string
Initialized() bool // Return whether this view is initialized
}
type ViewState struct {
Initialized bool
Width int
Height int
Err error
}
type View int
const (
ViewChat View = iota
ViewConversations
//StateSettings
//StateHelp
)
// a convenience struct for holding rendered content for indiviudal UI
// elements
type Sections struct {
Header string
Content string
Error string
Input string
Footer string
}
type (
// send to change the current state
MsgViewChange View
// sent to a state when it is entered
MsgViewEnter struct{}
// sent when an error occurs
MsgError error
// sent when the view has handled a key input
MsgKeyHandled tea.KeyMsg
)
func ViewEnter() tea.Cmd {
return func() tea.Msg {
return MsgViewEnter{}
}
}
func KeyHandled(key tea.KeyMsg) tea.Cmd {
return func() tea.Msg {
return MsgKeyHandled(key)
}
}
func WrapError(err error) tea.Cmd {
return func() tea.Msg {
return MsgError(err)
}
}