Private
Public Access
1
0

tui: Initial rough conversation list view

This commit is contained in:
2024-03-31 01:02:07 +00:00
parent cef87a55d8
commit c68cb14eb9
2 changed files with 227 additions and 20 deletions

View File

@@ -32,7 +32,7 @@ type appState int
const (
stateConversation = iota
//stateConversationList
stateConversationList
//stateModelSelect // stateOptions?
//stateHelp
)
@@ -60,6 +60,8 @@ type model struct {
// application state
state appState
conversations []models.Conversation
lastReplies []models.Message
conversation *models.Conversation
messages []models.Message
selectedMessage int
@@ -143,6 +145,8 @@ type (
msgConversationTitleChanged string
// sent when a conversation's messages are laoded
msgMessagesLoaded []models.Message
// send when conversation list is loaded
msgConversationsLoaded []models.Conversation
// sent when an error occurs
msgError error
)
@@ -154,13 +158,21 @@ func wrapError(err error) tea.Cmd {
}
func (m model) Init() tea.Cmd {
return tea.Batch(
cmds := []tea.Cmd{
textarea.Blink,
m.spinner.Tick,
m.loadConversation(m.convShortname),
m.waitForChunk(),
m.waitForReply(),
)
}
switch m.state {
case stateConversation:
if m.convShortname != "" {
cmds = append(cmds, m.loadConversation(m.convShortname))
}
case stateConversationList:
cmds = append(cmds, m.loadConversations())
}
return tea.Batch(cmds...)
}
func (m *model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
@@ -181,6 +193,8 @@ func (m *model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
switch m.state {
case stateConversation:
return m.handleConversationInput(msg)
case stateConversationList:
return m.handleConversationListInput(msg)
}
}
return nil
@@ -188,6 +202,9 @@ func (m *model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
func (m *model) handleConversationInput(msg tea.KeyMsg) tea.Cmd {
switch msg.String() {
case "esc":
m.state = stateConversationList
return m.loadConversations()
case "ctrl+p":
m.persistence = !m.persistence
case "ctrl+t":
@@ -209,10 +226,6 @@ func (m *model) handleConversationInput(msg tea.KeyMsg) tea.Cmd {
return nil
}
func (m *model) handleConversationListInput(msg tea.KeyMsg) tea.Cmd {
return nil
}
func (m *model) handleConversationUpdate(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd
switch msg := msg.(type) {
@@ -395,6 +408,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
switch m.state {
case stateConversationList:
cmds = append(cmds, m.handleConversationListUpdate(msg)...)
case stateConversation:
cmds = append(cmds, m.handleConversationUpdate(msg)...)
}
@@ -439,6 +454,11 @@ func (m model) View() string {
}
switch m.state {
case stateConversationList:
sections = append(sections, m.views.content)
if m.views.error != "" {
sections = append(sections, m.views.error)
}
case stateConversation:
sections = append(sections, m.views.content)
if m.views.error != "" {
@@ -451,23 +471,26 @@ func (m model) View() string {
sections = append(sections, m.views.footer)
}
return lipgloss.JoinVertical(
lipgloss.Left,
sections...,
)
return lipgloss.JoinVertical(lipgloss.Left, sections...)
}
func (m *model) headerView() string {
titleStyle := lipgloss.NewStyle().Bold(true)
var title string
if m.conversation != nil && m.conversation.Title != "" {
title = m.conversation.Title
} else {
title = "Untitled"
var header string
switch m.state {
case stateConversation:
var title string
if m.conversation != nil && m.conversation.Title != "" {
title = m.conversation.Title
} else {
title = "Untitled"
}
title = truncateToCellWidth(title, m.width-headerStyle.GetHorizontalPadding(), "...")
header = titleStyle.Render(title)
case stateConversationList:
header = titleStyle.Render("Conversations")
}
title = truncateToCellWidth(title, m.width-headerStyle.GetHorizontalPadding(), "...")
part := titleStyle.Render(title)
return headerStyle.Width(m.width).Render(part)
return headerStyle.Width(m.width).Render(header)
}
func (m *model) errorView() string {