Skip to content
Myr

Improved handling of tab characters

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 1b6cb35 · Browse files · 2 files+58 added−14 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+58 added−14 removed

  • other:+58 added−14 removed

2 files+58 added−14 removed

modified:src/constants.go+1 added-1 removed
3 unmodified lines
4 "github.com/junegunn/fzf/src/util"5)67// Current version8const Version = "0.9.1"8const Version = "0.9.2-dev"910// fzf events11const (12 EvtReadNew util.EventType = iota
6 unmodified lines
modified:src/terminal.go+57 added-13 removed
1package fzf23import (4 "bytes"5 "fmt"6 "os"7 "regexp"8 "sort"9 "strings"10 "sync"11 "time"1213 C "github.com/junegunn/fzf/src/curses"
156 unmodified lines
170 }171 }172}173174func runeWidth(r rune, prefixWidth int) int {175 if r == '\t' {176 return 8 - prefixWidth%8177 } else {178 return runewidth.RuneWidth(r)179 }180}181182func displayWidth(runes []rune) int {183 l := 0184 for _, r := range runes {175 l += runewidth.RuneWidth(r)185 l += runeWidth(r, l)186 }187 return l188}189
73 unmodified lines
263 }264}265266func trimRight(runes []rune, width int) ([]rune, int) {257 currentWidth := displayWidth(runes)258 trimmed := 0267 // We start from the beginning to handle tab characters268 l := 0269 for idx, r := range runes {270 l += runeWidth(r, l)271 if idx > 0 && l > width {272 return runes[:idx], len(runes) - idx273 }274 }275 return runes, 0276}277260 for currentWidth > width && len(runes) > 0 {261 sz := len(runes)262 currentWidth -= runewidth.RuneWidth(runes[sz-1])263 runes = runes[:sz-1]264 trimmed++278func displayWidthWithLimit(runes []rune, prefixWidth int, limit int) int {279 l := 0280 for _, r := range runes {281 l += runeWidth(r, l+prefixWidth)282 if l > limit {283 // Early exit284 return l285 }286 }266 return runes, trimmed287 return l288}289290func trimLeft(runes []rune, width int) ([]rune, int32) {291 currentWidth := displayWidth(runes)292 var trimmed int32293294 for currentWidth > width && len(runes) > 0 {274 currentWidth -= runewidth.RuneWidth(runes[0])295 runes = runes[1:]296 trimmed++297 currentWidth = displayWidthWithLimit(runes, 2, width)298 }299 return runes, trimmed300}301
41 unmodified lines
343 }344345 sort.Sort(ByOrder(offsets))346 var index int32347 var substr string348 var prefixWidth int349 for _, offset := range offsets {350 b := util.Max32(index, offset[0])351 e := util.Max32(index, offset[1])329 C.CPrint(col1, bold, string(text[index:b]))330 C.CPrint(col2, bold, string(text[b:e]))352353 substr, prefixWidth = processTabs(text[index:b], prefixWidth)354 C.CPrint(col1, bold, substr)355356 substr, prefixWidth = processTabs(text[b:e], prefixWidth)357 C.CPrint(col2, bold, substr)358359 index = e360 }361 if index < int32(len(text)) {362 substr, _ = processTabs(text[index:], prefixWidth)334 C.CPrint(col1, bold, string(text[index:]))363 C.CPrint(col1, bold, substr)364 }365}366367func processTabs(runes []rune, prefixWidth int) (string, int) {368 var strbuf bytes.Buffer369 l := prefixWidth370 for _, r := range runes {371 w := runeWidth(r, l)372 l += w373 if r == '\t' {374 strbuf.WriteString(strings.Repeat(" ", w))375 } else {376 strbuf.WriteRune(r)377 }378 }379 return strbuf.String(), l380}381382func (t *Terminal) printAll() {383 t.printList()384 t.printInfo()385 t.printPrompt()
313 unmodified lines