-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SoftWrap to handle Lines and ShowLineNumbers behavior
This commit introduces a `SoftWrap` option that adjusts the behavior of the `Lines` option and `ShowLineNumbers` when used with the `Wrap` option. - `Lines`: Currently, when both `Lines` and `Wrap` options are used, the line count is computed after wrapping occurs. This can lead to discrepancies where wrapped lines count as multiple lines, causing the final output to exclude some original input lines. With the new `SoftWrap` option, wrapped lines will count as a single line, ensuring that all lines specified in the `Lines` option are correctly included. - `ShowLineNumbers`: When `SoftWrap` is enabled, line numbers are not added to wrapped lines. This preserves the original line numbering, ensuring consistency with the input file.
- Loading branch information
Showing
4 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/muesli/reflow/wordwrap" | ||
"strings" | ||
) | ||
|
||
func SoftWrap(input string, wrapLength int) []bool { | ||
var wrap []bool | ||
for _, line := range strings.Split(input, "\n") { | ||
wrappedLine := wordwrap.String(line, wrapLength) | ||
|
||
for i := range strings.Split(wrappedLine, "\n") { | ||
if i == 0 { | ||
// We want line number on the original line | ||
wrap = append(wrap, true) | ||
} else { | ||
// for wrapped line, we do not want line number | ||
wrap = append(wrap, false) | ||
} | ||
} | ||
} | ||
return wrap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// Mock the dependency if needed, assuming wordwrap.String works correctly. | ||
func TestSoftWrap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
wrapLength int | ||
expected []bool | ||
}{ | ||
{ | ||
name: "Single short line, no wrapping", | ||
input: "Hello", | ||
wrapLength: 10, | ||
expected: []bool{true}, | ||
}, | ||
{ | ||
name: "Single long line, wrapping", | ||
input: "Hello World, this is a long line", | ||
wrapLength: 10, | ||
expected: []bool{true, false, false, false}, | ||
}, | ||
{ | ||
name: "Multiple lines, some wrapped", | ||
input: "Short\nThis is a long line", | ||
wrapLength: 10, | ||
expected: []bool{true, true, false}, | ||
}, | ||
{ | ||
name: "Multiple lines, multiple wraps", | ||
input: "This is an long line\nThis is an other long line\nThis is the last long line\nShort line", | ||
wrapLength: 10, | ||
expected: []bool{true, false, true, false, false, true, false, false, true}, | ||
}, | ||
{ | ||
name: "Empty input", | ||
input: "", | ||
wrapLength: 10, | ||
expected: []bool{true}, | ||
}, | ||
{ | ||
name: "Lines with spaces only", | ||
input: " \n ", | ||
wrapLength: 5, | ||
expected: []bool{true, true}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := SoftWrap(tt.input, tt.wrapLength) | ||
if !reflect.DeepEqual(got, tt.expected) { | ||
t.Errorf("SoftWrap() = %v, expected %v", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |