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)
|
|
|
|
View() string
|
|
|
|
Initialized() bool // Return whether this view is initialized
|
|
|
|
}
|
|
|
|
|
|
|
|
type ViewState struct {
|
2024-09-16 08:04:08 -06:00
|
|
|
Initialized bool
|
2024-05-30 00:44:40 -06:00
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:40:04 -06:00
|
|
|
type View int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ViewChat View = iota
|
|
|
|
ViewConversations
|
|
|
|
//StateSettings
|
|
|
|
//StateHelp
|
|
|
|
)
|
|
|
|
|
2024-05-30 01:04:55 -06:00
|
|
|
// a convenience struct for holding rendered content for indiviudal UI
|
|
|
|
// elements
|
|
|
|
type Sections struct {
|
2024-05-30 00:44:40 -06:00
|
|
|
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
|
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-16 08:04:08 -06:00
|
|
|
func ViewEnter() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
return MsgViewEnter{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return MsgError(err)
|
|
|
|
}
|
|
|
|
}
|