tui: Add setting view with support for changing the current model
This commit is contained in:
@@ -10,15 +10,11 @@ import (
|
||||
tuiutil "git.mlow.ca/mlow/lmcli/pkg/tui/util"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/tui/views/chat"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/tui/views/conversations"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/tui/views/settings"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type LaunchOptions struct {
|
||||
InitialConversation *api.Conversation
|
||||
InitialView shared.View
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
App *model.AppModel
|
||||
|
||||
@@ -26,7 +22,8 @@ type Model struct {
|
||||
width int
|
||||
height int
|
||||
|
||||
// errors we will display to the user and allow them to dismiss
|
||||
// errors to display
|
||||
// TODO: allow dismissing errors
|
||||
errs []error
|
||||
|
||||
activeView shared.View
|
||||
@@ -34,11 +31,7 @@ type Model struct {
|
||||
}
|
||||
|
||||
func initialModel(ctx *lmcli.Context, opts LaunchOptions) *Model {
|
||||
app := &model.AppModel{
|
||||
Ctx: ctx,
|
||||
Conversation: opts.InitialConversation,
|
||||
}
|
||||
app.NewConversation()
|
||||
app := model.NewAppModel(ctx, opts.InitialConversation)
|
||||
|
||||
m := Model{
|
||||
App: app,
|
||||
@@ -46,6 +39,7 @@ func initialModel(ctx *lmcli.Context, opts LaunchOptions) *Model {
|
||||
views: map[shared.View]shared.ViewModel{
|
||||
shared.ViewChat: chat.Chat(app),
|
||||
shared.ViewConversations: conversations.Conversations(app),
|
||||
shared.ViewSettings: settings.Settings(app),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -53,14 +47,15 @@ func initialModel(ctx *lmcli.Context, opts LaunchOptions) *Model {
|
||||
}
|
||||
|
||||
func (m *Model) Init() tea.Cmd {
|
||||
cmds := []tea.Cmd{
|
||||
func() tea.Msg {
|
||||
return shared.MsgViewChange(m.activeView)
|
||||
},
|
||||
}
|
||||
var cmds []tea.Cmd
|
||||
for _, v := range m.views {
|
||||
// Init views
|
||||
cmds = append(cmds, v.Init())
|
||||
}
|
||||
cmds = append(cmds, func() tea.Msg {
|
||||
// Initial view change
|
||||
return shared.MsgViewChange(m.activeView)
|
||||
})
|
||||
return tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
@@ -88,11 +83,11 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, cmd
|
||||
}
|
||||
case shared.MsgViewChange:
|
||||
currView := m.activeView
|
||||
m.activeView = shared.View(msg)
|
||||
return m, tea.Batch(tea.WindowSize(), shared.ViewEnter())
|
||||
return m, tea.Batch(tea.WindowSize(), shared.ViewEnter(currView))
|
||||
case shared.MsgError:
|
||||
m.errs = append(m.errs, msg.Err)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
view, cmd := m.views[m.activeView].Update(msg)
|
||||
@@ -134,6 +129,11 @@ func (m *Model) View() string {
|
||||
return lipgloss.JoinVertical(lipgloss.Left, sections...)
|
||||
}
|
||||
|
||||
type LaunchOptions struct {
|
||||
InitialConversation *api.Conversation
|
||||
InitialView shared.View
|
||||
}
|
||||
|
||||
type LaunchOption func(*LaunchOptions)
|
||||
|
||||
func WithInitialConversation(conv *api.Conversation) LaunchOption {
|
||||
|
||||
Reference in New Issue
Block a user