Fix FZF_CLICK_HEADER_NTH for multi-line headers
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent ffb6e28 · Browse files · 1 file·+37 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+37 added−18 removed
- other:+37 added−18 removed
Provenance
- Commit
c7ee3b8- Tree
a4d491f- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ffb6e28
1 file changed+37 added−18 removed
modified:src/terminal.go+37 added-18 removed
16 unmodified lines17 "sync"18 "sync/atomic"19 "syscall"20 "time"21+ "unicode"22 23 "github.com/rivo/uniseg"24 25 "github.com/junegunn/fzf/src/tui"4274 unmodified lines4300 * HL1 3 ------- -------4301 * HL2 4 H1 4 H1 44302 * 5 H2 5 H2 54303 */4303- lineNum := t.clickHeaderLine - 14304- if lineNum < 0 {4304+ clickHeaderLine := t.clickHeaderLine - 14305+ if clickHeaderLine < 0 {4306 // Never clicked on the header4307 return env4308 }4309 4310 // NOTE: t.header is padded with empty strings so that its size is equal to t.headerLines4310- var line string4311+ nthBase := 04312 headers := [2][]string{t.header, t.header0}4313 if t.layout == layoutReverse {4314 headers[0], headers[1] = headers[1], headers[0]4315 }4316+ var words []Token4317+ var lineNum int4318+ for lineNum = 0; lineNum <= clickHeaderLine; lineNum++ {4319+ currentLine := lineNum == clickHeaderLine4320+ var line string4315- if lineNum < len(headers[0]) {4316- index := lineNum4317- if t.layout == layoutDefault {4318- index = len(headers[0]) - index - 14321+ if lineNum < len(headers[0]) {4322+ index := lineNum4323+ if t.layout == layoutDefault {4324+ index = len(headers[0]) - index - 14325+ }4326+ line = headers[0][index]4327+ } else if lineNum-len(headers[0]) < len(headers[1]) {4328+ line = headers[1][lineNum-len(headers[0])]4329 }4320- line = headers[0][index]4321- } else if lineNum-len(headers[0]) < len(headers[1]) {4322- line = headers[1][lineNum-len(headers[0])]4330+ if currentLine && len(line) == 0 {4331+ return env4332+ }4333+4334+ words = Tokenize(line, t.delimiter)4335+ if currentLine {4336+ break4337+ } else {4338+ // TODO: Counting can be incorrect when the delimiter contains new line4339+ // characters, and there are empty lines in the header.4340+ nthBase += len(words)4341+ }4342 }4324- if len(line) == 0 {4325- return env4326- }4343 4344 colNum := t.clickHeaderColumn - 14329- words := Tokenize(line, t.delimiter)4345 for idx, token := range words {4346 prefixWidth := int(token.prefixLength)4347 word := token.text.ToString()4333- trimmed := strings.TrimSpace(word)4348+ trimmed := strings.TrimRightFunc(word, unicode.IsSpace)4349 trimWidth, _ := util.RunesWidth([]rune(trimmed), prefixWidth, t.tabstop, math.MaxInt32)4350 4351+ // Find the position of the first non-space character in the word4352+ minPos := strings.IndexFunc(trimmed, func(r rune) bool {4353+ return !unicode.IsSpace(r)4354+ })4336- if colNum >= prefixWidth && colNum < prefixWidth+trimWidth {4355+ if colNum >= minPos && colNum >= prefixWidth && colNum < prefixWidth+trimWidth {4356 env = append(env, fmt.Sprintf("FZF_CLICK_HEADER_WORD=%s", trimmed))4338- nth := fmt.Sprintf("FZF_CLICK_HEADER_NTH=%d", idx+1)4339- if idx == len(words)-1 {4357+ nth := fmt.Sprintf("FZF_CLICK_HEADER_NTH=%d", nthBase+idx+1)4358+ if lineNum == len(t.header)+len(t.header0)-1 && idx == len(words)-1 {4359 nth += ".."4360 }4361 env = append(env, nth)4362 return env2053 unmodified lines