diff options
author | Michael Muré <batolettre@gmail.com> | 2018-12-23 20:13:14 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-12-23 20:13:14 +0100 |
commit | 261aa61711639cf98a9aa987e86377d520b8c868 (patch) | |
tree | 7466fb4dd6cb3a64e4a3d9819859e488ffb8066e | |
parent | f9fc85ac825fec05a26d4029da15b33ae66e4e25 (diff) | |
download | git-bug-261aa61711639cf98a9aa987e86377d520b8c868.tar.gz |
text: fix a wrapping bug leading to line longer than they should
-rw-r--r-- | termui/show_bug.go | 4 | ||||
-rw-r--r-- | util/text/text.go | 2 | ||||
-rw-r--r-- | util/text/text_test.go | 34 |
3 files changed, 36 insertions, 4 deletions
diff --git a/termui/show_bug.go b/termui/show_bug.go index a16626a4..cb2fe04c 100644 --- a/termui/show_bug.go +++ b/termui/show_bug.go @@ -241,7 +241,7 @@ func (sb *showBug) renderMain(g *gocui.Gui, mainView *gocui.View) error { case *bug.CreateTimelineItem: create := op.(*bug.CreateTimelineItem) - content, lines := text.WrapLeftPadded(create.Message, maxX, 4) + content, lines := text.WrapLeftPadded(create.Message, maxX-1, 4) v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true) if err != nil { @@ -258,7 +258,7 @@ func (sb *showBug) renderMain(g *gocui.Gui, mainView *gocui.View) error { edited = " (edited)" } - message, _ := text.WrapLeftPadded(comment.Message, maxX, 4) + message, _ := text.WrapLeftPadded(comment.Message, maxX-1, 4) content := fmt.Sprintf("%s commented on %s%s\n\n%s", colors.Magenta(comment.Author.DisplayName()), comment.CreatedAt.Time().Format(timeLayout), diff --git a/util/text/text.go b/util/text/text.go index 443728aa..b41b892b 100644 --- a/util/text/text.go +++ b/util/text/text.go @@ -84,7 +84,7 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) { lineBuffer.Reset() lineBuffer.WriteString(word) firstWord = false - spaceLeft = lineWidth - wordLength + spaceLeft = lineWidth - leftPad - wordLength nbLine++ } } diff --git a/util/text/text_test.go b/util/text/text_test.go index cda26fda..f37b4ce6 100644 --- a/util/text/text_test.go +++ b/util/text/text_test.go @@ -106,7 +106,7 @@ func TestWrap(t *testing.T) { for i, tc := range cases { actual, lines := Wrap(tc.Input, tc.Lim) if actual != tc.Output { - t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`", + t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n`\n%s`", i, tc.Input, tc.Output, actual) } @@ -118,6 +118,38 @@ func TestWrap(t *testing.T) { } } +func TestWrapLeftPadded(t *testing.T) { + cases := []struct { + input, output string + lim, pad int + }{ + { + "The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.", + ` The Lorem ipsum text is typically composed of + pseudo-Latin words. It is commonly used as placeholder + text to examine or demonstrate the visual effects of + various graphic design.`, + 59, 4, + }, + } + + for i, tc := range cases { + actual, lines := WrapLeftPadded(tc.input, tc.lim, tc.pad) + if actual != tc.output { + t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`", + i, tc.input, tc.output, + "|"+strings.Repeat("-", tc.lim-2)+"|", + actual) + } + + expected := len(strings.Split(tc.output, "\n")) + if expected != lines { + t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d", + i, expected, lines) + } + } +} + func TestWordLen(t *testing.T) { cases := []struct { Input string |