2024-05-30 00:44:40 -06:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
// 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)
|
|
|
|
|
2024-09-22 17:34:53 -06:00
|
|
|
// View methods
|
|
|
|
Header(width int) string
|
|
|
|
// Render the view's main content into a container of the given dimensions
|
2024-12-12 00:09:58 -07:00
|
|
|
Content(width, height int, errors string) string
|
2024-09-22 17:34:53 -06:00
|
|
|
Footer(width int) string
|
2024-05-30 00:44:40 -06:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
type View int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ViewChat View = iota
|
|
|
|
ViewConversations
|
2024-09-25 09:49:45 -06:00
|
|
|
ViewSettings
|
2024-09-16 09:40:04 -06:00
|
|
|
//StateHelp
|
|
|
|
)
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
type (
|
|
|
|
// send to change the current state
|
|
|
|
MsgViewChange View
|
2024-09-25 09:49:45 -06:00
|
|
|
// sent to a state when it is entered, with the view we're leaving
|
|
|
|
MsgViewEnter View
|
2024-09-22 17:34:53 -06:00
|
|
|
// sent when a recoverable error occurs (displayed to user)
|
2024-09-22 21:37:01 -06:00
|
|
|
MsgError struct { Err error }
|
2024-09-16 09:40:04 -06:00
|
|
|
// sent when the view has handled a key input
|
|
|
|
MsgKeyHandled tea.KeyMsg
|
2024-05-30 00:44:40 -06:00
|
|
|
)
|
|
|
|
|
2024-09-25 09:49:45 -06:00
|
|
|
func ViewEnter(from View) tea.Cmd {
|
2024-09-16 08:04:08 -06:00
|
|
|
return func() tea.Msg {
|
2024-09-25 09:49:45 -06:00
|
|
|
return MsgViewEnter(from)
|
2024-09-16 08:04:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-20 20:47:03 -06:00
|
|
|
func ChangeView(view View) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
return MsgViewChange(view)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
func KeyHandled(key tea.KeyMsg) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
return MsgKeyHandled(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 00:44:40 -06:00
|
|
|
func WrapError(err error) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
2024-09-22 21:37:01 -06:00
|
|
|
return MsgError{ Err: err }
|
2024-05-30 00:44:40 -06:00
|
|
|
}
|
|
|
|
}
|
2024-09-22 21:37:01 -06:00
|
|
|
|
|
|
|
func AsMsgError(err error) MsgError {
|
|
|
|
return MsgError{ Err: err }
|
|
|
|
}
|