simple line wrapping that does not mangle spaces

This commit is contained in:
asdf 2019-09-10 20:19:45 +10:00
parent 3e8587f039
commit ea096af511
2 changed files with 15 additions and 85 deletions

View File

@ -84,8 +84,8 @@ func Clear(dir string) {
// takes the document content (as a slice) and modifies any lines that are longer
// than the specified console width, splitting them over two lines. returns the
// amended document content as a slice.
// word wrapping uses a "greedy" algorithm
func wrapLines(s []string, consolewidth int) []string {
//indent := " " //11 spaces
out := []string{}
for _, ln := range s {
if len(ln) <= consolewidth {
@ -95,14 +95,8 @@ func wrapLines(s []string, consolewidth int) []string {
var subout bytes.Buffer
for i, wd := range words {
sublen := subout.Len()
if sublen+len(wd)+1 <= consolewidth {
//if line was indented, reinsert indent
if i == 11 && sublen == 0 {
//subout.WriteString(indent)
}
if sublen > 0 {
//subout.WriteString(" ")
}
wdlen := len(wd)
if sublen+wdlen <= consolewidth {
subout.WriteString(wd)
if i == len(words)-1 {
out = append(out, subout.String())
@ -110,7 +104,6 @@ func wrapLines(s []string, consolewidth int) []string {
} else {
out = append(out, subout.String())
subout.Reset()
//subout.WriteString(indent)
subout.WriteString(wd)
if i == len(words)-1 {
out = append(out, subout.String())

View File

@ -5,92 +5,32 @@ import (
"testing"
)
func Test_wrapLines_doesnt_break_indents(t *testing.T) {
indent := " "
// tests related to issue 31
func Test_wrapLines_space_preservation(t *testing.T) {
tables := []struct {
testinput []string
expectedoutput []string
linelength int
}{
{
//indented long word - 20 characters - should not wrap
[]string{indent + "012345678"},
[]string{indent + "012345678"},
20,
},
{
//indented long word - 21 characters - should wrap
[]string{indent + "0123456789"},
[]string{indent + "012345678", indent + "9"},
20,
},
{
//indented really long word - should wrap
[]string{indent + "0123456789zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"},
[]string{
indent + "012345678",
indent + "9zzzzzzzz",
indent + "zzzzzzzzz",
indent + "zzzzzzzzz",
indent + "zzzzz"},
20,
}, {
//non-indented long word - 20 characters - should not wrap
[]string{"01234567890123456789"},
[]string{"01234567890123456789"},
20,
},
{
//non-indented long word - 21 characters - should wrap
[]string{"01234567890123456789a"},
[]string{"01234567890123456789", "a"},
20,
},
{
//non-indented really long word - should wrap
[]string{"01234567890123456789zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"},
[]string{
"01234567890123456789",
"zzzzzzzzzzzzzzzzzzzz",
"zzzzzzzzzzzzzzzzzzzz",
"zzzzzzzzzzzzzzzzzzzz",
"zzzz"},
20,
},
{
//indented normal sentence - 20 characters - should not wrap
[]string{indent + "it is her"},
[]string{indent + "it is her"},
20,
},
{
//indented normal sentence - more than 20 characters - should wrap
[]string{indent + "it is her favourite thing in the world"},
[]string{
indent + "it is her",
indent + "favourite",
indent + "thing in",
indent + "the world",
},
20,
},
{
//non-indented normal sentence - 20 characters - should not wrap
//normal sentence - 20 characters - should not wrap
[]string{"it is her fav thingy"},
[]string{"it is her fav thingy"},
20,
},
{
//non-indented normal sentence - more than 20 characters - should wrap
//normal sentence - more than 20 characters - should wrap with a space at the end of the first line
[]string{"it is her favourite thing in the world"},
[]string{
"it is her favourite",
"it is her favourite ",
"thing in the world",
},
20,
},
{
//a specific test from cat's phlog that was wrapping and I'm not sure why
// a specific test from cat's phlog (gopher://baud.baby:70/0/phlog/fs20190818.txt)
// I think the character U+2013 : EN DASH characteris causing wrapping incorrectly
// but the test passes and i'm not sure why
[]string{
" Suldusk Really cool dark folk/black metal sort of deal. The lead singer",
"is a tiny fairy of a person and she's very charming. It's the bass players",
@ -101,9 +41,6 @@ func Test_wrapLines_doesnt_break_indents(t *testing.T) {
},
80,
},
//TODO further tests
//lines that are just spaces don't get misidentified as indents and then mangled
}
for _, table := range tables {
@ -116,11 +53,11 @@ func Test_wrapLines_doesnt_break_indents(t *testing.T) {
}
func Benchmark_wrapLines(b *testing.B) {
indent := " "
teststring := []string{
indent + "0123456789\n",
indent + "a really long line that will prolly be wrapped\n",
indent + "a l i n e w i t h a l o t o f w o r d s\n",
"0123456789",
"a really long line that will prolly be wrapped",
"a l i n e w i t h a l o t o f w o r d s",
"onehugelongwordaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
}
b.ResetTimer()
for n := 0; n < b.N; n++ {