1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-25 13:48:50 +08:00

The text widget now wraps at words.

This commit is contained in:
Jakub Sobon 2019-03-02 17:56:37 -05:00
parent c43e453038
commit 2742487fc0
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
6 changed files with 57 additions and 3 deletions

View File

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- The Text widget now supports content wrapping on word boundaries.
- Refactoring packages that contained a mix of public and internal identifiers.
#### Breaking API changes

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 MiB

After

Width:  |  Height:  |  Size: 10 MiB

View File

@ -84,6 +84,15 @@ func (o option) set(opts *options) {
o(opts)
}
// WrapAtWords configures the text widget so that it automatically wraps lines
// that are longer than the width of the widget at word boundaries. If not
// provided, long lines are trimmed instead.
func WrapAtWords() Option {
return option(func(opts *options) {
opts.wrapMode = wrap.AtWords
})
}
// WrapAtRunes configures the text widget so that it automatically wraps lines
// that are longer than the width of the widget at rune boundaries. If not
// provided, long lines are trimmed instead.

View File

@ -211,7 +211,7 @@ func (t *Text) Draw(cvs *canvas.Canvas) error {
defer t.mu.Unlock()
width := cvs.Area().Dx()
if t.contentChanged || t.lastWidth != width {
if len(t.content) > 0 && (t.contentChanged || t.lastWidth != width) {
// The previous text preprocessing (line wrapping) is invalidated when
// new text is added or the width of the canvas changed.
wr, err := wrap.Cells(t.content, width, t.opts.wrapMode)

View File

@ -487,6 +487,50 @@ func TestTextDraws(t *testing.T) {
return ft
},
},
{
desc: "wraps lines at word boundaries",
canvas: image.Rect(0, 0, 10, 6),
opts: []Option{
WrapAtWords(),
},
writes: func(widget *Text) error {
return widget.Write("hello wor你\nhello wor你d\nand long 世")
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
testdraw.MustText(c, "hello", image.Point{0, 0})
testdraw.MustText(c, "wor你", image.Point{0, 1})
testdraw.MustText(c, "hello", image.Point{0, 2})
testdraw.MustText(c, "wor你d", image.Point{0, 3})
testdraw.MustText(c, "and long", image.Point{0, 4})
testdraw.MustText(c, "世", image.Point{0, 5})
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "wraps lines at word boundaries, inserts dash for long words",
canvas: image.Rect(0, 0, 10, 6),
opts: []Option{
WrapAtWords(),
},
writes: func(widget *Text) error {
return widget.Write("hello thisisalongword world")
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
testdraw.MustText(c, "hello", image.Point{0, 0})
testdraw.MustText(c, "thisisalo-", image.Point{0, 1})
testdraw.MustText(c, "ngword", image.Point{0, 2})
testdraw.MustText(c, "world", image.Point{0, 3})
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "rolls content upwards and trims lines",
canvas: image.Rect(0, 0, 10, 2),

View File

@ -112,7 +112,7 @@ func main() {
panic(err)
}
rolled, err := text.New(text.RollContent(), text.WrapAtRunes())
rolled, err := text.New(text.RollContent(), text.WrapAtWords())
if err != nil {
panic(err)
}
@ -158,7 +158,7 @@ func main() {
),
container.Right(
container.Border(linestyle.Light),
container.BorderTitle("Rolls and scrolls content"),
container.BorderTitle("Rolls and scrolls content wrapped at words"),
container.PlaceWidget(rolled),
),
),