Skip to content

Commit

Permalink
feat: synchronized output
Browse files Browse the repository at this point in the history
  • Loading branch information
nervo committed Jun 17, 2024
1 parent ab28057 commit 74ceda3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions nil_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ func (n nilRenderer) enableMouseSGRMode() {}
func (n nilRenderer) disableMouseSGRMode() {}
func (n nilRenderer) bracketedPasteActive() bool { return false }
func (n nilRenderer) setWindowTitle(_ string) {}
func (n nilRenderer) enableSynchronizedOutput() {}
3 changes: 3 additions & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type renderer interface {

// setWindowTitle sets the terminal window title.
setWindowTitle(string)

// enableSynchronizedOutput enables synchronized output.
enableSynchronizedOutput()
}

// repaintMsg forces a full repaint.
Expand Down
18 changes: 9 additions & 9 deletions screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,47 @@ func TestClearMsg(t *testing.T) {
{
name: "clear_screen",
cmds: []Cmd{ClearScreen},
expected: "\x1b[?25l\x1b[?2004h\x1b[2J\x1b[1;1H\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[2J\x1b[1;1H\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "altscreen",
cmds: []Cmd{EnterAltScreen, ExitAltScreen},
expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "altscreen_autoexit",
cmds: []Cmd{EnterAltScreen},
expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h",
},
{
name: "mouse_cellmotion",
cmds: []Cmd{EnableMouseCellMotion},
expected: "\x1b[?25l\x1b[?2004h\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "mouse_allmotion",
cmds: []Cmd{EnableMouseAllMotion},
expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "mouse_disable",
cmds: []Cmd{EnableMouseAllMotion, DisableMouse},
expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "cursor_hide",
cmds: []Cmd{HideCursor},
expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "cursor_hideshow",
cmds: []Cmd{HideCursor, ShowCursor},
expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\x1b[?25h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?25l\x1b[?25h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
{
name: "bp_stop_start",
cmds: []Cmd{DisableBracketedPaste, EnableBracketedPaste},
expected: "\x1b[?25l\x1b[?2004h\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
expected: "\x1b[?25l\x1b[?2026$p\x1b[?2004h\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",
},
}

Expand Down
21 changes: 21 additions & 0 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type standardRenderer struct {
// whether or not we're currently using bracketed paste
bpActive bool

// whether or not we're currently using synchronized output
soActive bool

// renderer dimensions; usually the size of the window
width int
height int
Expand Down Expand Up @@ -274,7 +277,18 @@ func (r *standardRenderer) flush() {
buf.WriteString(ansi.CursorLeft(r.width))
}

// Enable synchronized output
if r.soActive {
_, _ = io.WriteString(r.out, ansi.EnableSyncdOutput)
}

_, _ = r.out.Write(buf.Bytes())

// Disable synchronized output
if r.soActive {
_, _ = io.WriteString(r.out, ansi.DisableSyncdOutput)
}

r.lastRender = r.buf.String()
r.buf.Reset()
}
Expand Down Expand Up @@ -459,6 +473,13 @@ func (r *standardRenderer) setWindowTitle(title string) {
r.execute(ansi.SetWindowTitle(title))
}

func (r *standardRenderer) enableSynchronizedOutput() {
r.mtx.Lock()
defer r.mtx.Unlock()

r.soActive = true
}

// setIgnoredLines specifies lines not to be touched by the standard Bubble Tea
// renderer.
func (r *standardRenderer) setIgnoredLines(from int, to int) {
Expand Down
16 changes: 16 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/input"
"github.com/charmbracelet/x/term"
"github.com/muesli/cancelreader"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -493,6 +496,19 @@ func (p *Program) Run() (Model, error) {
return p.initialModel, err
}

// See: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
_ = term.QueryTerminal(p.input, p.output, time.Millisecond*100, func(events []input.Event) bool {

Check failure on line 500 in tea.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 100, in <argument> detected (gomnd)
for _, e := range events {
switch event := e.(type) {
case input.ReportModeEvent:
if event.Mode == 2026 && (event.Value == 2 || event.Value == 4) {
p.renderer.enableSynchronizedOutput()
}
}
}
return true
}, ansi.RequestSyncdOutput)

// Honor program startup options.
if p.startupTitle != "" {
p.renderer.setWindowTitle(p.startupTitle)
Expand Down

0 comments on commit 74ceda3

Please sign in to comment.