Pluto.jl icon indicating copy to clipboard operation
Pluto.jl copied to clipboard

ANSI color code rendering in output

Open pfitzseb opened this issue 4 years ago β€’ 10 comments

Would be cool to render ANSI color codes in text (and maybe even rich) output.

pfitzseb avatar Feb 04 '21 14:02 pfitzseb

Do you have an example package for us to test? Since we don't support stdout, I'm not sure if there's enough reason to add it

fonsp avatar Feb 08 '21 11:02 fonsp

IIRC this came up on Slack with a reference to MLJ.jl, but technically outputting ANSI color codes in show methods without the user/IOContext asking for it is wrong. Would still be kinda neat though, imho :)

pfitzseb avatar Feb 08 '21 11:02 pfitzseb

Hmmmmm but we also have HTML! Who wants ANSI colors if you can have <marquee> and font-family: "Comic Sans MS"? ☺️

fonsp avatar Feb 08 '21 11:02 fonsp

True true. This is only relevant for code ignorant of the (questionable) beauty of HTML ;)

pfitzseb avatar Feb 08 '21 12:02 pfitzseb

I don't think it's worth the effort! But counterexamples are welcome

fonsp avatar Mar 04 '21 23:03 fonsp

https://github.com/fonsp/Pluto.jl/issues/983

fonsp avatar Apr 07 '21 06:04 fonsp

So, if you are writing applications that normally work with the terminal, ANSI colours are rather obvious thing to do. Converting pre-formatted text + ANSI colours to HTML, like Jupyter does, seems like an obvious gap that Pluto has. Asking each library to write their own HTML formatter to get colour seems... like the wrong separation of concerns.

...

Here is concrete use case for FunSQL.jl -- when you use the console, in the display of q you'll see that the 1st Get.person_id is coloured blue. In the display of render(q) you'll notice that the second Get.person_id is highlighted in red. In both cases, the output has indentation which is helpful for readability.

(@v1.6) pkg> add FunSQL

julia> using FunSQL: From, Select, Get, Highlight, SQLTable, render

julia> person = SQLTable(:person, columns = [:person_id])
SQLTable(:person, columns = [:person_id])

julia> q = From(person) |> 
           Select(Get.person_id |> Highlight(:blue), Get.person_id)
let person = SQLTable(:person, …),
    q1 = From(person),
    q2 = q1 |> Select(Get.person_id, Get.person_id)
    q2
end

julia> render(q)
ERROR: DuplicateAliasError: person_id in:
let person = SQLTable(:person, …),
    q1 = From(person),
    q2 = q1 |> Select(Get.person_id, Get.person_id)
    q2
end
Stacktrace:
...

If you type this same into Pluto, you'll notice that the display of q is indented but that the blue highlight for the 1st Get.person_id is missing. In the display of render(q) you'll notice that indentation is lost, and there is no red highlighting of the 2nd Get.person_id.

clarkevans avatar Apr 07 '21 12:04 clarkevans

Take a peek at how we supported colour in the package installation terminal: https://github.com/fonsp/Pluto.jl/blob/main/frontend/components/PkgTerminalView.js

fonsp avatar Sep 07 '21 16:09 fonsp

I found this while using programs designed for the terminal, distributed in the Julia ecosystem as jll packages. For example:

# ╔═║ 4c4d8eaa-87ae-4f2b-8f33-4cced2cc824c
using FastME_jll

# ╔═║ 0505615f-bf25-4316-923e-eb4dcef34129
fastme() do _fastme
	run(`$_fastme -h`)
end

Here the problem is not only color code but also bold character and other things I have no idea about: Pluto: image

Terminal:

image

diegozea avatar Jun 01 '22 16:06 diegozea

Adding a vote for terminal color support. For user experience, there are only positives:

  • Enabling colors when packages implement terminal colors but not text/html
  • Avoiding garbled display when packages don't check the IOContext as they should (packages should be fixed too, but this would be a pragmatic and harmless band-aid)

As an example of the latter, here's BifurcationKit.jl; compare the raw output to the one that's captured from stdout using print (I'll try to make a PR to BifurcationKit):

image

danielwe avatar Nov 23 '23 01:11 danielwe