~ghost08/tcell-term

tcell-term: title: send tcell event on title change v1 PROPOSED

Tim Culverhouse: 1
 title: send tcell event on title change

 2 files changed, 58 insertions(+), 4 deletions(-)
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/~ghost08/tcell-term/patches/35187/mbox | git am -3
Learn more about email & git

[PATCH tcell-term] title: send tcell event on title change Export this patch

Send a tcell event when the title of the terminal changes

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
---
 terminal.go          | 35 ++++++++++++++++++++++++++++++++---
 termutil/terminal.go | 27 ++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/terminal.go b/terminal.go
index 9e1245a27c73..c59cec1ef953 100644
--- a/terminal.go
+++ b/terminal.go
@@ -21,7 +21,7 @@ type Terminal struct {
	curStyle tcell.CursorStyle
	curVis   bool

	view views.View
	view     views.View
	interval int

	close bool
@@ -31,7 +31,7 @@ type Terminal struct {

func New(opts ...Option) *Terminal {
	t := &Terminal{
		term: termutil.New(),
		term:     termutil.New(),
		interval: 8,
	}
	t.term.SetWindowManipulator(&windowManipulator{})
@@ -74,6 +74,7 @@ func (t *Terminal) RunWithAttrs(cmd *exec.Cmd, attr *syscall.SysProcAttr) error
func (t *Terminal) run(cmd *exec.Cmd, attr *syscall.SysProcAttr) error {
	w, h := t.view.Size()
	tmr := time.NewTicker(time.Duration(t.interval) * time.Millisecond)
	eventCh := make(chan tcell.Event)
	go func() {
		for {
			select {
@@ -85,10 +86,20 @@ func (t *Terminal) run(cmd *exec.Cmd, attr *syscall.SysProcAttr) error {
					t.PostEventWidgetContent(t)
					t.term.SetRedraw(false)
				}
			case ev := <-eventCh:
				switch ev := ev.(type) {
				case *termutil.EventTitle:
					t.PostEvent(&EventTitle{
						widget: t,
						when:   ev.When(),
						title:  ev.Title(),
					})
				}
			}
		}
	}()
	err := t.term.Run(cmd, uint16(h), uint16(w), attr)

	err := t.term.Run(cmd, uint16(h), uint16(w), attr, eventCh)
	if err != nil {
		return err
	}
@@ -96,6 +107,24 @@ func (t *Terminal) run(cmd *exec.Cmd, attr *syscall.SysProcAttr) error {
	return nil
}

type EventTitle struct {
	when   time.Time
	title  string
	widget *Terminal
}

func (ev *EventTitle) When() time.Time {
	return ev.when
}

func (ev *EventTitle) Widget() views.Widget {
	return ev.widget
}

func (ev *EventTitle) Title() string {
	return ev.title
}

func (t *Terminal) Close() {
	t.close = true
	t.term.Pty().Close()
diff --git a/termutil/terminal.go b/termutil/terminal.go
index 78ada1378973..92d846b85078 100644
--- a/termutil/terminal.go
+++ b/termutil/terminal.go
@@ -8,8 +8,10 @@ import (
	"os"
	"os/exec"
	"syscall"
	"time"

	"github.com/creack/pty"
	"github.com/gdamore/tcell/v2"
	"golang.org/x/term"
)

@@ -30,6 +32,7 @@ type Terminal struct {
	mouseExtMode      MouseExtMode
	theme             *Theme
	redraw            bool
	eventCh           chan tcell.Event
}

// NewTerminal creates a new terminal instance
@@ -116,9 +119,10 @@ func (t *Terminal) SetSize(rows, cols uint16) error {
}

// Run starts the terminal/shell proxying process
func (t *Terminal) Run(c *exec.Cmd, rows uint16, cols uint16, attr *syscall.SysProcAttr) error {
func (t *Terminal) Run(c *exec.Cmd, rows uint16, cols uint16, attr *syscall.SysProcAttr, eventCh chan tcell.Event) error {
	c.Env = append(os.Environ(), "TERM=xterm-256color")

	t.eventCh = eventCh
	// Start the command with a pty.
	var err error
	// t.pty, err = pty.Start(c)
@@ -230,6 +234,7 @@ func (t *Terminal) translateRune(b MeasuredRune) MeasuredRune {
}

func (t *Terminal) setTitle(title string) {
	t.eventCh <- newEventTitle(title)
	t.windowManipulator.SetTitle(title)
}

@@ -265,3 +270,23 @@ func (t *Terminal) useMainBuffer() {
func (t *Terminal) useAltBuffer() {
	t.switchBuffer(AltBuffer)
}

type EventTitle struct {
	when  time.Time
	title string
}

func (ev *EventTitle) When() time.Time {
	return ev.when
}

func (ev *EventTitle) Title() string {
	return ev.title
}

func newEventTitle(title string) tcell.Event {
	return &EventTitle{
		when:  time.Now(),
		title: title,
	}
}
-- 
2.37.3