released the mock
This commit is contained in:
+31
-21
@@ -117,6 +117,27 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
m.height = msg.Height
|
||||
|
||||
headerHeight := 1
|
||||
inputHeight := 5 // Textarea height(3) + border/padding
|
||||
|
||||
vpWidth := msg.Width - 4 // Account for viewportStyle padding and border
|
||||
vpHeight := msg.Height - headerHeight - inputHeight - 2
|
||||
|
||||
if !m.ready {
|
||||
m.viewport = viewport.New(vpWidth, vpHeight)
|
||||
m.viewport.SetContent(strings.Join(m.messages, "\n\n"))
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = vpWidth
|
||||
m.viewport.Height = vpHeight
|
||||
}
|
||||
|
||||
m.textarea.SetWidth(msg.Width - 4)
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.Type {
|
||||
case tea.KeyCtrlC, tea.KeyEsc:
|
||||
@@ -129,40 +150,29 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if strings.TrimSpace(v) == "" || m.isThinking {
|
||||
return m, nil
|
||||
}
|
||||
m.messages = append(m.messages, userStyle.Render("You")+"\n"+v)
|
||||
|
||||
wrappedUser := lipgloss.NewStyle().Width(m.viewport.Width).Render(userStyle.Render("You") + "\n" + v)
|
||||
m.messages = append(m.messages, wrappedUser)
|
||||
m.textarea.Reset()
|
||||
m.viewport.SetContent(strings.Join(m.messages, "\n\n"))
|
||||
m.viewport.GotoBottom()
|
||||
m.isThinking = true
|
||||
return m, m.runAgent(v)
|
||||
}
|
||||
|
||||
case agentResponseMsg:
|
||||
m.isThinking = false
|
||||
var newMsg string
|
||||
if msg.err != nil {
|
||||
m.messages = append(m.messages, sysStyle.Render(fmt.Sprintf("Error: %v", msg.err)))
|
||||
newMsg = sysStyle.Render(fmt.Sprintf("Error: %v", msg.err))
|
||||
} else {
|
||||
m.messages = append(m.messages, agentStyle.Render("Sidekick")+"\n"+msg.response)
|
||||
newMsg = agentStyle.Render("Sidekick") + "\n" + msg.response
|
||||
}
|
||||
wrappedMsg := lipgloss.NewStyle().Width(m.viewport.Width).Render(newMsg)
|
||||
m.messages = append(m.messages, wrappedMsg)
|
||||
m.viewport.SetContent(strings.Join(m.messages, "\n\n"))
|
||||
m.viewport.GotoBottom()
|
||||
return m, nil
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
m.height = msg.Height
|
||||
|
||||
headerHeight := 1
|
||||
inputHeight := 5 // Textarea height(3) + border/padding
|
||||
|
||||
if !m.ready {
|
||||
m.viewport = viewport.New(msg.Width-2, msg.Height-headerHeight-inputHeight-2)
|
||||
m.viewport.SetContent(strings.Join(m.messages, "\n\n"))
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width - 2
|
||||
m.viewport.Height = msg.Height - headerHeight - inputHeight - 2
|
||||
}
|
||||
|
||||
m.textarea.SetWidth(msg.Width - 4)
|
||||
}
|
||||
|
||||
m.textarea, tiCmd = m.textarea.Update(msg)
|
||||
@@ -192,7 +202,7 @@ func (m model) View() string {
|
||||
statusStyle.Render(" "+status+" "),
|
||||
)
|
||||
|
||||
vpView := viewportStyle.Width(m.width - 2).Height(m.height - 10).Render(m.viewport.View())
|
||||
vpView := viewportStyle.Width(m.width - 2).Render(m.viewport.View())
|
||||
|
||||
taStyle := textareaStyle
|
||||
if m.textarea.Focused() {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
@@ -54,3 +56,18 @@ func TestModelUpdate(t *testing.T) {
|
||||
t.Error("expected message list to grow after response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageWrapping(t *testing.T) {
|
||||
m := initialModel()
|
||||
m.ready = true
|
||||
m.viewport = viewport.New(10, 5) // Very narrow
|
||||
|
||||
longMsg := "This is a very long message that should be wrapped."
|
||||
respModel, _ := m.Update(agentResponseMsg{response: longMsg})
|
||||
rm := respModel.(model)
|
||||
|
||||
lastMsg := rm.messages[len(rm.messages)-1]
|
||||
if !strings.Contains(lastMsg, "\n") {
|
||||
t.Errorf("expected long message to be wrapped, but no newline found")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user