MilesCranmer / SymbolicRegression.jl

Distributed High-Performance Symbolic Regression in Julia
https://astroautomata.com/SymbolicRegression.jl/dev/
Apache License 2.0
600 stars 77 forks source link

Colorful printing #181

Open MilesCranmer opened 1 year ago

MilesCranmer commented 1 year ago

It could be nice to have colorful printouts of equations, with colored parentheses indicating level of nestedness, similar to OhMyREPL.jl.

@khdlr do you know if ProgressBars.jl set_multiline_postfix would support colors?

khdlr commented 1 year ago

Currently, ANSI escape codes will already work, e.g.

iter = ProgressBar(1:100)
for i in iter
  # ...
  set_multiline_postfix(iter, "\u001b[31mred: \u001b[32mgreen\u001b[37m")
end

However, this is not a great API to program around and might not work on windows platforms either. I guess the best way to go about this is for ProgressBars.jl to also accept a Crayon instead of a string for methods like set_postfix, set_multiline_postfix, etc.

Just realized that Crayons.jl is by the same author as OhMyREPL.jl, so it's likely what they are using there as well.

MilesCranmer commented 1 year ago

Awesome. Thanks! I didn't realize it would be that simple. I'm happy with that as-is, it seems like this works well and isn't much work at all to interface:

for i in iter
    set_multiline_postfix(iter, "$(Crayon(foreground=:red))In red: $i$(Crayon(reset=true))")
    sleep(0.01)
end

It will be cool to have different colors for each column of the output and also for nested expressions!

khdlr commented 1 year ago

Okay that's convenient, I guess we won't need a new API from the ProgressBars.jl side for that then.

The ANSI escapes will currently mess up the character-counting for the multiline_postfix:

for i in iter
    set_multiline_postfix(iter, "$(Crayon(foreground=:red))In red: $i$(Crayon(reset=true))\nsecond line")
    sleep(0.01)
end

Currently working on fixing that with some regex-magic :smiley:

khdlr commented 1 year ago

Just released v1.5.0 that fixes colorful multiline postfixes

MilesCranmer commented 1 year ago

That was fast 🤯 nice work!!