Skip to content

Commit 8abf2c1

Browse files
committed
Merge pull request #38 from connectstudios/feature-tree_output
Add Tree output
2 parents 5b0e0fd + 66eb045 commit 8abf2c1

File tree

6 files changed

+287
-0
lines changed

6 files changed

+287
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ table rendered for visual display.
4343
You can also explicitly set the renderer used by calling `cli\Table::setRenderer()` and giving it an instance of one
4444
of the concrete `cli\table\Renderer` classes.
4545

46+
Tree Display
47+
------------
48+
49+
* `cli\Tree::__construct()`
50+
* `cli\Tree::setData(array $data)`
51+
* `cli\Tree::setRenderer(cli\tree\Renderer $renderer)`
52+
* `cli\Tree::render()`
53+
* `cli\Tree::display()`
54+
4655
Argument Parser
4756
---------------
4857

examples/tree.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
require_once 'common.php';
4+
5+
$data = [
6+
'Test' => [
7+
'Something Cool' => [
8+
'This is a 3rd layer',
9+
],
10+
'This is a 2nd layer',
11+
],
12+
'Other test' => [
13+
'This is awesome' => [
14+
'This is also cool',
15+
'This is even cooler',
16+
'Wow like what is this' => [
17+
'Awesome eh?',
18+
'Totally' => [
19+
'Yep!'
20+
],
21+
],
22+
],
23+
],
24+
];
25+
26+
printf("ASCII:\n");
27+
28+
/**
29+
* ASCII should look something like this:
30+
*
31+
* -Test
32+
* |\-Something Cool
33+
* ||\-This is a 3rd layer
34+
* |\-This is a 2nd layer
35+
* \-Other test
36+
* \-This is awesome
37+
* \-This is also cool
38+
* \-This is even cooler
39+
* \-Wow like what is this
40+
* \-Awesome eh?
41+
* \-Totally
42+
* \-Yep!
43+
*/
44+
45+
$tree = new \cli\Tree;
46+
$tree->setData($data);
47+
$tree->setRenderer(new \cli\tree\Ascii);
48+
$tree->display();
49+
50+
printf("\nMarkdown:\n");
51+
52+
/**
53+
* Markdown looks like this:
54+
*
55+
* - Test
56+
* - Something Cool
57+
* - This is a 3rd layer
58+
* - This is a 2nd layer
59+
* - Other test
60+
* - This is awesome
61+
* - This is also cool
62+
* - This is even cooler
63+
* - Wow like what is this
64+
* - Awesome eh?
65+
* - Totally
66+
* - Yep!
67+
*/
68+
69+
$tree = new \cli\Tree;
70+
$tree->setData($data);
71+
$tree->setRenderer(new \cli\tree\Markdown(4));
72+
$tree->display();

lib/cli/Tree.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* PHP Command Line Tools
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this package in the file LICENSE.
7+
*
8+
* @author Ryan Sullivan <[email protected]>
9+
* @copyright 2010 James Logsdom (http://girsbrain.org)
10+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
11+
*/
12+
13+
namespace cli;
14+
15+
/**
16+
* The `Tree` class is used to display data in a tree-like format.
17+
*/
18+
class Tree {
19+
20+
protected $_renderer;
21+
protected $_data = array();
22+
23+
/**
24+
* Sets the renderer used by this tree.
25+
*
26+
* @param tree\Renderer $renderer The renderer to use for output.
27+
* @see tree\Renderer
28+
* @see tree\Ascii
29+
* @see tree\Markdown
30+
*/
31+
public function setRenderer(tree\Renderer $renderer) {
32+
$this->_renderer = $renderer;
33+
}
34+
35+
/**
36+
* Set the data.
37+
* Format:
38+
* [
39+
* 'Label' => [
40+
* 'Thing' => ['Thing'],
41+
* ],
42+
* 'Thing',
43+
* ]
44+
* @param array $data
45+
*/
46+
public function setData(array $data)
47+
{
48+
$this->_data = $data;
49+
}
50+
51+
/**
52+
* Render the tree and return it as a string.
53+
*
54+
* @return string|null
55+
*/
56+
public function render()
57+
{
58+
return $this->_renderer->render($this->_data);
59+
}
60+
61+
/**
62+
* Display the rendered tree
63+
*/
64+
public function display()
65+
{
66+
echo $this->render();
67+
}
68+
69+
}

lib/cli/tree/Ascii.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* PHP Command Line Tools
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this package in the file LICENSE.
7+
*
8+
* @author Ryan Sullivan <[email protected]>
9+
* @copyright 2010 James Logsdom (http://girsbrain.org)
10+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
11+
*/
12+
13+
namespace cli\tree;
14+
15+
/**
16+
* The ASCII renderer renders trees with ASCII lines.
17+
*/
18+
class Ascii extends Renderer {
19+
20+
/**
21+
* @param array $tree
22+
* @return string
23+
*/
24+
public function render(array $tree)
25+
{
26+
$output = '';
27+
28+
$treeIterator = new \RecursiveTreeIterator(
29+
new \RecursiveArrayIterator($tree),
30+
\RecursiveTreeIterator::SELF_FIRST
31+
);
32+
33+
foreach ($treeIterator as $val)
34+
{
35+
$output .= $val . "\n";
36+
}
37+
38+
return $output;
39+
}
40+
41+
}

lib/cli/tree/Markdown.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* PHP Command Line Tools
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this package in the file LICENSE.
7+
*
8+
* @author Ryan Sullivan <[email protected]>
9+
* @copyright 2010 James Logsdom (http://girsbrain.org)
10+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
11+
*/
12+
13+
namespace cli\tree;
14+
15+
/**
16+
* The ASCII renderer renders trees with ASCII lines.
17+
*/
18+
class Markdown extends Renderer {
19+
20+
/**
21+
* How many spaces to indent by
22+
* @var int
23+
*/
24+
protected $_padding = 2;
25+
26+
/**
27+
* @param int $padding Optional. Default 2.
28+
*/
29+
function __construct($padding = null)
30+
{
31+
if ($padding)
32+
{
33+
$this->_padding = $padding;
34+
}
35+
}
36+
37+
/**
38+
* Renders the tree
39+
*
40+
* @param array $tree
41+
* @param int $level Optional
42+
* @return string
43+
*/
44+
public function render(array $tree, $level = 0)
45+
{
46+
$output = '';
47+
48+
foreach ($tree as $label => $next)
49+
{
50+
51+
if (is_string($next))
52+
{
53+
$label = $next;
54+
}
55+
56+
// Output the label
57+
$output .= sprintf("%s- %s\n", str_repeat(' ', $level * $this->_padding), $label);
58+
59+
// Next level
60+
if (is_array($next))
61+
{
62+
$output .= $this->render($next, $level + 1);
63+
}
64+
65+
}
66+
67+
return $output;
68+
}
69+
70+
}

lib/cli/tree/Renderer.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* PHP Command Line Tools
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this package in the file LICENSE.
7+
*
8+
* @author Ryan Sullivan <[email protected]>
9+
* @copyright 2010 James Logsdom (http://girsbrain.org)
10+
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
11+
*/
12+
13+
namespace cli\tree;
14+
15+
/**
16+
* Tree renderers are used to change how a tree is displayed.
17+
*/
18+
abstract class Renderer {
19+
20+
/**
21+
* @param array $tree
22+
* @return string|null
23+
*/
24+
abstract public function render(array $tree);
25+
26+
}

0 commit comments

Comments
 (0)