JuliaLogging / TerminalLoggers.jl

Logging sinks and utilites for interactive terminals
MIT License
41 stars 10 forks source link

Markdown formatting for AbstractString log messages #23

Closed c42f closed 4 years ago

c42f commented 4 years ago

Fixes #16.

As suggested there, I'm now using show(io, MIME"text/plain"(), message) by default (except for strings, which go through Markdown.parse() first. With that in mind it might be sensible to tweak the API requirements for message again, revisiting whether show or string are used.

tkf commented 4 years ago

It's unfortunate that multi-line highlighting does not work...

image

I guess an easy solution may be to tweak Markdown.jl so that it prints ANSI escape code for each line?:

julia> sprint(
           show,
           "text/plain",
           Markdown.parse("`" * "aaa "^20 * "`"),
           context = IOContext(stdout, :displaysize => (30, 30)),
       )
"  \e[36maaa aaa aaa aaa aaa aaa\n  aaa aaa aaa aaa aaa aaa\n  aaa aaa aaa aaa aaa aaa\n  aaa aaa\e[39m"

It looks like printstyled is doing this:

julia> sprint(io -> printstyled(io, " AAA \n BBB \n CCC", color = :red), context = :color => true)
"\e[31m AAA \e[39m\n\e[31m BBB \e[39m\n\e[31m CCC\e[39m"

So it's probably that Markdown.jl is not using printstyled?

c42f commented 4 years ago

It's unfortunate that multi-line highlighting does not work

Yes this sure is annoying. Working around it seems to require ANSI code parsing and having a model of the terminal's internal color state. At that point it might be better to give up and fix upstream + vendor Markdown.term() though.

Thanks for the review!