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
Provenance
- Commit
8bead4a- Tree
ceba4ab- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1b6cb35
2 files changed+58 added−14 removed
modified:src/constants.go+1 added-1 removed
3 unmodified lines4 "github.com/junegunn/fzf/src/util"5 )6 7 // Current version8-const Version = "0.9.1"8+const Version = "0.9.2-dev"9 10 // fzf events11 const (12 EvtReadNew util.EventType = iota6 unmodified linesmodified:src/terminal.go+57 added-13 removed
1 package fzf2 3 import (4+ "bytes"5 "fmt"6 "os"7 "regexp"8 "sort"9+ "strings"10 "sync"11 "time"12 13 C "github.com/junegunn/fzf/src/curses"156 unmodified lines170 }171 }172 }173 174+func runeWidth(r rune, prefixWidth int) int {175+ if r == '\t' {176+ return 8 - prefixWidth%8177+ } else {178+ return runewidth.RuneWidth(r)179+ }180+}181+182 func 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 lines263 }264 }265 266 func 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+}277 260- for currentWidth > width && len(runes) > 0 {261- sz := len(runes)262- currentWidth -= runewidth.RuneWidth(runes[sz-1])263- runes = runes[:sz-1]264- trimmed++278+func 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 }289 290 func trimLeft(runes []rune, width int) ([]rune, int32) {291 currentWidth := displayWidth(runes)292 var trimmed int32293 294 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 lines343 }344 345 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]))352+353+ substr, prefixWidth = processTabs(text[index:b], prefixWidth)354+ C.CPrint(col1, bold, substr)355+356+ substr, prefixWidth = processTabs(text[b:e], prefixWidth)357+ C.CPrint(col2, bold, substr)358+359 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 }366 367+func 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+}381+382 func (t *Terminal) printAll() {383 t.printList()384 t.printInfo()385 t.printPrompt()313 unmodified lines