tut icon indicating copy to clipboard operation
tut copied to clipboard

Use pretty printing library such as pprint

Open julien-truffaut opened this issue 10 years ago • 12 comments

It is sometimes difficult to read the output of the REPL, could it be possible to use something like pprint

julien-truffaut avatar Dec 13 '15 20:12 julien-truffaut

The issue here (as discussed on gitter, repeated here for the record) is that tut just hooks into an IMain instance which just spits stuff to an output stream; we never see the results of an evaluation. So to do this we would need to use Ammonite or the script engine or something. Worth keeping in mind if we do a rewrite.

tpolecat avatar Dec 13 '15 21:12 tpolecat

Can it be reimplemented on top of Ammonite? That seems to pretty print values pretty well. I don't think it pretty prints types yet, but it might be a step in the right direction.

Edit: Ammonite uses a separate pretty printing library, so one wouldn't necessarily need to rely on the whole codebase. However, I tinkered with this tuff a while back and I found it easier to use Ammonite's wrapped IMain than raw IMain.

davegurnell avatar Dec 29 '15 14:12 davegurnell

I tried Ammonite a couple times and it blew up (here and here) so I don't think it's quite ready to deal with the kinds of types that I need to support for my personal work. But this may just be a matter of contributing some fixes; I haven't looked at the implementation at all.

I also am somewhat hesitant to change the look and feel because the intent is that tut should present realistic REPL interactions. Maybe there is some kind of middle ground.

This issue is related to #33.

tpolecat avatar Dec 29 '15 15:12 tpolecat

Understood. As you know, @d6y, @noelwelsh and I are interested in using tut for our books at Underscore, so we're a bit of an odd customer. We'd be happy if we could write a thin wrapper around the core library, to customise the output without messing up the out-of-the-box behaviour.

Edit: Having tried to work directly with IMain, I think there's a worthwhile project simply providing a functional wrapper that gives control over input and output without having to capture stdout and so on. I haven't looked at the tut codebase in detail, but if that abstraction is there we could definitely make use of it.

davegurnell avatar Dec 29 '15 15:12 davegurnell

I'm poking at letting Tut (optionally) use an ammonite repl instead of IMain; the two ammonite issues cited above seem long fixed.

Tut's interactions with IMain also seem quite minimal, so I'm hopeful this is possible / not too hard.

ryan-williams avatar Mar 15 '18 17:03 ryan-williams

Turns out I was mistaken in thinking that Ammonite/PPrint would support customizable pretty-printing for various types (https://github.com/lihaoyi/PPrint/issues/11), which somewhat saps my motivation to work on this 🙁

ryan-williams avatar Mar 15 '18 20:03 ryan-williams

At the risk of sounding like a crazy person, making the compiler itself pluggable in this regard seems like a better strategy. We just need to nerdsnipe someone into figuring out how to do it.

tpolecat avatar Mar 15 '18 21:03 tpolecat

That sounds good… but at the risk of sounding like a crazier person, I am going to work in parallel on a version of tut that e.g. wraps lines in println(_.show), using customizable cats.Show instances, possibly based on ammonite since it seems to offer better control over some of its output/error/result streams…

ryan-williams avatar Mar 15 '18 21:03 ryan-williams

👍 You can do better with something like trait TPrint[T] { val toString: String } and then tshow[T: TPrint: Show](t: T): String = ... which is kind of what Ammonite used to do.

tpolecat avatar Mar 15 '18 21:03 tpolecat

Making sure I'm following: you've formalized customizable display of both the type and value, piggy-backing on PPrint's existing TPrint type-class for the former, and using cats.Show for the latter?

ryan-williams avatar Mar 15 '18 21:03 ryan-williams

Yeah just saying you can derive pretty-printers for types too. Whether with Ammonite/PPrint or without.

tpolecat avatar Mar 15 '18 21:03 tpolecat

Taking a break from this for today; here's a branch with some minimal progress:

ryan-williams avatar Mar 15 '18 23:03 ryan-williams