Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

^O characters in coloured output #37

Closed
uazu opened this issue Sep 23, 2016 · 9 comments · Fixed by #240
Closed

^O characters in coloured output #37

uazu opened this issue Sep 23, 2016 · 9 comments · Fixed by #240
Labels
bug A bug.

Comments

@uazu
Copy link

uazu commented Sep 23, 2016

Viewing with --color always piped to less -R, colours appear correctly but there are ^O characters (\x0F) inserted regularly, which appear in less as inverted ^O. When removed with sed 's/\x0f//g' output appears like screenshot.

Tested release binary version 0.1.16 on Debian Linux, with TERM=screen.linux.

@BurntSushi BurntSushi added the bug A bug. label Sep 24, 2016
@BurntSushi
Copy link
Owner

Yup, I can reproduce it. Setting TERM=screen.linux was key.

@BurntSushi
Copy link
Owner

N.B. When I'm in a screen session, if I do TERM=xterm rg -p foo | less -R then things appear to work. When TERM=screen, I see the same ^0 characters as the OP. I wonder if it's a bug in the term library I'm using.

@balta2ar
Copy link

Same here (ArchLinux, tmux 2.2, rg 0.2.1). In tmux my $TERM is screen-256color, but running TERM=xterm rg temp | less helps.

@uazu
Copy link
Author

uazu commented Sep 27, 2016

Yes, I can work around it. ^O is shift-in, i.e. switch to non-graphic character set, so may be part of a generic "reset" sequence. As I need to add --color always to use with less, I need to put a wrapper script around it anyway so I'll add TERM=whatever there. Perhaps it only needs documenting. However if there was a way to just reset colours without also resetting character set that might avoid confusion.

@BurntSushi
Copy link
Owner

@uazu Ah thanks for the explanation. I think other tools seem to work fine, so we should try to fix it. Documenting it as a known bug in the README is also a good idea. By the way, you may also be interested in the -p/--pretty flag. :-)

@Screwtapello
Copy link

According to less(1), the -R option tells less to assume all sequences of the form ESC [ ... m are zero-width, and pass them through verbatim. ^O is not of that form, so less makes it printable. But where does the ^O come from?

Looking at the terminfo database entry for screen, we see:

$ infocmp -1 screen | grep sgr0
    sgr0=\E[m\017,

...so whenever ripgrep calls .reset(), the term crate looks up the sequence associated with sgr0 ("set graphical rendition to 0" i.e. all fancy things turned off), the terminfo database for screen defines sgr0 to be "ESC [ m ^O" and so that's what gets written to the screen.

If there's a bug here, I'd say it's in less for half-assing its escape-sequence passthrough.

@uazu
Copy link
Author

uazu commented Oct 9, 2016

Well, I now have a wrapper around ripgrep which sets TERM=xterm and pipes to less and everything is fine (and I use ripgrep all the time now -- Thanks!). However, maybe using the 'reset' (sgr0) sequence in this application is overkill, since you don't need to reset everything, but rather just the colours. If there is some other terminfo string that can be used for that then the problem is avoided. Otherwise the TERM=xterm workaround needs to be documented, since I'm sure some other people will want to use ripgrep with less on screen.

@Screwtapello
Copy link

sgr0 is definitely the canonical way to do it. And it's not just colours - ripgrep also makes some text bold.

BurntSushi added a commit that referenced this issue Nov 20, 2016
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.

In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:

    rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo

For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.

Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.

Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
@kaushalmodi
Copy link
Contributor

Thanks. I confirm the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants