fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
5.02k stars 293 forks source link

ANSI color code rendering in output #894

Open pfitzseb opened 3 years ago

pfitzseb commented 3 years ago

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

fonsp commented 3 years ago

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

pfitzseb commented 3 years ago

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 :)

fonsp commented 3 years ago

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

pfitzseb commented 3 years ago

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

fonsp commented 3 years ago

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

fonsp commented 3 years ago

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

clarkevans commented 3 years ago

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.

fonsp commented 3 years ago

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

diegozea commented 2 years ago

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

danielwe commented 1 year ago

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

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

MasonProtter commented 1 month ago

Another classic example would be BenchmarkTools.jl which is probably one of the most used packages out there in the ecosystem. image

The lack of colour in the regular display is pretty painful, and the purposefully ugly terminal display for stdout is also a bit hard to look at compared to how it looks in a regularly set up REPL: image

fonsp commented 4 weeks ago

It should be easy to implement! Take a look at how we implemented color in https://github.com/fonsp/Pluto.jl/blob/main/frontend/components/PkgTerminalView.js for inspiration, feel free to make a PR!