Use pretty printing library such as pprint
It is sometimes difficult to read the output of the REPL, could it be possible to use something like pprint
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.
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.
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.
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.
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.
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 🙁
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.
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…
👍 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.
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?
Yeah just saying you can derive pretty-printers for types too. Whether with Ammonite/PPrint or without.
Taking a break from this for today; here's a branch with some minimal progress:
- Ammonite
Replcopy-pasted fromTestRepl - a few tests showing ways to display results using custom
cats.Showinstances - half-baked interface over
IMainorRepl- plumbing printstreams through usage sites not done