using strings.SplitAfter in wrapLines

This commit is contained in:
asdf 2019-09-07 23:04:20 +10:00
parent a9d5651e3c
commit 3e8587f039
1 changed files with 6 additions and 5 deletions

View File

@ -85,23 +85,23 @@ func Clear(dir string) {
// than the specified console width, splitting them over two lines. returns the // than the specified console width, splitting them over two lines. returns the
// amended document content as a slice. // amended document content as a slice.
func wrapLines(s []string, consolewidth int) []string { func wrapLines(s []string, consolewidth int) []string {
indent := " " //11 spaces //indent := " " //11 spaces
out := []string{} out := []string{}
for _, ln := range s { for _, ln := range s {
if len(ln) <= consolewidth { if len(ln) <= consolewidth {
out = append(out, ln) out = append(out, ln)
} else { } else {
words := strings.Split(ln, " ") words := strings.SplitAfter(ln, " ")
var subout bytes.Buffer var subout bytes.Buffer
for i, wd := range words { for i, wd := range words {
sublen := subout.Len() sublen := subout.Len()
if sublen+len(wd)+1 <= consolewidth { if sublen+len(wd)+1 <= consolewidth {
//if line was indented, reinsert indent //if line was indented, reinsert indent
if i == 11 && sublen == 0 { if i == 11 && sublen == 0 {
subout.WriteString(indent) //subout.WriteString(indent)
} }
if sublen > 0 { if sublen > 0 {
subout.WriteString(" ") //subout.WriteString(" ")
} }
subout.WriteString(wd) subout.WriteString(wd)
if i == len(words)-1 { if i == len(words)-1 {
@ -110,7 +110,8 @@ func wrapLines(s []string, consolewidth int) []string {
} else { } else {
out = append(out, subout.String()) out = append(out, subout.String())
subout.Reset() subout.Reset()
subout.WriteString(indent + wd) //subout.WriteString(indent)
subout.WriteString(wd)
if i == len(words)-1 { if i == len(words)-1 {
out = append(out, subout.String()) out = append(out, subout.String())
subout.Reset() subout.Reset()