Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions stringwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ type WrappedStringSeq struct {

// lastWrappedLine pulls the last wrapped line that has been parsed
func (s *WrappedStringSeq) lastWrappedLine() *WrappedString {
return &s.WrappedLines[len(s.WrappedLines)-1]
n := len(s.WrappedLines)
if n == 0 {
return nil
}
return &s.WrappedLines[n-1]
}

// appendWrappedSeq adds a new WrappedString to the existing slice
Expand Down Expand Up @@ -489,7 +493,7 @@ func stringWrap(
// remove the last new line from the wrapped buffer
// if the last line is not a hard break.
lastWrappedLine := wrappedStringSeq.lastWrappedLine()
if !lastWrappedLine.IsHardBreak {
if lastWrappedLine != nil && !lastWrappedLine.IsHardBreak {
stateMachine.buffer.Truncate(stateMachine.buffer.Len() - 1)
lastWrappedLine.LastSegmentInOrig = true
}
Expand Down
13 changes: 12 additions & 1 deletion stringwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,24 @@ func TestStringWrap(t *testing.T) {
trimWhitespace: true,
splitWord: true,
},
{
input: "",
wrapped: "",
limit: 5,
trimWhitespace: true,
splitWord: true,
},
}

for idx, tt := range tests {
t.Run(fmt.Sprintf("Wrapped String Test %d", idx+1), func(t *testing.T) {
wrapped, seq, err := wrapString(tt)
expectedLines := 0
if wrapped != "" {
expectedLines = len(strings.Split(wrapped, "\n"))
}
assert.Nil(t, err)
assert.Equal(t, len(seq.WrappedLines), len(strings.Split(wrapped, "\n")))
assert.Equal(t, expectedLines, len(seq.WrappedLines))
assert.Equal(t, tt.wrapped, wrapped)
})
}
Expand Down