Skip to content

Fix color support on Windows via VT sequences #31491

@davidanthoff

Description

@davidanthoff

This is a follow up on #7267 (comment). The issue is that right now that @KristofferC's https://github.com/KristofferC/Crayons.jl package doesn't print colors properly on Windows, because the julia process on Windows doesn't configure proper VT support on newer versions of Windows 10.

I'm sketching what I found so far, but don't have the time to fix this. I believe this needs to be fixed somewhere either in libuv or julia, not sure. But maybe what I found so far is helpful for doing that.

I started out with starting julia on Windows 10 via the start menu link. I then ran:

julia> hOutput = ccall(:GetStdHandle, Int32, (Int32,), -11)
88

julia> dwMode = Ref{UInt32}(0)
Base.RefValue{UInt32}(0x00000000)

julia> ccall(:GetConsoleMode, Int32, (Int32, Ptr{Nothing}), hOutput, dwMode)
1

julia> dwMode[]
0x00000003

The first thing to note is that by default the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is not set for this process, which means that none of the new VT support in Windows 10 is active.

Alright, lets set that flag (the value of it is 0x004):

julia> newMode = dwMode[] | 0x0004
0x00000007

julia> ccall(:SetConsoleMode, Int32, (Int32, Int32), hOutput, newMode)
1

If I now call

julia> using Crayons

julia> Crayons.test_256_colors(false)

things still don't look good. BUT, if I do the following, I get the correct output:

julia> buf = IOBuffer()

julia> Crayons.test_256_colors(buf, false)

julia> tr_buf = transcode(UInt16, String(take!(buf)))

julia> ccall(:WriteConsoleW, Int32, (Int32, Ptr{UInt8}, Int32, Ptr{Nothing}, Ptr{Nothing}), hOutput, tr_buf , length(tr_buf ), C_NULL, C_NULL)

I'm not sure what is going on, but it looks like somehow that print and friends don't send that string in such a way to the console that the VT sequences make it there. Maybe libuv is intercepting these sequences in some form? I have no idea, but clearly whatever print is doing is preventing things from working as they should in this case.

I believe ENABLE_VIRTUAL_TERMINAL_INPUT is also not set by default. I don't think that matters for the color example, but it probably means that some other VT feature that is in principle supported on Windows is not active when julia runs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions