~ghost08/tcell-term

0dd3a05ea817250e78e15b790c1163feead0b737 — Tim Culverhouse 1 year, 2 months ago 06c8aaf
api: internalize redraw chan

Internalize redraw chan and use it to post an event to the tcell screen.
A new redraw request will issue a RedrawEvent to the screen.

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
1 files changed, 31 insertions(+), 5 deletions(-)

M terminal.go
M terminal.go => terminal.go +31 -5
@@ 5,6 5,7 @@ import (
	"image/color"
	"os"
	"os/exec"
	"time"

	"git.sr.ht/~ghost08/tcell-term/termutil"
	"github.com/gdamore/tcell/v2"


@@ 20,7 21,9 @@ type Terminal struct {
	curVis   bool

	screen tcell.Screen
	view     views.View
	view   views.View

	redrawChan chan struct{}

	views.WidgetWatchers
}


@@ 37,9 40,9 @@ func New(screen tcell.Screen, view views.View, opts ...Option) *Terminal {
		view = views.NewViewPort(screen, 0, 0, -1, -1)
	}
	t := &Terminal{
		term: termutil.New(),
		term:   termutil.New(),
		screen: screen,
		view: view,
		view:   view,
	}
	t.term.SetWindowManipulator(&windowManipulator{})
	for _, opt := range opts {


@@ 56,11 59,34 @@ func WithWindowManipulator(wm termutil.WindowManipulator) Option {
	}
}

func (t *Terminal) Run(cmd *exec.Cmd, redrawChan chan struct{}) error {
func (t *Terminal) Run(cmd *exec.Cmd) error {
	w, h := t.view.Size()
	return t.term.Run(cmd, redrawChan, uint16(h), uint16(w))
	t.redrawChan = make(chan struct{}, 10)
	go func() {
		for {
			select {
			case <-t.redrawChan:
				t.screen.PostEvent(NewRedrawEvent())
				// term.Draw()
				// s.Show()
			}
		}
	}()
	return t.term.Run(cmd, t.redrawChan, uint16(h), uint16(w))
}

type RedrawEvent struct {
	when time.Time
}

func NewRedrawEvent() *RedrawEvent {
	return &RedrawEvent{
		when: time.Now(),
	}
}

func (e RedrawEvent) When() time.Time { return e.when }

func (t *Terminal) SetView(view views.View) {
	t.view = view
}