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.

Functions

read (s) read a string representation of a Lua table.
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. An empty environment is used, and any occurance of the keyword 'function' will be considered a problem.

Parameters:

  • s:

    {string} string of the form '{...}', with perhaps some whitespace

        before or after the curly braces.
    
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.
    
  • not_clever:

    {bool} (optional) Use for plain output, e.g {['key']=1}.

        Defaults to false.
    

Returns:

  1. a string
  2. 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 number
  • kind: 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)
generated by LDoc 1.2