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

Colour output doesn't work in cmd on Windows #749

Closed
garyb opened this issue Mar 7, 2021 · 14 comments
Closed

Colour output doesn't work in cmd on Windows #749

garyb opened this issue Mar 7, 2021 · 14 comments
Labels

Comments

@garyb
Copy link
Member

garyb commented Mar 7, 2021

It just prints the colour codes as text:

←[34m[info] ←[0mInstalling 65 dependencies.←[0m
←[34m[info] ←[0mSearching for packages cache metadata..←[0m
←[34m[info] ←[0mUnable to find packages cache metadata, downloading from GitHub..←[0m
←[34m[info] ←[0mInstalling "exists"←[0m
←[34m[info] ←[0mInstalling "distributive"←[0m

Perhaps related to #729, but the suggestion there is there's a regression in an upstream library, and it's been this way for as long as I've been using spago. That doesn't necessarily rule out the upstream regression, as I came to it late!

@garyb garyb added the bug label Mar 7, 2021
@garyb
Copy link
Member Author

garyb commented Mar 7, 2021

I probably should have mentioned, this isn't true for all commands. The [info] highlight works correctly for builds, for example.

@f-f
Copy link
Member

f-f commented Mar 7, 2021

The [info] highlight works correctly for builds, for example.

Oh, that's funny then - if all color was wrong then we'd know that there's something wrong with the lway we do logging, but if only some coloring is wrong then I am not really sure how to even debug this. Do you have any ideas? We're using the logging machinery from RIO

@garyb
Copy link
Member Author

garyb commented Mar 7, 2021

Ah! I think it's something that purs is doing that makes it work actually. Any command that invokes the compiler has colour codes working correctly after "Compiling..." messages have been printed. spago docs is a perfect example actually, because it does this kind of thing:

←[34m[info] ←[0mGenerating documentation for the project. This might take a while...←[0m
Compiling documentation for Data.NaturalTransformation
Compiling documentation for Unsafe.Coerce
Compiling documentation for Record.Unsafe
Compiling documentation for Type.Proxy
... snip ...
Documentation written to: generated-docs/html
[info] Docs generation succeeded.
[info] Making the documentation searchable...

So before the compiler is invoked: no colour, after, colour.

@garyb
Copy link
Member Author

garyb commented Mar 7, 2021

I have an idea of something to try 🙂

@f-f
Copy link
Member

f-f commented Mar 7, 2021

Oh I see, that makes sense. Note that we just disable ANSI codes when TERM=win:

-- https://github.com/purescript/spago/issues/579
maybeTerm <- Env.lookupEnv "TERM"
let termDumb = maybeTerm == Just "dumb" || maybeTerm == Just "win"
let useColor = globalUseColor && not termDumb

..but maybe we should just disable it when running on Windows at all?

@stkb
Copy link
Contributor

stkb commented Mar 22, 2021

Yeah would nice to get this fixed. I've tried to do some investigating but it's quite laborious with the little knowledge I have.

I've found though that if you just open start a stack repl in the Spago folder, and then do System.IO.putStrLn "\ESC[31mRED\ESC[0m", then it works. But if you do that within the Spago app itself, it doesn't.

Also about TERM=win, I'm not sure what environments exactly that's for? Because that variable isn't set in vanilla Windows.

@garyb
Copy link
Member Author

garyb commented Mar 22, 2021

I've not yet managed to determine what the difference is between purs and spago that means it works in one place, and not the other, and that invoking purs inside spago will fix it from that point on too.

I was looking into how stdio was configured with utf-8 support, etc. to see if there was something there as it seems like it must be something along those lines, but I've not gotten back to it to try again yet. I'm probably going to try in a fresh CLI app entirely and see if it works "out of the box", and if not, I can maybe figure something out in there then take it back to spago.

@stkb
Copy link
Contributor

stkb commented Mar 22, 2021

I'm probably going to try in a fresh CLI app entirely and see if it works "out of the box"

Yeah that was just what I was trying next, just with stack new testproject and it does indeed work fine there too. But apart from that I'm completely in the dark.

@garyb
Copy link
Member Author

garyb commented Mar 22, 2021

Interesting! When I hacked spago to the point of printing a colour-coded line from its main before it does anything else (before withUtf8 even), it still wasn't displaying correctly.

@stkb
Copy link
Contributor

stkb commented Mar 22, 2021

with stack new testproject and it does indeed work fine there too.

Ok no I have to correct that. It's when running the exe through stack exec that the colors work. Works for a new project, works for Spago. When just running the .exe from the command line, it doesn't (also for a fresh project)

@f-f
Copy link
Member

f-f commented Apr 7, 2021

Could it be because the compiler calls IO.hSetBuffering IO.stdout IO.LineBuffering but we don't?

@garyb
Copy link
Member Author

garyb commented Apr 7, 2021

Replicating the IO setup there was the first thing I tried when I hacked my local spago 😄, I tried doing all that explicitly and avoiding withUtf8, and variations on all that without much luck. I really thought it would be the LineBuffering thing when I saw that.

@stkb
Copy link
Contributor

stkb commented Apr 7, 2021

That didn't work but I think it lead me to the right answer :) Just preparing a PR...

stkb added a commit to stkb/spago that referenced this issue Apr 7, 2021
Calling the `hSupportsANSIWithoutEmulation` function on Windows 10+
enables terminal colors where supported.
https://hackage.haskell.org/package/ansi-terminal-0.11/docs/System-Console-ANSI.html#v:hSupportsANSIWithoutEmulation

Also has the added benefit that (on Windows), the ANSI codes aren't
present when output is redirected to a text file.
stkb added a commit to stkb/spago that referenced this issue Apr 7, 2021
Calling the `hSupportsANSIWithoutEmulation` function on Windows 10+
enables terminal colors where supported.
https://hackage.haskell.org/package/ansi-terminal-0.11/docs/System-Console-ANSI.html#v:hSupportsANSIWithoutEmulation

Also has the added benefit that (on Windows), the ANSI codes aren't
present when output is redirected to a text file.
stkb added a commit that referenced this issue Apr 10, 2021
Fixes colors on Windows (#749), by calling the `hSupportsANSIWithoutEmulation` function, which enables terminal colors on Windows 10+ (https://hackage.haskell.org/package/ansi-terminal-0.11/docs/System-Console-ANSI.html#v:hSupportsANSIWithoutEmulation)

Additionally, using this function in the way we do here means colors are automatically disabled when output is redirected to a file. Test fixtures are modified to reflect this.

`hSupportsANSIWithoutEmulation` should also check for the `TERM=dumb` environment variable, but it doesn't on Windows, and on *nix this hasn't been tested, so the existing code here that does this check has been left in for now.

Finally, support is added for the https://no-color.org spec with a simple check for a `NO_COLOR` environment variable.
@stkb
Copy link
Contributor

stkb commented Apr 10, 2021

Oh I thought this would be closed when #768 was merged. I guess because I only mentioned the issue in the commit message body and not the first line. Anyway, closing now

@stkb stkb closed this as completed Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants