lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 23.02.2014 15:14 schröbte Andrew Starks:
On Sunday, February 23, 2014, Enrico Colombini <erix@erix.it> wrote:

On 23/02/2014 13.37, steve donovan wrote:

That's nice to know! In the meantime, have a look at ilua

https://github.com/ilua/ilua

which uses an exploratory compile trick to remove the restriction. It
also has a default pretty-printer which is good enough for most
purposes.


A basic table printer that could perhaps be useful in the standalone
interpreter could be:

- first print all the entries from 1 to #t using ipairs()
- mark the last one with '#'
- then print all entries with pairs() except those printed before

For example, this table:

{ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday';
   [27] = 'payday', [100] = { 1, 2, 3 },
   monday = 1, tuesday = 2, wednesday = 3, thursday = 4, friday = 5 }

could print like this:

1 = 'monday'
2 = 'tuesday'
3 = 'wednesday'
4 = 'thursday'
5 = 'friday'
#
'wednesday' = 3
'monday' = 1
100 = table: 00468538
'friday' = 5
'thursday' = 4
27 = 'payday'
'tuesday' = 2

If declared as a function (e.g. __table_printer()) the table printer would
be replaceable with the usual command line options, or possibly disabled
with a new option.

As an alternative, a sign different from '=' could be used to invoke the
table printer, or generally speaking a more informative printer.
(I don't dare venturing into table recursion territory)

P.S. I'm still trying to decide if I like this. Probably not much: it just
saves a few keys from calling a table printer loaded via LUA_INIT.

--
   Enrico


A small suggestion:

Make the output legal lua code.
Example: "--#"

Not quite as pretty, but not only do I sometimes munch pretty output, it
might even be clearer for new programmers? It is not as aesthetically
pleasing, however.


I would also prefer a Lua-like syntax, and I suggest the following modifications to the original proposal:

* Count the sequence elements via `ipairs()`, but only display N of them in case there are more than that (maybe some from the sequence end, the rest starting at index 1) * Then print at most N values using `pairs()` but skip integer keys between 1 and the sequence end determined above.
*   Write out a marker (e.g. `...`) unless you display all elements.
* Make N a global variable so that you can change the number of displayed elements in case the default is not good enough.
*   Abbreviate long keys/values, escape non-printable characters ;-).
* Try to fit multiple key-value pairs on a single line, but wrap around at say 80 characters.
*   Respect `__tostring`, but indicate original type.


Philipp