Skip to content
Myr

Skip redundant SGR sequences in light renderer

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Track the last emitted SGR sequence and skip no-op transitions. Reduces rendering output by 10-23% depending on the scenario.

authored by Junegunn Choi · committed by Junegunn Choi · parent 374b012 · Browse files · 2 files+103 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+103 added−21 removed

  • other:+103 added−21 removed

2 files+103 added−21 removed

modified:src/tui/light_test.go+58 added
417 unmodified lines
418 assertScroll("\x1b[<93;1;1M", -1, "ctrl-alt-shift") // ctrl+alt+shift + down419 assertScroll("\x1b[<94;1;1M", 0, "ctrl-alt-shift") // ctrl+alt+shift + left (ignored)420 assertScroll("\x1b[<95;1;1M", 0, "ctrl-alt-shift") // ctrl+alt+shift + right (ignored)421}422423// Assert the exact byte stream emitted for a scripted rendering sequence,424// locking down the SGR deduplication behavior of the light renderer.425func TestLightRendererSGRDeduplication(t *testing.T) {426 renderer, _ := NewLightRenderer(427 "", nil, &ColorTheme{}, true, false, 8, false, true,428 func(h int) int { return h })429 r := renderer.(*LightRenderer)430 r.width = 80431 r.height = 24432433 out, err := os.CreateTemp(t.TempDir(), "fzf-ttyout")434 if err != nil {435 t.Fatal(err)436 }437 r.ttyout = out438439 w := r.NewWindow(0, 0, 40, 4, WindowList, MakeBorderStyle(BorderNone, true), false).(*LightWindow)440 w.fg = colDefault441 w.bg = colDefault442 r.queued.Reset()443444 red := NewColorPair(196, colDefault, AttrRegular)445 blue := NewColorPair(21, colDefault, AttrRegular)446447 w.Move(0, 0)448 w.CPrint(red, "ab")449 w.CPrint(red, "cd") // same pair; no SGR expected450 w.CPrint(blue, "ef")451 w.Print("gh") // default colors; one reset expected452 w.Print("ij") // still default; no SGR expected453 w.Move(1, 2)454 w.CPrint(red, "kl")455 r.invalidateSGR()456 w.CPrint(red, "mn") // re-emitted after invalidation457 r.flush() // adds a trailing reset to leave the default state458459 expected := "\x1b[?2026h\x1b[?7l\x1b[?25l" +460 "\r" +461 "\x1b[;38;5;196mab" +462 "cd" +463 "\x1b[;38;5;21mef" +464 "\x1b[0mgh" +465 "ij" +466 "\x1b[1B\x1b[3G" +467 "\x1b[;38;5;196mkl" +468 "\x1b[;38;5;196mmn" +469 "\x1b[0m" +470 "\x1b[?25h\x1b[?7h\x1b[?2026l"471472 bytes, err := os.ReadFile(out.Name())473 if err != nil {474 t.Fatal(err)475 }476 if string(bytes) != expected {477 t.Errorf("expected: %q\n got: %q", expected, string(bytes))478 }479}
modified:src/tui/light.go+45 added-21 removed
37 unmodified lines
38}3940func (r *LightRenderer) PassThrough(str string) {41 r.queued.WriteString("\x1b7" + str + "\x1b8")42 r.invalidateSGR()43}4445func (r *LightRenderer) stderr(str string) {46 r.stderrInternal(str, true, "")
43 unmodified lines
90 r.stderr(fullcode)91 return fullcode92}9394// setSGR emits the given SGR sequence only when the terminal is not already95// in that state. Sequences built by csiColor start with a reset parameter,96// so each of them fully determines the state on its own. The zero value of97// r.sgr never matches a sequence, so the first update after Init, Resume, or98// invalidateSGR is always emitted.99func (r *LightRenderer) setSGR(code string) {100 if code != r.sgr {101 r.stderr(code)102 r.sgr = code103 }104}105106// invalidateSGR marks the terminal SGR state as unknown, e.g. after raw107// output that may have changed it behind the renderer's back.108func (r *LightRenderer) invalidateSGR() {109 r.sgr = ""110}111112func (r *LightRenderer) flush() {113 if r.queued.Len() > 0 {114 // Leave the terminal in the default SGR state between frames115 r.setSGR("\x1b[0m")116 // Wrap the frame in synchronized update mode (2026) so that the117 // terminal applies it atomically. Terminals without support ignore118 // the unknown private mode and behave as before.119 raw := "\x1b[?2026h\x1b[?7l\x1b[?25l" + r.queued.String()
30 unmodified lines
150 escDelay int151 fullscreen bool152 upOneLine bool153 queued strings.Builder154 sgr string155 y int156 x int157 maxHeightFunc func(int) int158 showCursor bool
864 unmodified lines
1023}10241025func (r *LightRenderer) Resume(clear bool, sigcont bool) {1026 r.setupTerminal()1027 // The programs that ran in the meantime may have left the terminal in an1028 // arbitrary SGR state1029 r.invalidateSGR()1030 if clear {1031 if r.fullscreen {1032 r.smcup()1033 } else {
263 unmodified lines
1297 w.drawHLine(w.height-1, w.border.bottom, leftCap, rightCap, color)1298 }1299}13001276func (w *LightWindow) csi(code string) string {1277 return w.renderer.csi(code)1278}12791301func (w *LightWindow) stderrInternal(str string, allowNLCR bool, resetCode string) {1302 w.renderer.stderrInternal(str, allowNLCR, resetCode)1303}1304
130 unmodified lines
1435 }1436 return ""1437}14381439// csiColor builds the SGR sequence for the given colors and attributes1440// without emitting it. The sequence starts with a reset parameter, so it1441// fully determines the SGR state on its own.1442func (w *LightWindow) csiColor(fg Color, bg Color, ul Color, attr Attr) (bool, string) {1443 codes := append(attrCodes(attr), colorCodes(fg, bg)...)1444 if ulCode := ulColorCode(ul); ulCode != "" {1445 codes = append(codes, ulCode)1446 }1423 code := w.csi(";" + strings.Join(codes, ";") + "m")1424 return len(codes) > 0, code1447 if len(codes) == 0 {1448 return false, "\x1b[0m"1449 }1450 return true, "\x1b[;" + strings.Join(codes, ";") + "m"1451}14521453func (w *LightWindow) Print(text string) {1454 w.cprint2(colDefault, w.bg, AttrRegular, text)
4 unmodified lines
1459}14601461func (w *LightWindow) CPrint(pair ColorPair, text string) {1462 _, code := w.csiColor(pair.Fg(), pair.Bg(), pair.Ul(), pair.Attr())1463 w.renderer.setSGR(code)1464 w.stderrInternal(cleanse(text), false, code)1438 w.csi("0m")1465}14661467func (w *LightWindow) cprint2(fg Color, bg Color, attr Attr, text string) {1442 hasColors, code := w.csiColor(fg, bg, colDefault, attr)1443 if hasColors {1444 defer w.csi("0m")1445 }1468 _, code := w.csiColor(fg, bg, colDefault, attr)1469 w.renderer.setSGR(code)1470 w.stderrInternal(cleanse(text), false, code)1471}14721473func (w *LightWindow) fill(str string, resetCode string) FillReturn {
12 unmodified lines
1486 return FillSuspend1487 }1488 w.MoveAndClear(w.posy, w.posx)1489 w.Move(w.posy+1, 0)1466 w.renderer.stderr(resetCode)1490 w.renderer.setSGR(resetCode)1491 if len(lines) > 1 {1492 sign := w.wrapSign1493 width := w.wrapSignWidth1494 if width > w.width {
1 unmodified line
1496 sign = string(runes)1497 width = truncatedWidth1498 }1499 w.stderrInternal(DIM+sign, false, resetCode)1476 w.renderer.stderr(resetCode)1500 w.renderer.invalidateSGR()1501 w.renderer.setSGR(resetCode)1502 w.Move(w.posy, width)1503 }1504 }1505 }
2 unmodified lines
1508 if w.posy+1 >= w.height {1509 return FillSuspend1510 }1511 w.Move(w.posy+1, 0)1487 w.renderer.stderr(resetCode)1512 w.renderer.setSGR(resetCode)1513 return FillNextLine1514 }1515 return FillContinue1516}15171518func (w *LightWindow) setBg() string {1494 if w.bg != colDefault {1495 _, code := w.csiColor(colDefault, w.bg, colDefault, AttrRegular)1496 return code1497 }1498 // Should clear dim attribute after ␍ in the preview window1519 // The plain reset code for the default background is still required to1520 // clear the dim attribute after ␍ in the preview window1521 // e.g. printf "foo\rbar" | fzf --ansi --preview 'printf "foo\rbar"'1500 return "\x1b[m"1522 _, code := w.csiColor(colDefault, w.bg, colDefault, AttrRegular)1523 w.renderer.setSGR(code)1524 return code1525}15261527func (w *LightWindow) LinkBegin(uri string, params string) {1528 w.renderer.queued.WriteString("\x1b]8;" + params + ";" + uri + "\x1b\\")
17 unmodified lines
1546 if bg == colDefault {1547 bg = w.bg1548 }1549 if hasColors, resetCode := w.csiColor(fg, bg, ul, attr); hasColors {1526 defer w.csi("0m")1550 w.renderer.setSGR(resetCode)1551 return w.fill(text, resetCode)1552 }1553 return w.fill(text, w.setBg())1554}
35 unmodified lines