Skip to content
Myr

Render CR and LF as ␍ and ␊

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

Close #2529

authored by Junegunn Choi · committed by Junegunn Choi · parent e5103d9 · Browse files · 6 files+95 added−55 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+95 added−55 removed

  • tests:+3 added−3 removed
  • other:+92 added−52 removed

6 files+95 added−55 removed

modified:src/tui/light.go+45 added-30 removed
30 unmodified lines
31var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")32var offsetRegexpBegin *regexp.Regexp = regexp.MustCompile("^\x1b\\[[0-9]+;[0-9]+R")3334func (r *LightRenderer) stderr(str string) {35 r.stderrInternal(str, true)35 r.stderrInternal(str, true, "")36}3738// FIXME: Need better handling of non-displayable characters39func (r *LightRenderer) stderrInternal(str string, allowNLCR bool) {38const CR string = "\x1b[2m␍"39const LF string = "\x1b[2m␊"4041func (r *LightRenderer) stderrInternal(str string, allowNLCR bool, resetCode string) {42 bytes := []byte(str)43 runes := []rune{}44 for len(bytes) > 0 {45 r, sz := utf8.DecodeRune(bytes)46 nlcr := r == '\n' || r == '\r'47 if r >= 32 || r == '\x1b' || nlcr {46 if r == utf8.RuneError || nlcr && !allowNLCR {47 runes = append(runes, ' ')48 } else {48 if nlcr && !allowNLCR {49 if r == '\r' {50 runes = append(runes, []rune(CR+resetCode)...)51 } else {52 runes = append(runes, []rune(LF+resetCode)...)53 }54 } else if r != utf8.RuneError {55 runes = append(runes, r)56 }57 }58 bytes = bytes[sz:]59 }60 r.queued.WriteString(string(runes))61}6257func (r *LightRenderer) csi(code string) {58 r.stderr("\x1b[" + code)63func (r *LightRenderer) csi(code string) string {64 fullcode := "\x1b[" + code65 r.stderr(fullcode)66 return fullcode67}6869func (r *LightRenderer) flush() {70 if r.queued.Len() > 0 {
761 unmodified lines
832 rem = (w.width - bcw) % hw833 w.CPrint(color, string(w.border.bottomLeft)+repeat(w.border.horizontal, (w.width-bcw)/hw)+repeat(' ', rem)+string(w.border.bottomRight))834}835828func (w *LightWindow) csi(code string) {829 w.renderer.csi(code)836func (w *LightWindow) csi(code string) string {837 return w.renderer.csi(code)838}839832func (w *LightWindow) stderrInternal(str string, allowNLCR bool) {833 w.renderer.stderrInternal(str, allowNLCR)840func (w *LightWindow) stderrInternal(str string, allowNLCR bool, resetCode string) {841 w.renderer.stderrInternal(str, allowNLCR, resetCode)842}843844func (w *LightWindow) Top() int {845 return w.top
97 unmodified lines
943 appendCode(bg, 10)944 return codes945}946939func (w *LightWindow) csiColor(fg Color, bg Color, attr Attr) bool {947func (w *LightWindow) csiColor(fg Color, bg Color, attr Attr) (bool, string) {948 codes := append(attrCodes(attr), colorCodes(fg, bg)...)941 w.csi(";" + strings.Join(codes, ";") + "m")942 return len(codes) > 0949 code := w.csi(";" + strings.Join(codes, ";") + "m")950 return len(codes) > 0, code951}952953func (w *LightWindow) Print(text string) {954 w.cprint2(colDefault, w.bg, AttrRegular, text)
3 unmodified lines
958 return strings.Replace(str, "\x1b", "", -1)959}960961func (w *LightWindow) CPrint(pair ColorPair, text string) {954 w.csiColor(pair.Fg(), pair.Bg(), pair.Attr())955 w.stderrInternal(cleanse(text), false)962 _, code := w.csiColor(pair.Fg(), pair.Bg(), pair.Attr())963 w.stderrInternal(cleanse(text), false, code)964 w.csi("m")965}966967func (w *LightWindow) cprint2(fg Color, bg Color, attr Attr, text string) {960 if w.csiColor(fg, bg, attr) {968 hasColors, code := w.csiColor(fg, bg, attr)969 if hasColors {970 defer w.csi("m")971 }963 w.stderrInternal(cleanse(text), false)972 w.stderrInternal(cleanse(text), false, code)973}974975type wrappedLine struct {976 text string
11 unmodified lines
988 var w int989 if len(rs) == 1 && rs[0] == '\t' {990 w = tabstop - (prefixLength+width)%tabstop991 str = repeat(' ', w)992 } else if rs[0] == '\r' {993 w++994 } else {995 w = runewidth.StringWidth(str)996 }997 width += w
10 unmodified lines
1008 lines = append(lines, wrappedLine{string(line), width})1009 return lines1010}10111001func (w *LightWindow) fill(str string, onMove func()) FillReturn {1012func (w *LightWindow) fill(str string, resetCode string) FillReturn {1013 allLines := strings.Split(str, "\n")1014 for i, line := range allLines {1015 lines := wrapLine(line, w.posx, w.width, w.tabstop)1016 for j, wl := range lines {1006 w.stderrInternal(wl.text, false)1017 w.stderrInternal(wl.text, false, resetCode)1018 w.posx += wl.displayWidth10191020 // Wrap line1021 if j < len(lines)-1 || i < len(allLines)-1 {
1 unmodified line
1023 return FillSuspend1024 }1025 w.MoveAndClear(w.posy, w.posx)1026 w.Move(w.posy+1, 0)1016 onMove()1027 w.renderer.stderr(resetCode)1028 }1029 }1030 }1031 if w.posx+1 >= w.Width() {1032 if w.posy+1 >= w.height {1033 return FillSuspend1034 }1035 w.Move(w.posy+1, 0)1025 onMove()1036 w.renderer.stderr(resetCode)1037 return FillNextLine1038 }1039 return FillContinue1040}10411031func (w *LightWindow) setBg() {1042func (w *LightWindow) setBg() string {1043 if w.bg != colDefault {1033 w.csiColor(colDefault, w.bg, AttrRegular)1044 _, code := w.csiColor(colDefault, w.bg, AttrRegular)1045 return code1046 }1047 // Should clear dim attribute after ␍ in the preview window1048 // e.g. printf "foo\rbar" | fzf --ansi --preview 'printf "foo\rbar"'1049 return "\x1b[m"1050}10511052func (w *LightWindow) Fill(text string) FillReturn {1053 w.Move(w.posy, w.posx)1039 w.setBg()1040 return w.fill(text, w.setBg)1054 code := w.setBg()1055 return w.fill(text, code)1056}10571058func (w *LightWindow) CFill(fg Color, bg Color, attr Attr, text string) FillReturn {1059 w.Move(w.posy, w.posx)
2 unmodified lines
1062 }1063 if bg == colDefault {1064 bg = w.bg1065 }1051 if w.csiColor(fg, bg, attr) {1066 if hasColors, resetCode := w.csiColor(fg, bg, attr); hasColors {1067 defer w.csi("m")1053 return w.fill(text, func() { w.csiColor(fg, bg, attr) })1068 return w.fill(text, resetCode)1069 }1055 return w.fill(text, w.setBg)1070 return w.fill(text, w.setBg())1071}10721073func (w *LightWindow) FinishFill() {1074 w.MoveAndClear(w.posy, w.posx)
10 unmodified lines
modified:src/tui/tcell.go+26 added-15 removed
6 unmodified lines
7 "time"89 "github.com/gdamore/tcell/v2"10 "github.com/gdamore/tcell/v2/encoding"11 "github.com/junegunn/fzf/src/util"1213 "github.com/mattn/go-runewidth"14 "github.com/rivo/uniseg"15)
556 unmodified lines
572 }573574 gr := uniseg.NewGraphemes(text)575 for gr.Next() {576 st := style577 rs := gr.Runes()578579 if len(rs) == 1 {580 r := rs[0]579 if r < rune(' ') { // ignore control characters580 continue581 if r == '\r' {582 st = style.Dim(true)583 rs[0] = '␍'584 } else if r == '\n' {582 w.lastY++583 lx = 0585 st = style.Dim(true)586 rs[0] = '␊'587 } else if r < rune(' ') { // ignore control characters588 continue585 } else if r == '\u000D' { // skip carriage return586 continue589 }590 }591 var xPos = w.left + w.lastX + lx592 var yPos = w.top + w.lastY593 if xPos < (w.left+w.width) && yPos < (w.top+w.height) {592 _screen.SetContent(xPos, yPos, rs[0], rs[1:], style)594 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)595 }594 lx += runewidth.StringWidth(string(rs))596 lx += util.StringWidth(string(rs))597 }598 w.lastX += lx599}600
20 unmodified lines
621 StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).622 Italic(a&Attr(tcell.AttrItalic) != 0)623624 gr := uniseg.NewGraphemes(text)625Loop:626 for gr.Next() {627 st := style628 rs := gr.Runes()629 if len(rs) == 1 {630 r := rs[0]631 switch r {632 case '\r':633 st = style.Dim(true)634 rs[0] = '␍'625 if len(rs) == 1 && rs[0] == '\n' {626 w.lastY++627 w.lastX = 0628 lx = 0629 continue635 case '\n':636 w.lastY++637 w.lastX = 0638 lx = 0639 continue Loop640 }641 }642643 // word wrap:644 xPos := w.left + w.lastX + lx
8 unmodified lines
653 if yPos >= (w.top + w.height) {654 return FillSuspend655 }656646 _screen.SetContent(xPos, yPos, rs[0], rs[1:], style)647 lx += runewidth.StringWidth(string(rs))657 _screen.SetContent(xPos, yPos, rs[0], rs[1:], st)658 lx += util.StringWidth(string(rs))659 }660 w.lastX += lx661 if w.lastX == w.width {662 w.lastY++
96 unmodified lines
modified:src/util/util.go+7 added-3 removed
9 unmodified lines
10 "github.com/mattn/go-runewidth"11 "github.com/rivo/uniseg"12)1314// StringWidth returns string width where each CR/LF character takes 1 column15func StringWidth(s string) int {16 return runewidth.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")17}1819// RunesWidth returns runes width20func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {21 width := 022 gr := uniseg.NewGraphemes(string(runes))
3 unmodified lines
26 var w int27 if len(rs) == 1 && rs[0] == '\t' {28 w = tabstop - (prefixWidth+width)%tabstop29 } else {25 s := string(rs)26 w = runewidth.StringWidth(s) + strings.Count(s, "\n")30 w = StringWidth(string(rs))31 }32 width += w33 if width > limit {34 return width, idx
9 unmodified lines
44 width := 045 gr := uniseg.NewGraphemes(input)46 for gr.Next() {47 rs := gr.Runes()44 w := runewidth.StringWidth(string(rs))48 w := StringWidth(string(rs))49 if width+w > limit {50 return runes, width51 }52 width += w
127 unmodified lines
modified:src/terminal.go+3 added-3 removed
750 unmodified lines
751 runes := []rune(text)752753 // Simpler printer for strings without ANSI colors or tab characters754 if colors == nil && strings.IndexRune(str, '\t') < 0 {755 length := runewidth.StringWidth(str)755 length := util.StringWidth(str)756 if length == 0 {757 return nil, 0758 }759 printFn := func(window tui.Window, limit int) {
654 unmodified lines
1414 case infoInline:1415 pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11416 str := t.infoSep1417 maxWidth := t.window.Width() - pos1418 width := runewidth.StringWidth(str)1418 width := util.StringWidth(str)1419 if width > maxWidth {1420 trimmed, _ := t.trimRight([]rune(str), maxWidth)1421 str = string(trimmed)1422 width = maxWidth
526 unmodified lines
1949 if len(rs) == 1 && rs[0] == '\t' {1950 w = t.tabstop - l%t.tabstop1951 strbuf.WriteString(strings.Repeat(" ", w))1952 } else {1953 w = runewidth.StringWidth(str)1953 w = util.StringWidth(str)1954 strbuf.WriteString(str)1955 }1956 l += w1957 }
1649 unmodified lines
modified:test/test_go.rb+3 added-3 removed
1928 unmodified lines
1929 end19301931 def test_keep_right1932 tmux.send_keys "seq 10000 | #{FZF} --read0 --keep-right", :Enter1933 tmux.until { |lines| assert lines.any_include?('9999 10000') }1933 tmux.until { |lines| assert lines.any_include?('9999␊10000') }1934 end19351936 def test_backward_eof1937 tmux.send_keys "echo foo | #{FZF} --bind 'backward-eof:reload(seq 100)'", :Enter
868 unmodified lines
2806 tmux.prepare2807 tmux.send_keys 'C-r'2808 tmux.until { |lines| assert_equal '>', lines[-1] }2809 tmux.send_keys 'foo bar'2810 tmux.until { |lines| assert lines[-3]&.end_with?('bar"') }2810 tmux.until { |lines| assert lines[-3]&.match?(/bar"␊?/) }2811 tmux.send_keys :Enter2812 tmux.until { |lines| assert lines[-1]&.end_with?('bar"') }2812 tmux.until { |lines| assert lines[-1]&.match?(/bar"␊?/) }2813 tmux.send_keys :Enter2814 tmux.until { |lines| assert_equal %w[foo bar], lines[-2..] }2815 end2816
350 unmodified lines
modified:CHANGELOG.md+11 added-1 removed
11 unmodified lines
1213 # Say hello14 curl "localhost:$(cat /tmp/fzf-port)" -d 'preview:echo Hello, fzf is listening on $FZF_PORT.'15 ```16- Bug fixes16- A carriage return and a line feed character will be rendered as dim ␍ and17 ␊ respectively.18 ```sh19 printf "foo\rbar\nbaz" | fzf --read0 --preview 'echo {}'20 ```21- fzf will stop rendering a non-displayable characters as a space. This will22 likely cause less glitches in the preview window.23 ```sh24 fzf --preview 'head -1000 /dev/random'25 ```26- Bug fixes and improvements27280.38.029------30- New actions
1558 unmodified lines