Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

Notifications You must be signed in to change notification settings

physacco/ruby-cli-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /