8.9 Pretty Printing

8.9.0.1 PRETTY_DEFAULT

include std/pretty.e
namespace pretty
public constant PRETTY_DEFAULT

8.9.0.2 DISPLAY_ASCII

include std/pretty.e
namespace pretty
public enum DISPLAY_ASCII

8.9.0.3 INDENT

include std/pretty.e
namespace pretty
public enum INDENT

8.9.0.4 START_COLUMN

include std/pretty.e
namespace pretty
public enum START_COLUMN

8.9.0.5 WRAP

include std/pretty.e
namespace pretty
public enum WRAP

8.9.0.6 INT_FORMAT

include std/pretty.e
namespace pretty
public enum INT_FORMAT

8.9.0.7 FP_FORMAT

include std/pretty.e
namespace pretty
public enum FP_FORMAT

8.9.0.8 MIN_ASCII

include std/pretty.e
namespace pretty
public enum MIN_ASCII

8.9.0.9 MAX_ASCII

include std/pretty.e
namespace pretty
public enum MAX_ASCII

8.9.0.10 MAX_LINES

include std/pretty.e
namespace pretty
public enum MAX_LINES

8.9.0.11 LINE_BREAKS

include std/pretty.e
namespace pretty
public enum LINE_BREAKS

8.9.1 Routines

8.9.1.1 pretty_print

include std/pretty.e
namespace pretty
public procedure pretty_print(integer fn, object x, sequence options = PRETTY_DEFAULT)

Print an object to a file or device, using braces { , , , }, indentation, and multiple lines to show the structure.

Parameters:
  1. fn : an integer, the file/device number to write to
  2. x : the object to display/convert to printable form
  3. options : is an (up to) 10-element options sequence.
Comments:

Pass {} in options to select the defaults, or set options as below:

  1. display ASCII characters:
    • 0 -- never
    • 1 -- alongside any integers in printable ASCII range (default)
    • 2 -- display as "string" when all integers of a sequence are in ASCII range
    • 3 -- show strings, and quoted characters (only) for any integers in ASCII range as well as the characters: \t \r \n
  2. amount to indent for each level of sequence nesting -- default: 2
  3. column we are starting at -- default: 1
  4. approximate column to wrap at -- default: 78
  5. format to use for integers -- default: "%d"
  6. format to use for floating-point numbers -- default: "%.10g"
  7. minimum value for printable ASCII -- default 32
  8. maximum value for printable ASCII -- default 127
  9. maximum number of lines to output
  10. line breaks between elements -- default 1 (0 = no line breaks, -1 = line breaks to wrap only)

If the length is less than 10, unspecified options at the end of the sequence will keep the default values. e.g. {0, 5} will choose "never display ASCII", plus 5-character indentation, with defaults for everything else.

The default options can be applied using the public constant PRETTY_DEFAULT, and the elements may be accessed using the following public enum:

  1. DISPLAY_ASCII
  2. INDENT
  3. START_COLUMN
  4. WRAP
  5. INT_FORMAT
  6. FP_FORMAT
  7. MIN_ASCII
  8. MAX_ASCII
  9. MAX_LINES
  10. LINE_BREAKS

The display will start at the current cursor position. Normally you will want to call pretty_print() when the cursor is in column 1 (after printing a <code>\n</code> character). If you want to start in a different column, you should call position() and specify a value for option [3]. This will ensure that the first and last braces in a sequence line up vertically.

When specifying the format to use for integers and floating-point numbers, you can add some decoration, e.g. "(%d)" or "$ %.2f"

Example 1:
pretty_print(1, "ABC", {})

{65'A',66'B',67'C'}
Example 2:
pretty_print(1, {{1,2,3}, {4,5,6}}, {})

{
  {1,2,3},
  {4,5,6}
}
Example 3:
pretty_print(1, {"Euphoria", "Programming", "Language"}, {2})

{
  "Euphoria",
  "Programming",
  "Language"
}
Example 4:
puts(1, "word_list = ") -- moves cursor to column 13
pretty_print(1, 
    {{"Euphoria", 8, 5.3}, 
     {"Programming", 11, -2.9}, 
     {"Language", 8, 9.8}}, 
     {2, 4, 13, 78, "%03d", "%.3f"}) -- first 6 of 8 options

word_list = {
    {
        "Euphoria",
        008,
        5.300
    },
    {
        "Programming",
        011,
        -2.900
    },
    {
        "Language",
        008,
        9.800
    }
}
See Also:

print, sprint, printf, sprintf, pretty_sprint

8.9.1.2 pretty_sprint

include std/pretty.e
namespace pretty
public function pretty_sprint(object x, sequence options = PRETTY_DEFAULT)

Format an object using braces { , , , }, indentation, and multiple lines to show the structure.

Parameters:
  1. x : the object to display
  2. options : is an (up to) 10-element options sequence: Pass {} to select the defaults, or set options
Returns:

A sequence, of printable characters, representing x in an human-readable form.

Comments:

This function formats objects the same as pretty_print(), but returns the sequence obtained instead of sending it to some file..

See Also:

pretty_print, sprint