~eliasnaur/gio-patches

gio: widget: make Editor implement io.Seeker, io.Reader and io.WriterTo v1 PROPOSED

~pierrec
~pierrec: 1
 widget: make Editor implement io.Seeker, io.Reader and io.WriterTo

 3 files changed, 68 insertions(+), 0 deletions(-)
#509518 apple.yml success
#509519 freebsd.yml success
#509520 linux.yml success
#509521 openbsd.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/22818/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gio] widget: make Editor implement io.Seeker, io.Reader and io.WriterTo Export this patch

~pierrec
From: pierre <pierre.curto@gmail.com>

The WriteTo, Seek, Read methods implement a more efficient access to
the Editor content than Text.

Signed-off-by: pierre <pierre.curto@gmail.com>
---
-

 widget/buffer.go      | 10 ++++++++++
 widget/editor.go      | 15 +++++++++++++++
 widget/editor_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)

diff --git a/widget/buffer.go b/widget/buffer.go
index e658d56..50e7175 100644
--- a/widget/buffer.go
+++ b/widget/buffer.go
@@ -142,6 +142,16 @@ func (e *editBuffer) ReadRune() (rune, int, error) {
	return r, s, nil
}

// WriteTo implements io.WriterTo.
func (e *editBuffer) WriteTo(w io.Writer) (int64, error) {
	n1, err := w.Write(e.text[:e.gapstart])
	if err != nil {
		return int64(n1), err
	}
	n2, err := w.Write(e.text[e.gapend:])
	return int64(n1 + n2), err
}

func (e *editBuffer) String() string {
	var b strings.Builder
	b.Grow(e.len())
diff --git a/widget/editor.go b/widget/editor.go
index 19aef46..2de5537 100644
--- a/widget/editor.go
+++ b/widget/editor.go
@@ -1250,6 +1250,21 @@ func (e *Editor) ClearSelection() {
	e.caret.end = e.caret.start
}

// WriteTo implements io.WriterTo.
func (e *Editor) WriteTo(w io.Writer) (int64, error) {
	return e.rr.WriteTo(w)
}

// Seek implements io.Seeker.
func (e *Editor) Seek(offset int64, whence int) (int64, error) {
	return e.rr.Seek(0, io.SeekStart)
}

// Read implements io.Reader.
func (e *Editor) Read(p []byte) (int, error) {
	return e.rr.Read(p)
}

func max(a, b int) int {
	if a > b {
		return a
diff --git a/widget/editor_test.go b/widget/editor_test.go
index 2babdbf..1d38e30 100644
--- a/widget/editor_test.go
+++ b/widget/editor_test.go
@@ -3,8 +3,10 @@
package widget

import (
	"bytes"
	"fmt"
	"image"
	"io"
	"math/rand"
	"reflect"
	"strings"
@@ -476,6 +478,47 @@ func TestSelectMove(t *testing.T) {
	testKey(key.NameDownArrow)
}

func TestEditor_Read(t *testing.T) {
	s := "hello world"
	buf := make([]byte, len(s))
	testEditorIO(t, s, func(e *Editor) (int, error) {
		_, err := e.Seek(0, io.SeekStart)
		if err != nil {
			t.Error(err)
		}
		return io.ReadFull(e, buf)
	})
	if got, want := string(buf), s; got != want {
		t.Errorf("got %q; want %q", got, want)
	}
}

func TestEditor_WriteTo(t *testing.T) {
	s := "hello world"
	var buf bytes.Buffer
	testEditorIO(t, s, func(e *Editor) (int, error) {
		n, err := io.Copy(&buf, e)
		return int(n), err
	})
	if got, want := buf.String(), s; got != want {
		t.Errorf("got %q; want %q", got, want)
	}
}

func testEditorIO(t *testing.T, s string, run func(e *Editor) (int, error)) {
	t.Helper()
	e := new(Editor)
	e.SetText(s)

	n, err := run(e)
	if err != nil {
		t.Error(err)
	}
	if got, want := n, len(s); got != want {
		t.Errorf("got %d; want %d", got, want)
	}
}

func textWidth(e *Editor, lineNum, colStart, colEnd int) float32 {
	var w fixed.Int26_6
	advances := e.lines[lineNum].Layout.Advances
-- 
2.30.2
builds.sr.ht
gio/patches: SUCCESS in 22m28s

[widget: make Editor implement io.Seeker, io.Reader and io.WriterTo][0] from [~pierrec][1]

[0]: https://lists.sr.ht/~eliasnaur/gio-patches/patches/22818
[1]: mailto:pierre.curto@gmail.com

✓ #509518 SUCCESS gio/patches/apple.yml   https://builds.sr.ht/~eliasnaur/job/509518
✓ #509520 SUCCESS gio/patches/linux.yml   https://builds.sr.ht/~eliasnaur/job/509520
✓ #509519 SUCCESS gio/patches/freebsd.yml https://builds.sr.ht/~eliasnaur/job/509519
✓ #509521 SUCCESS gio/patches/openbsd.yml https://builds.sr.ht/~eliasnaur/job/509521