-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Color doesn't work in windows mintty (git bash) #117
Comments
Do you know if mintty uses the Windows console for coloring? Or does it use something else? I wonder if |
Here is the raw output from
I tried the same exercise with rg, but couldn't get it to output the escape codes, both in mintty and cmd.exe. It looks like rg doesn't detect terminal output and ignores the color=always options. |
It does detect terminal output, just apparently not on Windows. :-) I think the issue is that I baked an assumption into For cc @retep998 Would you be able to add any insight here? Thanks so much. :-) |
@BurntSushi Basically with mintty you have a pipe instead of a console. Because of this there is no API you can call to determine whether a pipe is actually a terminal that accepts ansi escape codes. The best you can do is that if you're not talking to a console, then check whether Conhost (which is what |
Perhaps the |
@leafgarland That's plausible. Good idea. |
I don't think you really need the user's help deciding which to use. If you're in a Windows console, then the Windows console API will work all the time. If you're not in a Windows console, then the Windows console API will work none of time and the only other option is ansi escape codes. So I'd probably enable color by default in the Windows console, and elsewhere support color using ansi escape codes only if enabled via |
We have the same problem in emacs where ripgrep is apparently not recognizing pseudo emacs terminal (pty) as a terminal, see emacs-helm/helm#1624. |
@thierryvolpiatto Is that on Windows? If not, it sounds like an entirely distinct issue. |
Andrew Gallant notifications@github.com writes:
No, on GNU/Linux.
Ok, should I open an other issue ? Thierry |
@thierryvolpiatto I think #182 might be relevant. |
Ok, yes I just see @chunyang have opened another issue, thanks. |
@retep998 I'm in the process of fixing this issue, and it actually looks like I can successfully get a handle to the Windows console when using |
@BurntSushi |
@retep998 This is the code I'm using to get a console, and it doesn't appear to return an error in pub fn new() -> io::Result<Console> {
let name = b"CONOUT$\0";
let handle = unsafe {
kernel32::CreateFileA(
name.as_ptr() as *const i8,
winapi::GENERIC_READ | winapi::GENERIC_WRITE,
winapi::FILE_SHARE_WRITE,
ptr::null_mut(),
winapi::OPEN_EXISTING,
0,
ptr::null_mut(),
)
};
if handle == winapi::INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
let mut info = unsafe { mem::zeroed() };
let res = unsafe {
kernel32::GetConsoleScreenBufferInfo(handle, &mut info)
};
if res == 0 {
return Err(io::Error::last_os_error());
}
let attr = TextAttributes::from_word(info.wAttributes);
Ok(Console {
handle: handle,
start_attr: attr,
cur_attr: attr,
})
} So I'm not using |
Hmm, I guess the difference here is that in that code above, I am opening |
@BurntSushi Getting |
@retep998 Ah I see now, OK that works! Thanks! My only problem now is figuring out how to detect whether stdout is tty or not inside of mintty. From searching, it seems like the problem is basically unsolveable, although I will note that sigh |
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
For those following this issue but not #94, this will be fixed in the next ripgrep release! |
Colors stopped working in one of the releases, on an ARM target I tested |
Yey, colors work again on that ARM target. |
I can't get rg to output color in mintty, ie the default term that comes with git for windows and MSYS2. Tried
--color always
and-p
, to no avail. Color works fine (by default) in standard cmd.exe.Color (
--color=auto
) works with standard grep in mintty, or at least the grep build that comes with git for windows.The text was updated successfully, but these errors were encountered: