Add support for hyperlinks in preview window
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #2165
authored by Junegunn Choi · committed by Junegunn Choi · parent 2c8a96b · Browse files · 6 files·+80 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+80 added−10 removed
- other:+80 added−10 removed
Provenance
- Commit
d90a969- Tree
be0bbec- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2c8a96b
6 files changed+80 added−10 removed
modified:src/tui/light.go+8 added
1116 unmodified lines1117 // e.g. printf "foo\rbar" | fzf --ansi --preview 'printf "foo\rbar"'1118 return "\x1b[m"1119 }1120 1121+func (w *LightWindow) LinkBegin(uri string, params string) {1122+ w.renderer.queued.WriteString("\x1b]8;" + params + ";" + uri + "\x1b\\")1123+}1124+1125+func (w *LightWindow) LinkEnd() {1126+ w.renderer.queued.WriteString("\x1b]8;;\x1b\\")1127+}1128+1129 func (w *LightWindow) Fill(text string) FillReturn {1130 w.Move(w.posy, w.posx)1131 code := w.setBg()1132 return w.fill(text, code)36 unmodified linesmodified:src/tui/tcell.go+20 added
2 unmodified lines3 package tui4 5 import (6 "os"7+ "regexp"8 "time"9 10 "github.com/gdamore/tcell/v2"11 "github.com/junegunn/fzf/src/util"37 unmodified lines49 lastX int50 lastY int51 moveCursor bool52 borderStyle BorderStyle53+ uri *string54+ params *string55 }56 57 func (w *TcellWindow) Top() int {58 return w.top609 unmodified lines668 Underline(a&Attr(tcell.AttrUnderline) != 0).669 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).670 Italic(a&Attr(tcell.AttrItalic) != 0)671 672+ if w.uri != nil {673+ style = style.Url(*w.uri)674+ if md := regexp.MustCompile(`id=([^:]+)`).FindStringSubmatch(*w.params); len(md) > 1 {675+ style = style.UrlId(md[1])676+ }677+ }678+679 gr := uniseg.NewGraphemes(text)680 Loop:681 for gr.Next() {682 st := style42 unmodified lines725 func (w *TcellWindow) Fill(str string) FillReturn {726 return w.fillString(str, w.normal)727 }728 729+func (w *TcellWindow) LinkBegin(uri string, params string) {730+ w.uri = &uri731+ w.params = ¶ms732+}733+734+func (w *TcellWindow) LinkEnd() {735+ w.uri = nil736+ w.params = nil737+}738+739 func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) FillReturn {740 if fg == colDefault {741 fg = w.normal.Fg()742 }85 unmodified linesmodified:src/tui/tui.go+2 added
562 unmodified lines563 Print(text string)564 CPrint(color ColorPair, text string)565 Fill(text string) FillReturn566 CFill(fg Color, bg Color, attr Attr, text string) FillReturn567+ LinkBegin(uri string, params string)568+ LinkEnd()569 Erase()570 EraseMaybe() bool571 }572 307 unmodified linesmodified:src/ansi.go+33 added-5 removed
1 package fzf2 3 import (4+ "fmt"5 "strconv"6 "strings"7 "unicode/utf8"8 4 unmodified lines13 offset [2]int3214 color ansiState15 }16 17+type url struct {18+ uri string19+ params string20+}21+22 type ansiState struct {23 fg tui.Color24 bg tui.Color25 attr tui.Attr26 lbg tui.Color27+ url *url28 }29 30 func (s *ansiState) colored() bool {24- return s.fg != -1 || s.bg != -1 || s.attr > 0 || s.lbg >= 031+ return s.fg != -1 || s.bg != -1 || s.attr > 0 || s.lbg >= 0 || s.url != nil32 }33 34 func (s *ansiState) equals(t *ansiState) bool {35 if t == nil {36 return !s.colored()37 }31- return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr && s.lbg == t.lbg38+ return s.fg == t.fg && s.bg == t.bg && s.attr == t.attr && s.lbg == t.lbg && s.url == t.url39 }40 41 func (s *ansiState) ToString() string {42 if !s.colored() {23 unmodified lines66 ret += "9;"67 }68 ret += toAnsiString(s.fg, 30) + toAnsiString(s.bg, 40)69 63- return "\x1b[" + strings.TrimSuffix(ret, ";") + "m"70+ ret = "\x1b[" + strings.TrimSuffix(ret, ";") + "m"71+ if s.url != nil {72+ ret = fmt.Sprintf("\x1b]8;%s;%s\x1b\\%s\x1b]8;;\x1b", s.url.params, s.url.uri, ret)73+ }74+ return ret75 }76 77 func toAnsiString(color tui.Color, offset int) string {78 col := int(color)29 unmodified lines108 if i < len(s) {109 if s[i] == '\x07' {110 return i + 1111 }112+ // `\x1b]8;PARAMS;URI\x1b\\TITLE\x1b]8;;\x1b`113+ // ------114 if s[i] == '\x1b' && i < len(s)-1 && s[i+1] == '\\' {115 return i + 2116 }117 }118+119+ // `\x1b]8;PARAMS;URI\x1b\\TITLE\x1b]8;;\x1b`120+ // ------------121+ if i < len(s) && s[:i+1] == "\x1b]8;;\x1b" {122+ return i + 1123+ }124+125 return -1126 }127 128 func matchControlSequence(s string) int {218 unmodified lines347 348 func interpretCode(ansiCode string, prevState *ansiState) ansiState {349 var state ansiState350 if prevState == nil {331- state = ansiState{-1, -1, 0, -1}351+ state = ansiState{-1, -1, 0, -1, nil}352 } else {333- state = ansiState{prevState.fg, prevState.bg, prevState.attr, prevState.lbg}353+ state = ansiState{prevState.fg, prevState.bg, prevState.attr, prevState.lbg, prevState.url}354 }355 if ansiCode[0] != '\x1b' || ansiCode[1] != '[' || ansiCode[len(ansiCode)-1] != 'm' {356 if prevState != nil && strings.HasSuffix(ansiCode, "0K") {357 state.lbg = prevState.bg358+ } else if ansiCode == "\x1b]8;;\x1b" { // End of a hyperlink359+ state.url = nil360+ } else if strings.HasPrefix(ansiCode, "\x1b]8;") && strings.HasSuffix(ansiCode, "\x1b\\") {361+ if paramsEnd := strings.IndexRune(ansiCode[4:], ';'); paramsEnd >= 0 {362+ params := ansiCode[4 : 4+paramsEnd]363+ uri := ansiCode[5+paramsEnd : len(ansiCode)-2]364+ state.url = &url{uri: uri, params: params}365+ }366 }367 return state368 }369 110 unmodified linesmodified:src/result_test.go+4 added-4 removed
122 unmodified lines123 offsets := []Offset{{5, 15}, {10, 12}, {25, 35}}124 item := Result{125 item: &Item{126 colors: &[]ansiOffset{127- {[2]int32{0, 20}, ansiState{1, 5, 0, -1}},128- {[2]int32{22, 27}, ansiState{2, 6, tui.Bold, -1}},129- {[2]int32{30, 32}, ansiState{3, 7, 0, -1}},130- {[2]int32{33, 40}, ansiState{4, 8, tui.Bold, -1}}}}}127+ {[2]int32{0, 20}, ansiState{1, 5, 0, -1, nil}},128+ {[2]int32{22, 27}, ansiState{2, 6, tui.Bold, -1, nil}},129+ {[2]int32{30, 32}, ansiState{3, 7, 0, -1, nil}},130+ {[2]int32{33, 40}, ansiState{4, 8, tui.Bold, -1, nil}}}}}131 132 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)133 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)134 colors := item.colorOffsets(offsets, tui.Dark256, colBase, colMatch, true)40 unmodified linesmodified:src/terminal.go+13 added-1 removed
1066 unmodified lines1067 // ------- // Do not apply ANSI attributes to the trailing whitespaces1068 // // unless the part has a non-default ANSI state1069 loc := whiteSuffix.FindStringIndex(trimmed)1070 if loc != nil {1071- blankState := ansiOffset{[2]int32{int32(loc[0]), int32(loc[1])}, ansiState{-1, -1, tui.AttrClear, -1}}1071+ blankState := ansiOffset{[2]int32{int32(loc[0]), int32(loc[1])}, ansiState{-1, -1, tui.AttrClear, -1, nil}}1072 if item.colors != nil {1073 lastColor := (*item.colors)[len(*item.colors)-1]1074 if lastColor.offset[1] < int32(loc[1]) {1075 blankState.offset[0] = lastColor.offset[1]1591 unmodified lines2667 }2668 2669 var fillRet tui.FillReturn2670 prefixWidth := 02671+ var url *url2672 _, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {2673 trimmed := []rune(str)2674 isTrimmed := false2675 if !t.previewOpts.wrap {2676 trimmed, isTrimmed = t.trimRight(trimmed, maxWidth-t.pwindow.X())2677 }2678+ if url == nil && ansi != nil && ansi.url != nil {2679+ url = ansi.url2680+ t.pwindow.LinkBegin(url.uri, url.params)2681+ }2682+ if url != nil && (ansi == nil || ansi.url == nil) {2683+ url = nil2684+ t.pwindow.LinkEnd()2685+ }2686 str, width := t.processTabs(trimmed, prefixWidth)2687 if width > prefixWidth {2688 prefixWidth = width2689 if t.theme.Colored && ansi != nil && ansi.colored() {5 unmodified lines2695 }2696 return !isTrimmed &&2697 (fillRet == tui.FillContinue || t.previewOpts.wrap && fillRet == tui.FillNextLine)2698 })2699+ if url != nil {2700+ t.pwindow.LinkEnd()2701+ }2702 t.previewer.scrollable = t.previewer.scrollable || t.pwindow.Y() == height-1 && t.pwindow.X() == t.pwindow.Width()2703 if fillRet == tui.FillNextLine {2704 continue2705 } else if fillRet == tui.FillSuspend {2353 unmodified lines