From 38c9721817ee942c2768409276cc7008e41387cc Mon Sep 17 00:00:00 2001 From: asdf Date: Wed, 11 Sep 2019 15:36:56 +1000 Subject: [PATCH] this is a test only for wraplines rune tests --- cui/cui.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++-- cui/cui_test.go | 24 ++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/cui/cui.go b/cui/cui.go index 67d8b3a..f3950a1 100644 --- a/cui/cui.go +++ b/cui/cui.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "strings" + "unicode/utf8" ) var shapes = map[string]string{ @@ -88,14 +89,14 @@ func Clear(dir string) { func wrapLines(s []string, consolewidth int) []string { out := []string{} for _, ln := range s { - if len(ln) <= consolewidth { + if len([]rune(ln)) <= consolewidth { out = append(out, ln) } else { words := strings.SplitAfter(ln, " ") var subout bytes.Buffer for i, wd := range words { sublen := subout.Len() - wdlen := len(wd) + wdlen := len([]rune(wd)) if sublen+wdlen <= consolewidth { subout.WriteString(wd) if i == len(words)-1 { @@ -171,3 +172,65 @@ func HandleAlternateScreen(opt string) { // to run _ = cmd.Run() } + +func wrapLines2(s []string, consolewidth int) []string { + out := []string{} + for _, ln := range s { + if utf8.RuneCountInString(ln) <= consolewidth { + out = append(out, ln) + } else { + words := strings.SplitAfter(ln, " ") + var subout bytes.Buffer + for i, wd := range words { + sublen := subout.Len() + wdlen := utf8.RuneCountInString(wd) + if sublen+wdlen <= consolewidth { + subout.WriteString(wd) + if i == len(words)-1 { + out = append(out, subout.String()) + } + } else { + out = append(out, subout.String()) + subout.Reset() + subout.WriteString(wd) + if i == len(words)-1 { + out = append(out, subout.String()) + subout.Reset() + } + } + } + } + } + return out +} + +func wrapLines3(s []string, consolewidth int) []string { + out := []string{} + for _, ln := range s { + if len(ln) <= consolewidth { + out = append(out, ln) + } else { + words := strings.SplitAfter(ln, " ") + var subout bytes.Buffer + for i, wd := range words { + sublen := subout.Len() + wdlen := len(wd) + if sublen+wdlen <= consolewidth { + subout.WriteString(wd) + if i == len(words)-1 { + out = append(out, subout.String()) + } + } else { + out = append(out, subout.String()) + subout.Reset() + subout.WriteString(wd) + if i == len(words)-1 { + out = append(out, subout.String()) + subout.Reset() + } + } + } + } + } + return out +} diff --git a/cui/cui_test.go b/cui/cui_test.go index 374be25..f7897ae 100644 --- a/cui/cui_test.go +++ b/cui/cui_test.go @@ -81,3 +81,27 @@ func Benchmark_wrapLines(b *testing.B) { wrapLines(teststring, 20) } } +func Benchmark_wrapLines2(b *testing.B) { + teststring := []string{ + "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++ { + wrapLines2(teststring, 20) + } +} +func Benchmark_wrapLines3(b *testing.B) { + teststring := []string{ + "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++ { + wrapLines3(teststring, 20) + } +}