From 0dd3a05ea817250e78e15b790c1163feead0b737 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 7 Sep 2022 10:10:08 -0500 Subject: [PATCH] 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 --- terminal.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/terminal.go b/terminal.go index e895fb6..8cfdb1a 100644 --- a/terminal.go +++ b/terminal.go @@ -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 } -- 2.45.2