lmcli/pkg/tui/tui.go

126 lines
2.6 KiB
Go
Raw Normal View History

2024-03-12 01:10:54 -06:00
package tui
import (
"fmt"
"git.mlow.ca/mlow/lmcli/pkg/api"
2024-03-12 01:10:54 -06:00
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
"git.mlow.ca/mlow/lmcli/pkg/tui/model"
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
"git.mlow.ca/mlow/lmcli/pkg/tui/views/chat"
"git.mlow.ca/mlow/lmcli/pkg/tui/views/conversations"
2024-03-12 01:10:54 -06:00
tea "github.com/charmbracelet/bubbletea"
)
type LaunchOptions struct {
InitialConversation *api.Conversation
InitialView shared.View
}
type Model struct {
App *model.AppModel
activeView shared.View
views map[shared.View]shared.ViewModel
}
2024-03-12 01:10:54 -06:00
func initialModel(ctx *lmcli.Context, opts LaunchOptions) Model {
sharedData := shared.ViewState{}
app := &model.AppModel{
Ctx: ctx,
Conversation: opts.InitialConversation,
}
m := Model{
App: app,
activeView: opts.InitialView,
views: map[shared.View]shared.ViewModel{
shared.ViewChat: chat.Chat(app, sharedData),
shared.ViewConversations: conversations.Conversations(app, sharedData),
},
}
return m
2024-03-29 14:43:19 -06:00
}
2024-03-12 01:10:54 -06:00
func (m Model) Init() tea.Cmd {
return tea.Batch(
func() tea.Msg {
return shared.MsgViewChange(m.activeView)
},
)
2024-03-12 01:10:54 -06:00
}
func (m *Model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
view, cmd := m.views[m.activeView].Update(msg)
m.views[m.activeView] = view
if cmd != nil {
return cmd
}
2024-03-31 19:06:13 -06:00
switch msg.String() {
case "ctrl+c", "ctrl+q":
return tea.Quit
2024-03-31 19:06:13 -06:00
}
return nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
cmd := m.handleGlobalInput(msg)
if cmd != nil {
return m, cmd
}
case shared.MsgViewChange:
m.activeView = shared.View(msg)
view := m.views[m.activeView]
var cmds []tea.Cmd
if !view.Initialized() {
cmds = append(cmds, view.Init())
}
cmds = append(cmds, tea.WindowSize(), shared.ViewEnter())
return m, tea.Batch(cmds...)
}
view, cmd := m.views[m.activeView].Update(msg)
m.views[m.activeView] = view
return m, cmd
2024-03-12 01:10:54 -06:00
}
func (m Model) View() string {
return m.views[m.activeView].View()
2024-03-12 01:10:54 -06:00
}
type LaunchOption func(*LaunchOptions)
func WithInitialConversation(conv *api.Conversation) LaunchOption {
return func(opts *LaunchOptions) {
opts.InitialConversation = conv
}
}
func WithInitialView(view shared.View) LaunchOption {
return func(opts *LaunchOptions) {
opts.InitialView = view
}
}
func Launch(ctx *lmcli.Context, options ...LaunchOption) error {
opts := &LaunchOptions{
InitialView: shared.ViewChat,
}
for _, opt := range options {
opt(opts)
}
program := tea.NewProgram(initialModel(ctx, *opts), tea.WithAltScreen())
if _, err := program.Run(); err != nil {
2024-03-12 01:10:54 -06:00
return fmt.Errorf("Error running program: %v", err)
}
return nil
}