Skip to main content

Pretty Printer

Latest Version: 1.0.1

This library ‘pretty prints’ Squirrel objects.

You can view the library’s source code on GitHub. Click here to see information on other versions of this library.

On the device, this library is dependent on the JSONEncoder library. Under an agent, the library makes use of the imp API’s own JSON encoder. How you include the library depends on whether you are using it in agent or device code.

Agent Code

To add this library to your project, add #require "PrettyPrinter.class.nut:1.0.1" to the top of your agent code.

Device Code

To add this library to your project, add #require "PrettyPrinter.class.nut:1.0.1" and #require "JSONEncoder.class.nut:1.0.0" to the top of your device code.

Quick Start

To define a global function, print(), which logs its input without truncation, use the following:

// 'null' to use default string for indentation,
// 'false' to force printing objects in full, without truncation
pp <- PrettyPrinter(null, false);
print <- pp.print.bindenv(pp);

// example usage
::print([1,2,3]);
/*
[
    1,
    2,
    3
]
*/

Class Usage

Constructor: PrettyPrinter([indentString][, truncate])

The PrettyPrinter constructor takes two optional parameters: a string indentString containing the level of indentation you require, and a boolean, truncate, which lets you select whether a long log output should be truncated. The default value for indentString is a string containing four spaces. The default value of truncate is true.

pp <- PrettyPrinter();

Class Methods

format(squirrelObject)

The format() method takes one required parameter: the Squirrel object to be formatted. It returns a string containing the prettifed JSON-encoded version of object.

Note When formatting classes or instances, functions will be omitted from the output as they are not currently supported by JSONEncoder.

array <- [1,2,3,4,5];
string <- @"A long,

multiline,

string

    with indentation";

myData <- {
    "key": "value",
    "array": array,
    "string": string
};

prettyJSON <- pp.format(myData);

/* returns this multiline string:
{
     "array": [
         1,
         2,
         3,
         4,
         5
    ],
     "string": "A long,\n\nmultiline,\n\nstring\n\n    with indentation",
     "key": "value"
}
*/

### print(*squirrelObject[, truncate]*) ###

The *print()* method formats a Squirrel object using the *format()* method and prints the formatted string. This method takes one required parameter: the Squirrel object to be formatted and printed, and one optional boolean parameter, *truncate*. If *truncate* is not passed in, it will fall back to the default set in the constructor. If *truncate* is set to `true`, [**server.log()**](/api/server/log) will be called on the formatted string and the output may be truncated.  If *truncate* is set to `false`, [**server.log()**](/api/server/log) is called on each line of the formatted string separately, avoiding truncation, although the string may still be subject to message throttling for very long output.

```squirrel
// Print myData and do not truncate
pp.print(myData, false)

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.1 GitHub Fixed printing inconsistency between JSON encoders

License

The PrettyPrint library is licensed under MIT License.