├── .gitignore ├── example.png ├── package.json ├── example.js ├── README.md ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafintosh/pretty-tree/HEAD/example.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pretty-tree", 3 | "version": "1.0.0", 4 | "description": "Make colorful trees out of JSON objects using archy", 5 | "repository": "git://github.com/mafintosh/pretty-tree.git", 6 | "dependencies": { 7 | "archy": "0.0.2", 8 | "chalk": "~1.0.0" 9 | }, 10 | "keywords": [ 11 | "archy", 12 | "colors", 13 | "color", 14 | "tree", 15 | "terminal", 16 | "cli", 17 | "pretty", 18 | "print", 19 | "prettyprint" 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var tree = require('./index'); 2 | 3 | var str = tree({ 4 | label: '(root)', // the label of this node 5 | nodes: [{ 6 | label: '(child)', 7 | leaf: { 8 | hello: 'world', 9 | hej: 'verden' 10 | } 11 | }] 12 | }); 13 | 14 | console.log(str); 15 | 16 | var str = tree({ 17 | label: '(root)', 18 | nodes: [ 19 | { 20 | label: '(child-1)', 21 | leaf: { 22 | foo: 'bar', 23 | bar: true, 24 | foobar:'baz', 25 | multiline:'foo\nbar\nbaz', 26 | nested: { 27 | value: 'her', 28 | count: 42 29 | }, 30 | nested2: { 31 | value: 'her', 32 | count: 42 33 | }, 34 | empty: [], 35 | empty2: {}, 36 | list: [ 37 | 11, 38 | 22, 39 | 32, 40 | { 41 | foo: 'bar', 42 | bar: 'baz' 43 | }, 44 | { 45 | foo: 'meh' 46 | }, 47 | { 48 | foo1: 'bar', 49 | bar2: 'baz' 50 | } 51 | ] 52 | } 53 | }, 54 | { 55 | leaf: 'hello' 56 | }, 57 | { 58 | label: '(child-2)', 59 | leaf: true 60 | } 61 | ] 62 | }); 63 | 64 | console.log(str); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pretty-tree 2 | 3 | Make colorful trees out of JSON objects using [archy](https://github.com/substack/node-archy) 4 | 5 | npm install pretty-tree 6 | 7 | ## Usage 8 | 9 | ``` js 10 | var tree = require('pretty-tree'); 11 | 12 | var str = tree({ 13 | label: '(root)', // the label of this node 14 | nodes: [{ 15 | label: '(child)', 16 | leaf: { 17 | hello: 'world', 18 | hej: 'verden' 19 | } 20 | }] 21 | }); 22 | 23 | console.log(str); 24 | ``` 25 | 26 | The above example results in the following output: 27 | 28 | ![example](https://raw.github.com/mafintosh/pretty-tree/master/example.png) 29 | 30 | The node passed to tree can contain the following options 31 | 32 | ``` js 33 | tree({ 34 | label: '(child)', // an optional lable of this node 35 | leaf: { // set this if you want to print an object 36 | key: value, 37 | ... 38 | }, 39 | nodes: [ // or put in some child nodes 40 | child_nodes_with_same_structure 41 | ] 42 | }) 43 | ``` 44 | 45 | If you want to disable coloring (even when the terminal is a tty) use `tree.plain(options)` 46 | 47 | ## License 48 | 49 | MIT -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var archy = require('archy'); 2 | var chalk = require('chalk'); 3 | 4 | var echo = function(val) { 5 | return val 6 | } 7 | 8 | var tree = function(color) { 9 | var cyan = color ? chalk.cyan : echo 10 | var grey = color ? chalk.grey : echo 11 | var yellow = color ? chalk.yellow : echo 12 | 13 | var isAtomic = function(v) { 14 | return v === null || v === undefined || typeof v !== 'object'; 15 | }; 16 | 17 | var leaf = function(obj) { 18 | if (isAtomic(obj)) return [''+obj]; 19 | 20 | var keys = Object.keys(obj); 21 | var isArray = Array.isArray(obj); 22 | var nodes = []; 23 | 24 | var atomic = keys.filter(function(key) { 25 | return isAtomic(obj[key]); 26 | }); 27 | 28 | var nonAtomic = keys.filter(function(key) { 29 | return !isAtomic(obj[key]); 30 | }); 31 | 32 | var pad = atomic.reduce(function(max, val) { 33 | return max.length >= val.length ? max : val.replace(/./g, ' '); 34 | }, ' '); 35 | 36 | if (!atomic.length && !nonAtomic.length) return [grey('(empty)')]; 37 | 38 | atomic.forEach(function(key) { 39 | var val = (obj[key]+'').replace(/\n/g, '\n '+pad); 40 | key = key+':'+pad.slice(key.length-pad.length-1); 41 | nodes.push(isArray ? val : (cyan(key)+val)); 42 | }); 43 | 44 | nonAtomic.forEach(function(key) { 45 | nodes.push({label:isArray ? undefined : cyan(key), nodes:leaf(obj[key])}); 46 | }); 47 | 48 | return nodes; 49 | }; 50 | 51 | var visit = function(node) { 52 | if (node.label) node.label = yellow(node.label); 53 | if (node.nodes) node.nodes = [].concat(node.nodes).map(visit); 54 | if (node.leaf) node.nodes = [].concat(node.nodes || [], leaf(node.leaf)); 55 | if (node.label && (!node.nodes || !node.nodes.length)) node.nodes = [grey('(empty)')]; 56 | return node; 57 | }; 58 | 59 | return function(node) { 60 | return archy(visit(node)) 61 | .replace(/([├└])─┬ \n[│ ]+├/gm, '$1─┬') 62 | .replace(/([├└])─┬ \n[│ ]+└/gm, '$1──') 63 | .replace(/[┬├─└│┐]/g, function(_) { 64 | return grey(_); 65 | }); 66 | }; 67 | } 68 | 69 | module.exports = tree(true); 70 | module.exports.plain = tree(false); --------------------------------------------------------------------------------