NAME

Text::Tree::Indented - render a tree data structure in the classic indented view

SYNOPSIS

use Text::Tree::Indented qw/ generate_tree /;

my $data = [ 'ABC', [
               'DEF', [ 'GHI', 'JKL' ],
               'MNO', [ 'PQR', ['STU' ]],
               'VWX'
           ] ];

binmode(STDOUT, "utf8");
print generate_tree($data);

which produces

ABC
  ├─DEF
  │   ├─GHI
  │   └─JKL
  ├─MNO
  │   └─PQR
  │       └─STU
  └─VWX

DESCRIPTION

This module provides a single function, generate_tree, which takes a perl data structure and renders it into an indented tree view.

Note: the design of this module is still very much in flux, so the data structure and other aspects may change from release to release.

The tree data is passed as an arrayref. A string in the arrayref represents a node in the tree; if it's followed by an arrayref, that's a subtree. So let's say the root of your tree is Fruit, and it has three children, Apples, Bananas, and Oranges, then the data would look like this:

my $tree = ['Fruit', ['Apples', 'Bananas', 'Oranges'] ];

This results in the following tree:

Fruit
  ├─Apples
  ├─Bananas
  └─Oranges

Now you want to add in Red Bananas and Williams Bananas, so your data becomes:

my $tree = ['Fruit', ['Apples', 'Bananas', ['Red', 'Williams'], 'Oranges'] ];

And now the tree looks like this:

Fruit
  ├─Apples
  ├─Bananas
  │   ├─Red
  │   └─Williams
  └─Oranges

generate_tree( $data, $options )

In addition to the tree data, this function takes an optional second argument, which should be a hashref.

At the moment there is just one option, style, which can be one of 'boxrule', 'classic', or 'norule':

print generate_tree($data, { style => 'classic' });

For the example shown in the SYNOPSIS, the resulting tree is:

ABC
  +-DEF
  |   +-GHI
  |   +-JKL
  +-MNO
  |   +-PQR
  |       +-STU
  +-VWX

The default is 'boxrule'.

If you are using the boxrule style, then you should make sure your output can handle wide characters, as in the SYNOPSIS.

SEE ALSO

There are many modules on CPAN for building tree data structures, such as Tree, Tree::AVL, Tree::Binary, etc.

Data::RenderAsTree, Data::TreeDumper, and Data::TreeDraw will render a Perl data structure as an ASCII tree.

Text::Tree takes a representation of a tree and draws a top-down tree with ASCII boxes around the labels.

Tree::To::TextLines draws an indented tree, but requires the tree to be in a more complex, but richer, data structure.

Tree::Visualize can render trees in a number of ways, aimed at situations where you're already using something like Tree::Binary to hold your tree data.

REPOSITORY

https://github.com/neilb/Text-Tree-Indented

AUTHOR

Neil Bowers <neilb@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Neil Bowers.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.