~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, 67 insertions(+), 0 deletions(-)
#509592 apple.yml success
#509593 freebsd.yml success
#509594 linux.yml success
#509595 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/22822/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 | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 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..56e7631 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,46 @@ func TestSelectMove(t *testing.T) {
	testKey(key.NameDownArrow)
}

func TestEditor_Read(t *testing.T) {
	s := "hello world"
	buf := make([]byte, len(s))
	e := new(Editor)
	e.SetText(s)

	_, err := e.Seek(0, io.SeekStart)
	if err != nil {
		t.Error(err)
	}
	n, err := io.ReadFull(e, buf)
	if err != nil {
		t.Error(err)
	}
	if got, want := n, len(s); got != want {
		t.Errorf("got %d; want %d", got, want)
	}
	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
	e := new(Editor)
	e.SetText(s)

	n, err := io.Copy(&buf, e)
	if err != nil {
		t.Error(err)
	}
	if got, want := int(n), len(s); got != want {
		t.Errorf("got %d; want %d", got, want)
	}
	if got, want := buf.String(), s; got != want {
		t.Errorf("got %q; want %q", 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 20m38s

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

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

✓ #509592 SUCCESS gio/patches/apple.yml   https://builds.sr.ht/~eliasnaur/job/509592
✓ #509593 SUCCESS gio/patches/freebsd.yml https://builds.sr.ht/~eliasnaur/job/509593
✓ #509595 SUCCESS gio/patches/openbsd.yml https://builds.sr.ht/~eliasnaur/job/509595
✓ #509594 SUCCESS gio/patches/linux.yml   https://builds.sr.ht/~eliasnaur/job/509594
Thanks, merged.