Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Feb 5, 2025
1 parent c6e52ba commit 76dfe3a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/filterable_viewport/filterable_viewport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TestItem struct {

func (t TestItem) Render() linebuffer.LineBufferer {
lb := linebuffer.New(t.content)
return &lb
return lb
}

func (t TestItem) String() string {
Expand Down
41 changes: 29 additions & 12 deletions internal/viewport/linebuffer/multilinebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ func (m *MultiLineBuffer) SeekToWidth(width int) {
return
}

// reset all buffers before seeking
for _, buf := range m.buffers {
buf.SeekToWidth(0)
}

// find which buffer contains the target width
remainingWidth := width
m.currentBufferIdx = 0

for i, buf := range m.buffers {
bufWidth := buf.Width()
if remainingWidth <= bufWidth {
// found the buffer containing our target
m.currentBufferIdx = i
m.buffers[i].SeekToWidth(remainingWidth)
buf.SeekToWidth(remainingWidth)
return
}
remainingWidth -= bufWidth
m.currentBufferIdx = i + 1
}

// if we get here, we're seeking past the end
Expand All @@ -95,17 +102,27 @@ func (m *MultiLineBuffer) PopLeft(width int, continuation, toHighlight string, h
return ""
}

// get content from current buffer
result := m.buffers[m.currentBufferIdx].PopLeft(width, continuation, toHighlight, highlightStyle)
var result string
remainingWidth := width

for remainingWidth > 0 && m.currentBufferIdx < len(m.buffers) {
currentBuffer := m.buffers[m.currentBufferIdx]
chunk := currentBuffer.PopLeft(remainingWidth, continuation, toHighlight, highlightStyle)

if chunk == "" {
if m.currentBufferIdx < len(m.buffers)-1 {
m.currentBufferIdx++
continue
}
break
}

// if we got less than requested width and have more buffers, move to next buffer
if resultWidth := lipgloss.Width(result); resultWidth < width && m.currentBufferIdx < len(m.buffers)-1 {
m.currentBufferIdx++
m.buffers[m.currentBufferIdx].SeekToWidth(0)
// get remaining content from next buffer
remainingWidth := width - resultWidth
nextResult := m.buffers[m.currentBufferIdx].PopLeft(remainingWidth, continuation, toHighlight, highlightStyle)
result += nextResult
result += chunk
remainingWidth -= lipgloss.Width(chunk)

if remainingWidth > 0 && m.currentBufferIdx < len(m.buffers)-1 {
m.currentBufferIdx++
}
}

return result
Expand Down
27 changes: 25 additions & 2 deletions internal/viewport/linebuffer/multlinebuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

var equivalentLineBuffers = map[string][]LineBufferer{
// TODO LEO: add ansi, unicode
"hello world": {
New("hello world"),
NewMulti(
Expand Down Expand Up @@ -71,6 +72,30 @@ func TestMultiLineBuffer_SeekToWidth(t *testing.T) {
continuation: "",
expectedPopLeft: "hello w",
},
{
name: "hello world 1",
key: "hello world",
seekToWidth: 1,
takeWidth: 7,
continuation: "",
expectedPopLeft: "ello wo",
},
{
name: "hello world end",
key: "hello world",
seekToWidth: 10,
takeWidth: 3,
continuation: "",
expectedPopLeft: "d",
},
{
name: "hello world past end",
key: "hello world",
seekToWidth: 11,
takeWidth: 3,
continuation: "",
expectedPopLeft: "",
},
}

for _, tt := range tests {
Expand All @@ -84,5 +109,3 @@ func TestMultiLineBuffer_SeekToWidth(t *testing.T) {
})
}
}

//func TestMultiLin

0 comments on commit 76dfe3a

Please sign in to comment.