Module pl.pretty
Pretty-printing Lua tables.
Also provides a sandboxed Lua table reader and a function to present large numbers in human-friendly format.
Dependencies: pl.utils , pl.lexer
Functions
| read (s) | read a string representation of a Lua table. |
| load (s, env, paranoid) | read a Lua chunk. |
| write (tbl, space, not_clever) | Create a string representation of a Lua table. |
| dump (t, ...) | Dump a Lua table out to a file or stdout. |
| number (num, kind, prec) | format large numbers nicely for human consumption. |
Functions
- read (s)
-
read a string representation of a Lua table.
Uses load(), but tries to be cautious about loading arbitrary code!
It is expecting a string of the form ‘{…}’, with perhaps some whitespace
before or after the curly braces. A comment may occur beforehand.
An empty environment is used, and
any occurance of the keyword ‘function’ will be considered a problem.
in the given environment – the return value may be
nil.Parameters:
s:{string} string of the form ‘{…}’, with perhaps some whitespace
before or after the curly braces.
Returns:
-
a table
- load (s, env, paranoid)
-
read a Lua chunk.
Parameters:
s: Lua codeenv: optional environmentparanoid: prevent any looping constructs and disable string methods
Returns:
-
the environment
- write (tbl, space, not_clever)
-
Create a string representation of a Lua table.
This function never fails, but may complain by returning an
extra value. Normally puts out one item per line, using
the provided indent; set the second parameter to ‘’ if
you want output on one line.
Parameters:
tbl: {table} Table to serialize to a string.space:{string} (optional) The indent to use.
Defaults to two spaces; make it the empty string for no indentationnot_clever:{bool} (optional) Use for plain output, e.g {[‘key’]=1}.
Defaults to false.
Returns:
- a string
- a possible error message
- dump (t, ...)
-
Dump a Lua table out to a file or stdout.
Parameters:
t: {table} The table to write to a file or stdout....:{string} (optional) File name to write too. Defaults to writing
to stdout.
- number (num, kind, prec)
-
format large numbers nicely for human consumption.
Parameters:
num: a numberkind: one of ‘M’ (memory in KiB etc), ‘N’ (postfixes are ‘K’,‘M’ and ‘B’) and ’T' (use commas as thousands separator)prec: number of digits to use for ‘M’ and ‘N’ (default 1)