hoelzro / lua-repl

A Lua REPL implemented in Lua for embedding in other programs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create a formatting plugin

hoelzro opened this issue · comments

Sometimes, when I'm working with the REPL, I would like specific formatting to applied to different types. For example, instead of seeing this:

> math.pi
3.1415926535898

I might prefer this:

> math.pi
3.14

Or instead of this:

> 0x1000 - 1
4095

I might prefer this:

> 0x1000 - 1
0xFFF

Another example:

> 2 ^ 32
4,294,967,296

ilua offers something like this, and I definitely think this functionality should be available in lua-repl. Of course, the implementation of this plugin raises some questions:

  • We need to detect number separator (, vs .) from a user's locale, perhaps falling back to a suitable default if we can't.
  • We need to detect if numbers are typically separated into thousands, hundreds, ten-thousands.
  • We need to provide a way for a user to configure this plugin (in case he/she would like to have numbers rounded to the tens, or the thousands, etc)
  • We should provide a way to toggle this at runtime (in case you want to turn it on/off temporarily)
  • We should be able to alter the configuration at runtime (in case you want to change the precision, or stop printing numbers in hexidecimal, etc)

Other nice-to-haves:

  • Allow the user to specify rules to figure out which formatting options to use. For example, if I detect that a user entered an expression involving math on a literal specified in hexidecimal (ex. 0x1000 - 1), they should be able to tell the REPL to display the result in hexidecimal format.
  • Such a plugin could possibly interfere with other printing plugins (ex. pretty_print), so maybe we should have a "stack" of formatters so they can leverage each other. We might even want a "format level" on formatting plugins so that their relative order doesn't screw things up. Instead of an explicit stack, we could provide a format_result method that must return a string; formatting plugins can use the around plugin object to fallback on "lower level" formatters.