~ghost08/tcell-term

0055e77717ba7f72a36a78f11fc7b06c8935ea49 — Tim Culverhouse 2 years ago 833545e
title: send tcell event on title change

Send a tcell event when the title of the terminal changes

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
2 files changed, 58 insertions(+), 4 deletions(-)

M terminal.go
M termutil/terminal.go
M terminal.go => terminal.go +32 -3
@@ 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()

M termutil/terminal.go => termutil/terminal.go +26 -1
@@ 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,
	}
}