mirror of https://github.com/mum4k/termdash.git
Adding Text Write option that atomically replaces the text content.
This commit is contained in:
parent
d4b68e905b
commit
38a2a36234
|
@ -85,7 +85,11 @@ func New(opts ...Option) (*Text, error) {
|
||||||
func (t *Text) Reset() {
|
func (t *Text) Reset() {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
t.reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset implements Reset, caller must hold t.mu.
|
||||||
|
func (t *Text) reset() {
|
||||||
t.buff.Reset()
|
t.buff.Reset()
|
||||||
t.givenWOpts = nil
|
t.givenWOpts = nil
|
||||||
t.wOptsTracker = attrrange.NewTracker()
|
t.wOptsTracker = attrrange.NewTracker()
|
||||||
|
@ -109,8 +113,13 @@ func (t *Text) Write(text string, wOpts ...WriteOption) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts := newWriteOptions(wOpts...)
|
||||||
|
if opts.replace {
|
||||||
|
t.reset()
|
||||||
|
}
|
||||||
|
|
||||||
pos := t.buff.Len()
|
pos := t.buff.Len()
|
||||||
t.givenWOpts = append(t.givenWOpts, newWriteOptions(wOpts...))
|
t.givenWOpts = append(t.givenWOpts, opts)
|
||||||
wOptsIdx := len(t.givenWOpts) - 1
|
wOptsIdx := len(t.givenWOpts) - 1
|
||||||
if err := t.wOptsTracker.Add(pos, pos+len(text), wOptsIdx); err != nil {
|
if err := t.wOptsTracker.Add(pos, pos+len(text), wOptsIdx); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -133,6 +133,24 @@ func TestTextDraws(t *testing.T) {
|
||||||
return ft
|
return ft
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "multiple writes replace when requested",
|
||||||
|
canvas: image.Rect(0, 0, 12, 1),
|
||||||
|
writes: func(widget *Text) error {
|
||||||
|
if err := widget.Write("hello", WriteReplace()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return widget.Write("world", WriteReplace())
|
||||||
|
},
|
||||||
|
want: func(size image.Point) *faketerm.Terminal {
|
||||||
|
ft := faketerm.MustNew(size)
|
||||||
|
c := testcanvas.MustNew(ft.Area())
|
||||||
|
|
||||||
|
testdraw.MustText(c, "world", image.Point{0, 0})
|
||||||
|
testcanvas.MustApply(c, ft)
|
||||||
|
return ft
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
desc: "reset clears the content",
|
desc: "reset clears the content",
|
||||||
canvas: image.Rect(0, 0, 12, 1),
|
canvas: image.Rect(0, 0, 12, 1),
|
||||||
|
|
|
@ -29,6 +29,7 @@ type WriteOption interface {
|
||||||
// writeOptions stores the provided options.
|
// writeOptions stores the provided options.
|
||||||
type writeOptions struct {
|
type writeOptions struct {
|
||||||
cellOpts *cell.Options
|
cellOpts *cell.Options
|
||||||
|
replace bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// newWriteOptions returns new writeOptions instance.
|
// newWriteOptions returns new writeOptions instance.
|
||||||
|
@ -56,3 +57,11 @@ func WriteCellOpts(opts ...cell.Option) WriteOption {
|
||||||
wOpts.cellOpts = cell.NewOptions(opts...)
|
wOpts.cellOpts = cell.NewOptions(opts...)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteReplace instructs the text widget to replace the entire text content on
|
||||||
|
// this write instead of appending.
|
||||||
|
func WriteReplace() WriteOption {
|
||||||
|
return writeOption(func(wOpts *writeOptions) {
|
||||||
|
wOpts.replace = true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue