Skip to content

Commit

Permalink
fix(chat.go): fix viewport width calculation to correctly wrap markdo…
Browse files Browse the repository at this point in the history
…wn renderer

fix(chat.go): handle nil chat message when updating user prompt or AI response to prevent errors
  • Loading branch information
MohammadBnei committed Feb 15, 2024
1 parent a86098f commit c622954
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ui/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (m chatModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Width = m.size.Width - w
m.viewport.Height = m.size.Height - lipgloss.Height(m.GetTitleView()) - m.textarea.Height() - lipgloss.Height(m.help.View(m.keys)) - h

m.mdRenderer, _ = glamour.NewTermRenderer(glamour.WithAutoStyle(), glamour.WithWordWrap(m.size.Width-w))
m.mdRenderer, _ = glamour.NewTermRenderer(glamour.WithAutoStyle(), glamour.WithWordWrap(m.viewport.Width-2))

case tea.KeyMsg:
if m.err != nil {
Expand All @@ -200,9 +200,17 @@ func (m chatModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if len(m.stack) == 0 {
switch {
case m.currentChatIndices.user != -1:
m.userPrompt = m.promptConfig.ChatMessages.FindById(m.currentChatIndices.user).Content
if c := m.promptConfig.ChatMessages.FindById(m.currentChatIndices.user); c != nil {
m.userPrompt = c.Content
} else {
m.currentChatIndices.user = -1
}
case m.currentChatIndices.assistant != -1:
m.aiResponse = m.promptConfig.ChatMessages.FindById(m.currentChatIndices.assistant).Content
if c := m.promptConfig.ChatMessages.FindById(m.currentChatIndices.assistant); c != nil {
m.aiResponse = c.Content
} else {
m.currentChatIndices.assistant = -1
}
}
}
cmds = append(cmds, tea.Sequence(event.Transition("clear"), event.UpdateChatContent("", ""), event.Transition("")))
Expand Down

0 comments on commit c622954

Please sign in to comment.