<formatter>
Formatted output support

JavaScript: lz.formatter

See <text> and lz.DebugService for additional examples of how to present formatted text to the user interface.

Here are some simple examples that show the use of the s and w conversions:

Example 4. %s, %w, and %#w

<canvas debug="true" height="150" width="100%">
<debug y="5%" height="90%"/>
  <script>
    var myObj = {a: 'eh?', b: 'bee', c: 'sea'};
    Debug.debug("My object using %%s conversion: %s", myObj);
    Debug.debug("My object using %%w conversion: %w", myObj);
    Debug.debug("My object using %%#w conversion: %#w", myObj);
  </script>
</canvas>

Here is an example showing how representations can be abbreviated. If you click on the abbreviated representation, you can inspect the entire object.

Example 5. Abbreviating representations

<canvas debug="true" height="150" width="100%">
<debug y="5%" height="90%"/>
  <script>
    var myObj = {one: 1, too: 2, 3: 'threeee', 42: "The answer", 'a very long tag name': "With a very long value to boot"};
    Debug.debug("My object using %%w: %w", myObj);
    Debug.debug("My object using %%0.32w: %0.32w", myObj);
  </script>
</canvas>

Here is an example showing how positional arguments could be used to accomodate parameterized control strings, say to support internationalizations where the values converted may have to be accessed in different orders.

Example 6. Positional arguments

<canvas layout="axis: y; spacing: 10" height="60" width="100%">
  <debug y="5%" height="90%"/>
  <script>
    var date = {us: "%2$d/%3$d/%1$d", eu: "%1$d-%2$02d-%3$02d"};
    var now = new Date();
  </script>
  <text oninit="this.format(date.us, now.getFullYear(), now.getMonth(), now.getDay())"/>
  <text oninit="this.format(date.eu, now.getFullYear(), now.getMonth(), now.getDay());"/>
</canvas>

Finally, here is an example of using a custom representation for an object

Example 7. Custom object representation

<canvas debug="true" height="300" width="100%">
  <debug y="5%" height="90%"/>
  <script><![CDATA[
    var myObj = {a: "Complex", 'object with an': "ugly representation", 42: "The answer", length: 768};
    Debug.debug("%=s <-- click for further details", myObj, 'Complex!')
  ]]></script>
</canvas>

Methods

abbreviate()
formatter.abbreviate(s : String, l : Number);

Abbreviate a string

This is a support method LzFormatter.formatToString(), which is the preferred interface.

Parameter Name Type Description
s String the string to abbreviate
l Number the desired length, defaults to Infinity
Returns Type Description
  String the abbreviated string

formatToString()
formatter.formatToString(control : String, args : [*]);

Formatted output to string

Formats its arguments according to the control string.

The standard Unix printf(3) conversion specifications are accepted (with the exception of a, n, and p). e, f, and g conversions are accepted but equivalent to f. Positional arguments using the $ field are supported. All the standard flags (# for alternate, 0 for zero padding, - for left justification,   for sign alignment, + for explicit sign) are supported. The ' flag (thousands separator) is ignored. Field width and precision are supported, including the use of parameterized (*) values. The h and l length modifiers are accepted but ignored.

No errors are signalled for invalid conversion directives.

There is an additional conversion specifier w that formats the argument in a readable representation, similar to that of LzDebugService.write(), but with less detail. If the alternate form is requested (with the # flag), w uses the full representation used by LzDebugService.write(). The %w directive obeys Debug.printLength. If a precision is specified, that is used as Debug.printLength for the corresponding conversion. If a representation is truncated because of precision for w or #s conversions, the abbreviation will be noted by ellipsis (…), rather than truncation. If the debugger is not present, w conversion behaves the same as s.

Any Object that is represented by s or w conversions using the Debug formatted output methods will create a link that can be clicked on to inspect the represented object. There is an additional flag for the s and w formats (=) which will take the next argument as the object to be represented (i.e., linked to) by the conversion; thus permitting custom representations.

Parameter Name Type Description
control String A control string where % indicates a conversion directive that will be replaced by a representation of the corresponding argument as specified by the details of the conversion directive.
args [*] arguments to be formatted according to the control string.
Returns Type Description
  lz.Message the return value is an LzMessage which can be coreced to a string or HTML (the latter supports the hot-linking feature).

pad()
formatter.pad(value : *, widthMin : Number, decMax : Number, pad : String, sign : String, radix : Number, force : Boolean);

Pad or trim a string to a specified length

This is a support method LzFormatter.formatToString(), which is the preferred interface.

Parameter Name Type Description
value * the value to pad, will be coerced to a string if not a number
widthMin Number the minimum width, if negative pad on the right, default 0
decMax Number for numbers: ensure decimals characters to the right of '.', padding with 0, otherwise maximum width
pad String character to pad on left with, default ' '
sign String ' ', '-', '+', default '-'
radix Number radix to represent numbers in, default 10
force Boolean ensure numbers always have a decimal