tui: renamed stateConversation -> stateChat

stateConversationList -> stateConversations
This commit is contained in:
Matt Low 2024-03-31 02:29:35 +00:00
parent 811ec4b251
commit 020db40401
3 changed files with 47 additions and 46 deletions

View File

@ -14,6 +14,20 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
type focusState int
const (
focusInput focusState = iota
focusMessages
)
type editorTarget int
const (
input editorTarget = iota
selectedMessage
)
// custom tea.Msg types // custom tea.Msg types
type ( type (
// sent on each chunk received from LLM // sent on each chunk received from LLM
@ -63,10 +77,10 @@ var (
footerStyle = lipgloss.NewStyle() footerStyle = lipgloss.NewStyle()
) )
func (m *model) handleConversationInput(msg tea.KeyMsg) tea.Cmd { func (m *model) handleChatInput(msg tea.KeyMsg) tea.Cmd {
switch msg.String() { switch msg.String() {
case "esc": case "esc":
m.state = stateConversationList m.state = stateConversations
return m.loadConversations() return m.loadConversations()
case "ctrl+p": case "ctrl+p":
m.persistence = !m.persistence m.persistence = !m.persistence
@ -81,15 +95,15 @@ func (m *model) handleConversationInput(msg tea.KeyMsg) tea.Cmd {
default: default:
switch m.focus { switch m.focus {
case focusInput: case focusInput:
return m.handleInputKey(msg) return m.handleChatInputKey(msg)
case focusMessages: case focusMessages:
return m.handleMessagesKey(msg) return m.handleChatMessagesKey(msg)
} }
} }
return nil return nil
} }
func (m *model) handleConversationUpdate(msg tea.Msg) []tea.Cmd { func (m *model) handleChatUpdate(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd var cmds []tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case msgTempfileEditorClosed: case msgTempfileEditorClosed:
@ -248,7 +262,7 @@ func (m *model) handleConversationUpdate(msg tea.Msg) []tea.Cmd {
return cmds return cmds
} }
func (m *model) handleMessagesKey(msg tea.KeyMsg) tea.Cmd { func (m *model) handleChatMessagesKey(msg tea.KeyMsg) tea.Cmd {
switch msg.String() { switch msg.String() {
case "tab": case "tab":
m.focus = focusInput m.focus = focusInput
@ -287,7 +301,7 @@ func (m *model) handleMessagesKey(msg tea.KeyMsg) tea.Cmd {
return nil return nil
} }
func (m *model) handleInputKey(msg tea.KeyMsg) tea.Cmd { func (m *model) handleChatInputKey(msg tea.KeyMsg) tea.Cmd {
switch msg.String() { switch msg.String() {
case "esc": case "esc":
m.focus = focusMessages m.focus = focusMessages

View File

@ -17,10 +17,10 @@ type (
msgConversationsLoaded []models.Conversation msgConversationsLoaded []models.Conversation
) )
func (m *model) handleConversationListInput(msg tea.KeyMsg) tea.Cmd { func (m *model) handleConversationsInput(msg tea.KeyMsg) tea.Cmd {
switch msg.String() { switch msg.String() {
case "enter": case "enter":
m.state = stateConversation m.state = stateChat
m.updateContent() m.updateContent()
// load selected conversation and switch state // load selected conversation and switch state
case "n": case "n":
@ -37,12 +37,12 @@ func (m *model) handleConversationListInput(msg tea.KeyMsg) tea.Cmd {
return nil return nil
} }
func (m *model) handleConversationListUpdate(msg tea.Msg) []tea.Cmd { func (m *model) handleConversationsUpdate(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd var cmds []tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case msgConversationsLoaded: case msgConversationsLoaded:
m.conversations = msg m.conversations = msg
m.content.SetContent(m.conversationListView()) m.content.SetContent(m.renderConversationList())
} }
var cmd tea.Cmd var cmd tea.Cmd
@ -73,7 +73,7 @@ func (m *model) loadConversations() tea.Cmd {
} }
} }
func (m *model) loadConversationLastReplies(conversations []models.Conversation) tea.Cmd { func (m *model) loadConversationLastestReplies(conversations []models.Conversation) tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
//lastMessage, err := m.ctx.Store.LastMessage(&conversation) //lastMessage, err := m.ctx.Store.LastMessage(&conversation)
return nil return nil
@ -83,7 +83,7 @@ func (m *model) loadConversationLastReplies(conversations []models.Conversation)
func (m *model) setConversations(conversations []models.Conversation) { func (m *model) setConversations(conversations []models.Conversation) {
} }
func (m *model) conversationListView() string { func (m *model) renderConversationList() string {
sb := &strings.Builder{} sb := &strings.Builder{}
type AgeGroup struct { type AgeGroup struct {
name string name string

View File

@ -24,28 +24,15 @@ import (
type appState int type appState int
const ( const (
stateConversation = iota stateChat = iota
stateConversationList stateConversations
//stateModelSelect // stateOptions? //stateModelSelect // stateOptions?
//stateHelp //stateHelp
) )
type focusState int // this struct holds the final rendered content of various UI components, and
// gets populated in the application's Update() method. View() simply composes
const ( // these elements into the final view
focusInput focusState = iota
focusMessages
)
type editorTarget int
const (
input editorTarget = iota
selectedMessage
)
// we populate these fields as part of Update(), and let View() be
// responsible for returning the final composition of elements
type views struct { type views struct {
header string header string
content string content string
@ -113,7 +100,7 @@ func initialModel(ctx *lmcli.Context, convShortname string) model {
views: &views{}, views: &views{},
} }
m.state = stateConversation m.state = stateChat
m.content = viewport.New(0, 0) m.content = viewport.New(0, 0)
@ -155,11 +142,11 @@ func (m model) Init() tea.Cmd {
m.waitForReply(), m.waitForReply(),
} }
switch m.state { switch m.state {
case stateConversation: case stateChat:
if m.convShortname != "" { if m.convShortname != "" {
cmds = append(cmds, m.loadConversation(m.convShortname)) cmds = append(cmds, m.loadConversation(m.convShortname))
} }
case stateConversationList: case stateConversations:
cmds = append(cmds, m.loadConversations()) cmds = append(cmds, m.loadConversations())
} }
return tea.Batch(cmds...) return tea.Batch(cmds...)
@ -180,10 +167,10 @@ func (m *model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
} }
default: default:
switch m.state { switch m.state {
case stateConversation: case stateChat:
return m.handleConversationInput(msg) return m.handleChatInput(msg)
case stateConversationList: case stateConversations:
return m.handleConversationListInput(msg) return m.handleConversationsInput(msg)
} }
} }
return nil return nil
@ -210,10 +197,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
switch m.state { switch m.state {
case stateConversationList: case stateConversations:
cmds = append(cmds, m.handleConversationListUpdate(msg)...) cmds = append(cmds, m.handleConversationsUpdate(msg)...)
case stateConversation: case stateChat:
cmds = append(cmds, m.handleConversationUpdate(msg)...) cmds = append(cmds, m.handleChatUpdate(msg)...)
} }
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
@ -233,12 +220,12 @@ func (m model) View() string {
} }
switch m.state { switch m.state {
case stateConversationList: case stateConversations:
sections = append(sections, m.views.content) sections = append(sections, m.views.content)
if m.views.error != "" { if m.views.error != "" {
sections = append(sections, m.views.error) sections = append(sections, m.views.error)
} }
case stateConversation: case stateChat:
sections = append(sections, m.views.content) sections = append(sections, m.views.content)
if m.views.error != "" { if m.views.error != "" {
sections = append(sections, m.views.error) sections = append(sections, m.views.error)
@ -257,7 +244,7 @@ func (m *model) headerView() string {
titleStyle := lipgloss.NewStyle().Bold(true) titleStyle := lipgloss.NewStyle().Bold(true)
var header string var header string
switch m.state { switch m.state {
case stateConversation: case stateChat:
var title string var title string
if m.conversation != nil && m.conversation.Title != "" { if m.conversation != nil && m.conversation.Title != "" {
title = m.conversation.Title title = m.conversation.Title
@ -266,7 +253,7 @@ func (m *model) headerView() string {
} }
title = truncateToCellWidth(title, m.width-headerStyle.GetHorizontalPadding(), "...") title = truncateToCellWidth(title, m.width-headerStyle.GetHorizontalPadding(), "...")
header = titleStyle.Render(title) header = titleStyle.Render(title)
case stateConversationList: case stateConversations:
header = titleStyle.Render("Conversations") header = titleStyle.Render("Conversations")
} }
return headerStyle.Width(m.width).Render(header) return headerStyle.Width(m.width).Render(header)