
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Print directory or structured data in a tree like format.
TTY::Tree provides independent directory or hash data rendering component for TTY toolkit.
Add this line to your application's Gemfile:
gem 'tty-tree'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tty-tree
TTY::Tree
accepts as input a directory path:
tree = TTY::Tree.new(Dir.pwd)
tree = TTY::Tree.new('dir-name')
or can be given as its input a hash
data structure with keys representing directories and values as array
s representing directory contents:
data = {
dir1: [
'config.dat',
{ dir2: [
{ dir3: [ 'file3-1.txt' ] },
'file2-1.txt'
]
},
'file1-1.txt',
'file1-2.txt'
]
}
tree = TTY::Tree.new(data)
You can also construct tree with a DSL:
tree = TTY::Tree.new do
node 'dir1' do
node 'config.dat'
node 'dir2' do
node 'dir3' do
leaf 'file3-1.txt'
end
leaf 'file2-1.txt'
end
node 'file1-1.txt'
leaf 'file1-2.txt'
end
end
The TTY::Tree
can print the content in various formats. By default, a directory format is used by invoking render
:
puts tree.render
# =>
# dir1
# ├── config.dat
# ├── dir2
# │ ├── dir3
# │ │ └── file3-1.txt
# │ └── file2-1.txt
# ├── file1-1.txt
# └── file1-2.txt
The render
call returns a string and leaves it up to api consumer how to handle the tree like output.
In order to create TTY::Tree
you need to provide either a path to directory which can be a String, Pathname or Dir:
tree = TTY::Tree.new(Dir.pwd)
tree = TTY::Tree.new('dir-name')
tree = TTY::Tree.new(Pathname.pwd)
Or hash data structure:
data = {
dir1: [
'config.dat',
...
]
}
tree = TTY::Tree.new(data)
As as shortcut notation you can call []
like so:
tree = TTY::Tree[Dir.pwd]
You can also use DSL to build tree by using node
& leaf
:
tree = TTY::Tree.new do
node 'dir1' do
node 'config.dat'
node 'dir2' do
node 'dir3' do
leaf 'file3-1.txt'
end
leaf 'file2-1.txt'
end
node 'file1-1.txt'
leaf 'file1-2.txt'
end
end
The maximum level of depth for this tree when parsing directory. The initial directory is treated as index 0
.
tree = TTY::Tree.new('dir-name', level: 2)
# => parse directories as deep as 2 levels
:file_limit
Prevent TTY::Tree
descending directories more than #
entries:
tree = TTY::Tree.new('dir-name', file_limit: 2)
:show_hidden
In order to for TTY::Tree
to include hidden files in its output use :show_hidden
option like so:
tree = TTY::Tree.new('dir-name', show_hidden: true)
To only display directory entries in the output use :only_dirs
option:
tree = TTY::Tree.new('dir-name', only_dirs: true)
Listing directories does not inlucde hidden ones. If you wish to show hidden directories as well do:
tree = TTY::Tree.new('dir-name', only_dirs: true, show_hidden: true)
By deafult content is printed using TTY::PathRenderer
.
If you prefer a numeric notation of nested content you can use TTY::NumberRenderer
to enumerates each nested node like so:
puts tree.render(as: :number)
# =>
# dir1
# 1.1 config.dat
# 1.2 dir2
# 2.3 dir3
# 3.4 file3-1.txt
# 2.5 file2-1.txt
# 1.6 file1-1.txt
# 1.7 file1-2.txt
:indent
The number of spaces to use when indenting nested directories. By default 4
spaces are used.
tree.render(as: :dir, indent: 2)
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Copyright (c) 2017 Piotr Murach. See LICENSE for further details.
FAQs
Unknown package
We found that tty-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.