Matt Low
02228d65ac
- Allow each view to position error banners where they choose - Add global 'esc' key handler to dismiss errors
67 lines
1.3 KiB
Go
67 lines
1.3 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 methods
|
|
Header(width int) string
|
|
// Render the view's main content into a container of the given dimensions
|
|
Content(width, height int, errors string) string
|
|
Footer(width int) string
|
|
}
|
|
|
|
type View int
|
|
|
|
const (
|
|
ViewChat View = iota
|
|
ViewConversations
|
|
ViewSettings
|
|
//StateHelp
|
|
)
|
|
|
|
type (
|
|
// send to change the current state
|
|
MsgViewChange View
|
|
// sent to a state when it is entered, with the view we're leaving
|
|
MsgViewEnter View
|
|
// sent when a recoverable error occurs (displayed to user)
|
|
MsgError struct { Err error }
|
|
// sent when the view has handled a key input
|
|
MsgKeyHandled tea.KeyMsg
|
|
)
|
|
|
|
func ViewEnter(from View) tea.Cmd {
|
|
return func() tea.Msg {
|
|
return MsgViewEnter(from)
|
|
}
|
|
}
|
|
|
|
func ChangeView(view View) tea.Cmd {
|
|
return func() tea.Msg {
|
|
return MsgViewChange(view)
|
|
}
|
|
}
|
|
|
|
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: err }
|
|
}
|
|
}
|
|
|
|
func AsMsgError(err error) MsgError {
|
|
return MsgError{ Err: err }
|
|
}
|