first attempt on line wrapping indents issue
This commit is contained in:
parent
309d6244e1
commit
fb2130518c
16
cui/cui.go
16
cui/cui.go
|
@ -81,17 +81,25 @@ func Clear(dir string) {
|
|||
|
||||
}
|
||||
|
||||
func wrapLines(s []string, length int) []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.
|
||||
func wrapLines(s []string, consolewidth int) []string {
|
||||
indent := " " //11 spaces
|
||||
out := []string{}
|
||||
for _, ln := range s {
|
||||
if len(ln) <= length {
|
||||
if len(ln) <= consolewidth {
|
||||
out = append(out, ln)
|
||||
} else {
|
||||
words := strings.Split(ln, " ")
|
||||
var subout bytes.Buffer
|
||||
for i, wd := range words {
|
||||
sublen := subout.Len()
|
||||
if sublen+len(wd)+1 <= length {
|
||||
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(" ")
|
||||
}
|
||||
|
@ -102,7 +110,7 @@ func wrapLines(s []string, length int) []string {
|
|||
} else {
|
||||
out = append(out, subout.String())
|
||||
subout.Reset()
|
||||
subout.WriteString(wd)
|
||||
subout.WriteString(indent + wd)
|
||||
if i == len(words)-1 {
|
||||
out = append(out, subout.String())
|
||||
subout.Reset()
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package cui
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_wrapLines_doesnt_break_indents(t *testing.T) {
|
||||
indent := " "
|
||||
tables := []struct {
|
||||
testinput []string
|
||||
expectedoutput []string
|
||||
linelength int
|
||||
}{
|
||||
{
|
||||
//20 character input - should not wrap
|
||||
[]string{indent + "012345678"},
|
||||
[]string{indent + "012345678"},
|
||||
20,
|
||||
},
|
||||
{
|
||||
//21 character input - should wrap
|
||||
[]string{indent + "0123456789"},
|
||||
[]string{indent + "0123456789"},
|
||||
20,
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
output := wrapLines(table.testinput, table.linelength)
|
||||
|
||||
if !reflect.DeepEqual(output, table.expectedoutput) {
|
||||
t.Errorf("Expected %v, got %v", table.expectedoutput, output)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue