colored-rs / colored

(Rust) Coloring terminal so simple you already know how to do it !

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work on windows when binary is installed with cargo install

cmontella opened this issue · comments

Using v1.9.3.

I'm not sure how to debug this. I'm on Windows 10 using the command prompt. When I build my app from source, colored works as expected. But if I cargo install my program from crates.io (from the same source), I get output like this:

 �[90mHello�[0m

term_painter works as expected from either source or cargo install. Running from cargo install for Linux and Mac work fine. Any ideas why this would be happening?

Hi @cmontella, can you please link the crate from crates.io and github?

Here's an example of where I used colored: https://github.com/mech-lang/mech/blob/master/src/bin/mech.rs#L400

Here is the source that was published to crates.io: https://github.com/mech-lang/mech/releases/tag/v0.0.4
Here is the crate where I cargo install and see this output: https://crates.io/crates/mech

This is what I see when I run the binary that was generated from crates.io
Capture
The bright blue there is generated by term_painter. I was in the process of moving over to colored because I like the interface better.

Here's what it looks like when I run the binary compiled from my machine:
Capture

Hi @cmontella,

I am not exactly sure why it would be different between the version on crates.io and the source code, however if you run the executable that is produced by building the source (rather than running it through cargo run) you will get the messy output. This leads me to believe that cargo is setting the terminal to virtual, and giving you a red herring result.

In any case, as you are running it through windows you need to enable the virtual terminal state before writing to stdio. Please see https://docs.rs/colored/1.9.3/x86_64-pc-windows-msvc/colored/control/fn.set_virtual_terminal.html for the function. Enabling this early on will give you the result you desire.

I see, yeah cargo is probably doing this for its own color. This has fixed it thanks!

Ohmigosh! I was literally tearing my hair out wondering if was my Windows environment or not that was providing weird output when I ran from Powershell and Command Prompt!

I provided output of what I was seeing when I did cargo install --path . in my case below:

image

As noted above, colored output showed as perfectly when I did just cargo run instead. I had no idea what was causing it, but glad I found this thread. I actually put up with it for about two weeks, but I didn't really understand what was causing the strange issue I noticed; also the discrepancy in colored output between cargo r and just running the binary executable file was driving me mad quite nearly.

As to the actual resolution -- I'm glad to know that it was just as simple as calling an init function at the start of my program. Seems easy enough to add, now that I think about it!


PS: @kurtlawrence not sure if I'm pinging the right person, but it would be nice if this was also mentioned in the colored docs -- not sure if it is already, do apologize if so. I think it would go a long way to helping out others (like me) who would be confused and not know how to resolve issues in Windows environments in such cases 😅