1
0
An advanced pretty printer, aiming for human readability.
Go to file
2017-06-24 20:37:43 +02:00
test The documentation for builtins will now be wrapped, to prevent extremely long lines. 2017-06-24 20:37:43 +02:00
analyze_structure.lua analyse_structure is not automatically called, if no info regarding a table could be found. 2017-06-24 18:36:01 +02:00
function.lua The documentation for builtins will now be wrapped, to prevent extremely long lines. 2017-06-24 20:37:43 +02:00
init.lua Implemented more advanced utf8 support + moved fractional support to the SPECIAL_NUMBER list. 2017-04-12 14:52:51 +02:00
library.lua Changed project priorities, and removed lots of redundant features from number.lua 2017-06-15 16:30:34 +02:00
number.lua Changed project priorities, and removed lots of redundant features from number.lua 2017-06-15 16:30:34 +02:00
pretty.lua Function formatting will now automatically pull documentation from a lua file, if it knows where to look. 2017-06-24 20:06:36 +02:00
README.md Changed project priorities, and removed lots of redundant features from number.lua 2017-06-15 16:30:34 +02:00
table_type.lua Moved some constants into their own file, more info is stored in l, and improved some marker stuff. 2017-04-03 11:24:51 +02:00

Pretty

Introduction

pretty is an advanced pretty printer for Lua aiming primarily for human readability. This is done by looking for patterns in the input data, and creating an output string utilizing and highlighting those patterns. Thus it's a primarily a debugging tool, not a speedy serialization tool.

Sometimes just aligning elements are enough to easy the burden on the eyes. Contrast the two following pieces of code:

	bad = {
		a = 'hello world',
		hello = 'hi'
	}
	good = {
		a      =  'hello world',
		hello  =  'hi'
	}

This project is the outcome of my frustration with existing pretty printers, and a desire to expand upon the pretty printer I developed for Xenoterm. The default Xenoterm pretty printer is much simpler than pretty, but the enhancements compared to other pretty printers, inspired me to create pretty. See the bottom of the page for other pretty printers.

pretty sorts it's priorities like so:

  1. Human readability.
  2. Lua-compatible output.
  3. Customization support.

I'd rather have good defaults than provide a ton of customization options. And if some structure cannot be represented in Lua, I will rather extend the syntax, than lose the info.

Another aspect where pretty shines is in exploratory programming, when attempting to avoid reliance on outside documentation. The amount of information pretty exposes varies by the data you are inspecting. If you're inspecting a list of functions, their function signatures are visible, but if you're inspecting a single function, documentation and source location may appear if available.

Features

  • Written in good-old pureblood Lua, with support for PUC Lua 5.0 - 5.3 and LuaJIT 2.0 and up.
  • Redefining what it means to be "human readable":
    • Is multi-line centric, to aid readablitiy.
    • Indention and alignment of keys-value pairs.
    • Keys-value pairs are properly sorted by key type and thereafter alphabetically.
    • The format and structure of output changes depending upon the input. Maps appear differently to deeply nested tables to long sequences with short strings to short lists.
    • Uses the standard debug library to gain information about functions and other advanced structures.

Performance

As specified in the introduction, pretty is not a performance oriented library. Expected usage is in error conditions and debugging, not in tight inner loops.

Don't use pretty.lua if you want fast serialization. Use one of the pretty printers specified below.

TODO

I'm looking into implementing following features:

  • Improve display of medium-long lists with short elements better. One option would be to implement something analog to the default results of ls on Linux.
  • Add support for setmetatable, and exploring the values accessible through it.
  • Nice formatting for cdata datatype in LuaJIT.
  • Add possibility of comments in output, for stuff like __tostring methods, and global namespaces like io or math.
  • Better support upvalues for in functions. Complete support is impossible without traversing the original code or inspecting the intermediate representation, due to lexical scoping. (Pluto does it, but it's written in C.)
  • Look more into string.dump in the core library.
  • Look into using concat operation to improve appearance of overly long non-breaking strings. Maybe even attempt to break near whitespace.
  • Attempt to fit output within a predefined width limit. Default to 80(?)
  • Find a better name than pretty.
  • Add options for colored output, and allow custom formatting.

Other pretty printers

pretty is a large and slow library, and is not designed for serialization purposes, nor is pretty concerned with offering the same level of customization as other libraries do.

Luckily Lua has a large library of pretty printers and serialization libraries:

  • inspect.lua: One of the classic debugging pretty printers.
  • pprint.lua: Reimplementation of inspect.lua
  • serpent: Advanced and fast pretty printer.
  • pluto: Can serialize arbitrary parts of Lua, including functions, upvalues, and proper lexical scoping. Not written in native Lua.
  • binser: Library for special purpose serialization.

Others can be found at the lua-users wiki.