Skip to content
Myr

Much faster image processing

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

Fix #3984

authored by Junegunn Choi · committed by Junegunn Choi · parent 61d10d8 · Browse files · 3 files+102 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

3 files+102 added−6 removed

  • other:+102 added−6 removed

3 files+102 added−6 removed

modified:src/terminal_test.go+28 added
505 unmodified lines
506 }507 }508}509510func TestExtractPassthroughs(t *testing.T) {511 for _, middle := range []string{512 "\x1bPtmux;\x1b\x1bbar\x1b\\",513 "\x1bPtmux;\x1b\x1bbar\x1bbar\x1b\\",514 "\x1b]1337;bar\x1b\\",515 "\x1b]1337;bar\x1bbar\x1b\\",516 "\x1b]1337;bar\a",517 "\x1b_Ga=T,f=32,s=1258,v=1295,c=74,r=35,m=1\x1b\\",518 "\x1b_Ga=T,f=32,s=1258,v=1295,c=74,r=35,m=1\x1b\\\r",519 "\x1b_Ga=T,f=32,s=1258,v=1295,c=74,r=35,m=1\x1bbar\x1b\\\r",520 "\x1b_Gm=1;AAAAAAAAA=\x1b\\",521 "\x1b_Gm=1;AAAAAAAAA=\x1b\\\r",522 "\x1b_Gm=1;\x1bAAAAAAAAA=\x1b\\\r",523 } {524 line := "foo" + middle + "baz"525 loc := findPassThrough(line)526 if loc == nil || line[0:loc[0]] != "foo" || line[loc[1]:] != "baz" {527 t.Error("failed to find passthrough")528 }529 garbage := "\x1bPtmux;\x1b]1337;\x1b_Ga=\x1b]1337;bar\x1b."530 line = strings.Repeat("foo"+middle+middle+"baz", 3) + garbage531 passthroughs, result := extractPassThroughs(line)532 if result != "foobazfoobazfoobaz"+garbage || len(passthroughs) != 6 {533 t.Error("failed to extract passthroughs")534 }535 }536}537538/* utilities section */539540// Item represents one line in fzf UI. Usually it is relative path to files and folders.541func newItem(str string) *Item {
146 unmodified lines
modified:src/terminal.go+73 added-6 removed
49 unmodified lines
50var placeholder *regexp.Regexp51var whiteSuffix *regexp.Regexp52var offsetComponentRegex *regexp.Regexp53var offsetTrimCharsRegex *regexp.Regexp54var passThroughRegex *regexp.Regexp54var passThroughBeginRegex *regexp.Regexp55var passThroughEndTmuxRegex *regexp.Regexp56var ttyin *os.File5758const clearCode string = "\x1b[2J"59
14 unmodified lines
74 // * https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it75 // * https://sw.kovidgoyal.net/kitty/graphics-protocol76 // * https://en.wikipedia.org/wiki/Sixel77 // * https://iterm2.com/documentation-images.html77 passThroughRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b.*?[^\x1b]\x1b\\|\x1b(_G|P[0-9;]*q).*?\x1b\\\r?|\x1b]1337;.*?(\a|\x1b\\)`)78 /*79 passThroughRegex = regexp.MustCompile(`80 \x1bPtmux;\x1b\x1b .*? [^\x1b]\x1b\\81 | \x1b(_G|P[0-9;]*q) .*? \x1b\\\r?82 | \x1b]1337; .*? (\a|\x1b\\)83 `)84 */85 passThroughBeginRegex = regexp.MustCompile(`\x1bPtmux;\x1b\x1b|\x1b(_G|P[0-9;]*q)|\x1b]1337;`)86 passThroughEndTmuxRegex = regexp.MustCompile(`[^\x1b]\x1b\\`)87}8889type jumpMode int90
2539 unmodified lines
2630 }2631 return v + strings.Repeat(" ", repeat) + v2632}26332634func findPassThrough(line string) []int {2635 loc := passThroughBeginRegex.FindStringIndex(line)2636 if loc == nil {2637 return nil2638 }26392640 rest := line[loc[0]:]2641 after := line[loc[1]:]2642 if strings.HasPrefix(rest, "\x1bPtmux") { // Tmux2643 eloc := passThroughEndTmuxRegex.FindStringIndex(after)2644 if eloc == nil {2645 return nil2646 }2647 return []int{loc[0], loc[1] + eloc[1]}2648 } else if strings.HasPrefix(rest, "\x1b]1337;") { // iTerm22649 index := loc[1]2650 for {2651 after := line[index:]2652 pos := strings.IndexAny(after, "\x1b\a")2653 if pos < 0 {2654 return nil2655 }2656 if after[pos] == '\a' {2657 return []int{loc[0], index + pos + 1}2658 }2659 if pos < len(after)-1 && after[pos+1] == '\\' {2660 return []int{loc[0], index + pos + 2}2661 }2662 index += pos + 12663 }2664 }2665 // Kitty2666 pos := strings.Index(after, "\x1b\\")2667 if pos < 0 {2668 return nil2669 }2670 if pos < len(after)-2 && after[pos+2] == '\r' {2671 return []int{loc[0], loc[1] + pos + 3}2672 }2673 return []int{loc[0], loc[1] + pos + 2}2674}26752676func extractPassThroughs(line string) ([]string, string) {2677 passThroughs := []string{}2678 transformed := ""2679 index := 02680 for {2681 rest := line[index:]2682 loc := findPassThrough(rest)2683 if loc == nil {2684 transformed += rest2685 break2686 }2687 passThroughs = append(passThroughs, rest[loc[0]:loc[1]])2688 transformed += line[index : index+loc[0]]2689 index += loc[1]2690 }26912692 return passThroughs, transformed2693}26942695func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unchanged bool) {2696 maxWidth := t.pwindow.Width()2697 var ansi *ansiState2698 spinnerRedraw := t.pwindow.Y() == 0
6 unmodified lines
2705 if ansi != nil {2706 ansi.lbg = -12707 }27082639 passThroughs := passThroughRegex.FindAllString(line, -1)2640 if passThroughs != nil {2641 line = passThroughRegex.ReplaceAllString(line, "")2642 }2709 passThroughs, line := extractPassThroughs(line)2710 line = strings.TrimLeft(strings.TrimRight(line, "\r\n"), "\r")27112712 if lineNo >= height || t.pwindow.Y() == height-1 && t.pwindow.X() > 0 {2713 t.previewed.filled = true
2503 unmodified lines
modified:CHANGELOG.md+1 added
4 unmodified lines
5------6- Built-in walker improvements7 - `--walker-root` can take multiple directory arguments. e.g. `--walker-root include src lib`8 - `--walker-skip` can handle multi-component patterns. e.g. `--walker-skip target/build`9- Removed long processing delay when displaying images in the preview window10- `FZF_PREVIEW_*` environment variables are exported to all child processes (#4098)11- Bug fixes in fish scripts12130.56.3
2318 unmodified lines