Project

cli-tree

0.0
No release in over 3 years
Low commit activity in last 3 years
This library can be used to build a tree structure, and render or print it like an ASCII graph. It can be used to implement a directory tree viewer or something like that.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

ruby-cli-tree

A command line printer of tree structures (e.g. directory tree) written in Ruby.

This library can be used to build a tree structure, and render or print it like an ASCII graph.

e.g. it can be used to implement a directory tree printer just like the Linux utility tree. See bin/tree.rb

Installation

gem install cli-tree

Usage

  1. require 'cli-tree'
  2. Create a TreeNode: tree = TreeNode.new(node_name, children = [])
  3. Add more TreeNode objects to children: tree.children << child_node
  4. Add more TreeNode objects to child_node, and so on.
  5. Call puts tree.render or tree.print to print the tree.

TreeNode methods:

  • initialize(name, children = [])
  • TreeNode.from_h(hash): hash is {name: "string", children: [hash1, ...]}
  • TreeNode.from_json(json): similar to from_h
  • to_h: convert TreeNode to Hash
  • to_json(**kwargs): kwargs refer to JSON#generate
  • render: draw an ASCII tree graph
  • print(stream: STDOUT, prefix: ''): puts the rendered text to a stream

Example

require 'cli-tree'

# the tree and all nodes are TreeNode objects
tree = TreeNode.new("root", [
  TreeNode.new("foo", [
    TreeNode.new("bar"),
    TreeNode.new("baz"),
  ])
])

puts tree.render

or build the tree from a Hash or JSON:

require 'cli-tree'

data = {
  name: "root",  # every node must have a name
  children: [    # and an optional children array
    {  # non-leaf child must be a Hash
      name: "foo",
      children: [
        "bar",  # leaf child can be a String
        "baz",
      ],
    },
  ],
}

tree = TreeNode.from_h(data)
tree.print

Output:

root
└── foo
    ├── bar
    └── baz