Skip to content
Myr

Do not split preview header through an image

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

--preview-window ~N draws the header in its own pass, resuming the body in the middle of an image: an image arrives as one line but takes up many rows. ~N is typically global and meant for text, while the same preview command also renders images.

authored by Junegunn Choi · committed by Junegunn Choi · parent 5763168 · Browse files · 2 files+114 added−6 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+114 added−6 removed

  • other:+114 added−6 removed

2 files+114 added−6 removed

modified:src/terminal_test.go+54 added
576 unmodified lines
577 }578 }579}580581func TestIsImagePassThrough(t *testing.T) {582 for _, tc := range []struct {583 given string584 image bool585 }{586 // Kitty587 {"\x1b_Ga=T,f=32,s=1258,v=1295,c=74,r=35,m=1\x1b\\", true},588 {"\x1b_Ga=p,i=1\x1b\\", true},589 // The action key is not always first, and a put carries no payload590 {"\x1b_Gi=1,a=p\x1b\\", true},591 {"\x1b_Ga=p\x1b\\", true},592 {"\x1b_Ga=T,f=100\x1b\\\r", true},593 {"\x1b_Gi=1,a=d\x1b\\", false},594 {"\x1b_Ga=d,d=A\x1b\\", false}, // 'kitten icat --clear'595 {"\x1b_Ga=q,i=1\x1b\\", false}, // query596 {"\x1b_Gi=1,f=100\x1b\\", false}, // transmit only, the default action597 {"\x1b_Gm=1;AAAA\x1b\\", false}, // continuation chunk598 {"\x1b_Ga=f,i=1;AAAA\x1b\\", false}, // animation frame599 {"\x1b_Ga=T,U=1,f=32;AAAA\x1b\\", true}, // unicode placeholder600 // Wrapped in the tmux passthrough sequence601 {"\x1bPtmux;\x1b\x1b_Ga=T,f=100\x1b\x1b\\\x1b\\", true},602 {"\x1bPtmux;\x1b\x1b_Ga=d,d=A\x1b\x1b\\\x1b\\", false},603 {"\x1bPtmux;\x1b\x1b_Gi=1,a=p\x1b\x1b\\\x1b\\", true},604 // iTerm2605 {"\x1b]1337;File=inline=1:AAAA\a", true},606 {"\x1b]1337;MultipartFile=inline=1\a", true},607 {"\x1b]1337;SetUserVar=foo=YmFy\a", false},608 {"\x1b]1337;CurrentDir=/tmp\a", false},609 // Sixel610 {"\x1bP0;1;0q#0;2;0;0;0#0~~@@vv@@~~@\x1b\\", true},611 {"\x1bPq#0~~\x1b\\", true},612 {"\x1bPtmux;\x1b\x1bP0;1;0q#0~~\x1b\x1b\\\x1b\\", true},613 {"", false},614 } {615 if actual := isImagePassThrough(tc.given); actual != tc.image {616 t.Errorf("expected %v for %q, got %v", tc.image, tc.given, actual)617 }618 }619620 if containsImage([]string{"foo", "bar"}) {621 t.Error("plain text carries no image")622 }623 if !containsImage([]string{"foo", "bar\x1b_Ga=T,f=100\x1b\\baz"}) {624 t.Error("failed to find an image in a line")625 }626 if containsImage([]string{"foo\x1b_Ga=d,d=A\x1b\\"}) {627 t.Error("a delete command is not an image")628 }629 // The image is not the first passthrough on the line630 if !containsImage([]string{"\x1b_Ga=d,d=A\x1b\\text\x1b_Ga=T,f=100;AAAA\x1b\\"}) {631 t.Error("failed to look past an earlier passthrough")632 }633}634635/* utilities section */636637// Item represents one line in fzf UI. Usually it is relative path to files and folders.638func newItem(str string) *Item {
215 unmodified lines
modified:src/terminal.go+60 added-6 removed
55 unmodified lines
56var offsetComponentRegex *regexp.Regexp57var offsetTrimCharsRegex *regexp.Regexp58var passThroughBeginRegex *regexp.Regexp59var passThroughEndTmuxRegex *regexp.Regexp60var sixelBeginRegex *regexp.Regexp61var ttyin *os.File6263var inTmux = len(os.Getenv("TMUX")) > 064
29 unmodified lines
94 `)95 */96 passThroughBeginRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b|\x1b(_G|P[0-9;]*q)|\x1b]1337;`)97 passThroughEndTmuxRegex = regexp.MustCompile(`[^\x1b]\x1b\\`)98 sixelBeginRegex = regexp.MustCompile(`^\x1bP[0-9;]*q`)99}100101type jumpMode int102
4575 unmodified lines
46784679 height := t.pwindow.Height()4680 body := t.previewer.lines4681 headerLines := t.activePreviewOpts.headerLines4682 lineNo := -t.previewer.offset + headerLines4683 // Scrollbar is sized from the body alone, split off or not4684 scrollLines := len(body)4685 // Do not enable preview header lines if it's value is too large4686 if headerLines > 0 && headerLines < min(len(body), height) {4687 scrollLines -= headerLines4688 header := t.previewer.lines[0:headerLines]4689 // A separate header pass would resume the body inside an image, which4690 // takes up more rows than the line it arrives on4683 body = t.previewer.lines[headerLines:]4684 // Always redraw header4685 t.renderPreviewText(height, header, 0, false)4686 t.pwindow.MoveAndClear(t.pwindow.Y(), 0)4691 if !containsImage(header) {4692 // Always redraw header4693 t.renderPreviewText(height, header, 0, false)4694 t.pwindow.MoveAndClear(t.pwindow.Y(), 0)4695 body = t.previewer.lines[headerLines:]4696 }4697 }4688 t.renderPreviewText(height, body, -t.previewer.offset+headerLines, unchanged)4698 t.renderPreviewText(height, body, lineNo, unchanged)46994700 if !unchanged {4701 t.pwindow.FinishFill()4702 }
2 unmodified lines
4705 return4706 }47074708 effectiveHeight := height - headerLines4699 barLength, barStart := getScrollbar(1, len(body), effectiveHeight, min(len(body)-effectiveHeight, t.previewer.offset-headerLines))4709 barLength, barStart := getScrollbar(1, scrollLines, effectiveHeight, min(scrollLines-effectiveHeight, t.previewer.offset-headerLines))4710 t.renderPreviewScrollbar(headerLines, barLength, barStart)4711}47124713func (t *Terminal) makeImageBorder(width int, top bool) string {
71 unmodified lines
4785 }4786 return "\x1bPtmux;" + strings.ReplaceAll(passThrough, "\x1b", "\x1b\x1b") + "\x1b\\" + suffix4787}47884789// Whether the sequence draws an image. Kitty commands that only transmit or4790// delete do not4791func isImagePassThrough(passThrough string) bool {4792 // Unwrap the tmux passthrough sequence, in which every ESC is doubled4793 if after, ok := strings.CutPrefix(passThrough, "\x1bPtmux;"); ok {4794 passThrough = strings.ReplaceAll(after, "\x1b\x1b", "\x1b")4795 }4796 if after, ok := strings.CutPrefix(passThrough, "\x1b_G"); ok {4797 // Control data ends at the payload delimiter or at the terminator4798 keys := after4799 if index := strings.IndexAny(keys, ";\x1b"); index >= 0 {4800 keys = keys[:index]4801 }4802 for _, key := range strings.Split(keys, ",") {4803 // Transmit and display, or put an image already transmitted4804 if key == "a=T" || key == "a=p" {4805 return true4806 }4807 }4808 return false4809 }4810 if after, ok := strings.CutPrefix(passThrough, "\x1b]1337;"); ok {4811 return strings.HasPrefix(after, "File=") || strings.HasPrefix(after, "MultipartFile=")4812 }4813 return sixelBeginRegex.MatchString(passThrough)4814}48154816// Whether any line carries an image4817func containsImage(lines []string) bool {4818 for _, line := range lines {4819 for {4820 loc := findPassThrough(line)4821 if loc == nil {4822 break4823 }4824 if isImagePassThrough(line[loc[0]:loc[1]]) {4825 return true4826 }4827 line = line[loc[1]:]4828 }4829 }4830 return false4831}48324833func extractPassThroughs(line string) ([]string, string) {4834 passThroughs := []string{}4835 transformed := ""4836 index := 0
3953 unmodified lines