From f57920ad903105381b02502580be2bb11e4e6714 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 12 Apr 2018 17:49:52 +0900 Subject: [PATCH] Do not print non-displayable characters fzf used to print non-displayable characters (ascii code < 32) as '?', but we will simply ignore those characters with this patch, just like our terminals do. \n and \r are exceptions. They will be printed as a space character. TODO: \H should delete the preceding character, but this is not implemented. Related: #1253 --- src/tui/light.go | 14 ++++++++------ src/util/util.go | 9 +++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/tui/light.go b/src/tui/light.go index 7b474181..a96a8440 100644 --- a/src/tui/light.go +++ b/src/tui/light.go @@ -48,11 +48,13 @@ func (r *LightRenderer) stderrInternal(str string, allowNLCR bool) { runes := []rune{} for len(bytes) > 0 { r, sz := utf8.DecodeRune(bytes) - if r == utf8.RuneError || r < 32 && - r != '\x1b' && (!allowNLCR || r != '\n' && r != '\r') { - runes = append(runes, '?') - } else { - runes = append(runes, r) + nlcr := r == '\n' || r == '\r' + if r >= 32 || r == '\x1b' || nlcr { + if r == utf8.RuneError || nlcr && !allowNLCR { + runes = append(runes, ' ') + } else { + runes = append(runes, r) + } } bytes = bytes[sz:] } @@ -807,7 +809,7 @@ func (w *LightWindow) Print(text string) { } func cleanse(str string) string { - return strings.Replace(str, "\x1b", "?", -1) + return strings.Replace(str, "\x1b", "", -1) } func (w *LightWindow) CPrint(pair ColorPair, attr Attr, text string) { diff --git a/src/util/util.go b/src/util/util.go index 867935ac..95c4e1b4 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -17,11 +17,12 @@ func RuneWidth(r rune, prefixWidth int, tabstop int) int { return tabstop - prefixWidth%tabstop } else if w, found := _runeWidths[r]; found { return w - } else { - w := Max(runewidth.RuneWidth(r), 1) - _runeWidths[r] = w - return w + } else if r == '\n' || r == '\r' { + return 1 } + w := runewidth.RuneWidth(r) + _runeWidths[r] = w + return w } // Max returns the largest integer