All Projects → piotrmurach → tty-tree

piotrmurach / tty-tree

Licence: MIT license
Print directory or structured data in a tree like format

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to tty-tree

tty-editor
Opens a file or text in the user's preferred editor
Stars: ✭ 26 (-51.85%)
Mutual labels:  ruby-gem, tty, tty-components
Tty Command
Execute shell commands with pretty output logging and capture stdout, stderr and exit status.
Stars: ✭ 348 (+544.44%)
Mutual labels:  ruby-gem, tty
tty-reader
A set of methods for processing keyboard input in character, line and multiline modes.
Stars: ✭ 73 (+35.19%)
Mutual labels:  ruby-gem, tty
Tty Pager
Terminal output paging - cross-platform, major ruby interpreters
Stars: ✭ 37 (-31.48%)
Mutual labels:  ruby-gem, tty
CLIp
CLIp is a clipboard manager for a command line interface written in 100% standard C only. Pipe to it to copy, pipe from it to paste.
Stars: ✭ 12 (-77.78%)
Mutual labels:  tty, tty-components
Tty Spinner
A terminal spinner for tasks that have non-deterministic time frame.
Stars: ✭ 386 (+614.81%)
Mutual labels:  ruby-gem, tty
Tty Which
Cross-platform implementation of Unix `which` command
Stars: ✭ 11 (-79.63%)
Mutual labels:  ruby-gem, tty
Tty
Toolkit for developing sleek command line apps.
Stars: ✭ 2,329 (+4212.96%)
Mutual labels:  ruby-gem, tty-components
Tty Table
A flexible and intuitive table generator
Stars: ✭ 161 (+198.15%)
Mutual labels:  ruby-gem, tty
Tty Prompt
A beautiful and powerful interactive command line prompt
Stars: ✭ 1,210 (+2140.74%)
Mutual labels:  ruby-gem, tty
Merkle tree
A merkle tree is a data structure used for efficiently summarizing sets of data, often one-time signatures.
Stars: ✭ 68 (+25.93%)
Mutual labels:  ruby-gem, tree-structure
tty-platform
Operating system detection
Stars: ✭ 28 (-48.15%)
Mutual labels:  ruby-gem, tty
tty-link
Hyperlinks in your terminal
Stars: ✭ 30 (-44.44%)
Mutual labels:  tty, tty-components
Scripts-Sploits
A number of scripts POC's and problems solved as pentests move along.
Stars: ✭ 37 (-31.48%)
Mutual labels:  directory
jsonapi-serializer-formats
💎 Gem to enrich jsonapi-serializer with multiple formats
Stars: ✭ 20 (-62.96%)
Mutual labels:  ruby-gem
ruby attic
💍 Unmaintained ruby projects needing people!
Stars: ✭ 26 (-51.85%)
Mutual labels:  ruby-gem
sfm
simple file manager
Stars: ✭ 163 (+201.85%)
Mutual labels:  tty
kitchen-pester
A Test Kitchen driver to execute Pester as a verifier
Stars: ✭ 29 (-46.3%)
Mutual labels:  ruby-gem
Singulink.IO.FileSystem
Reliable cross-platform strongly-typed file/directory path manipulation and file system access in .NET.
Stars: ✭ 16 (-70.37%)
Mutual labels:  directory
algorithms
Algorithms in python and C
Stars: ✭ 71 (+31.48%)
Mutual labels:  tree-structure
TTY Toolkit logo

TTY::Tree Gitter

Gem Version Actions CI Build status Code Climate Coverage Status Inline docs

Print directory or structured data in a tree like format.

TTY::Tree provides independent directory or hash data rendering component for TTY toolkit.

Installation

Add this line to your application's Gemfile:

gem "tty-tree"

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty-tree

Contents

1. Usage

TTY::Tree accepts as input a directory path:

tree = TTY::Tree.new(Dir.pwd)
tree = TTY::Tree.new("dir-name")

It can also be given as its input a hash data structure with keys representing directories and values as arrays 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 the consumer how to handle the tree-like output.

2. Interface

2.1 new

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 a shortcut notation, you can create a tree using [] like so:

tree = TTY::Tree[Dir.pwd]

You can also use DSL to build a tree by using node and leaf methods:

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

2.1.1 :level

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

2.1.2 :file_limit

Prevent TTY::Tree descending directories with more than a given number of entries:

tree = TTY::Tree.new("dir-name", file_limit: 2)

2.1.3 :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)

2.1.4 :only_dirs

To only display directory entries in the output use :only_dirs option:

tree = TTY::Tree.new("dir-name", only_dirs: true)

By default, hidden directories are not included in the output. If you wish to show hidden directories as well do:

tree = TTY::Tree.new("dir-name", only_dirs: true, show_hidden: true)

2.2 render

By default, 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

2.2.1 :indent

The number of spaces to use when indenting nested directories. By default, 4 spaces are used.

tree.render(as: :dir, indent: 2)

Development

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.

Contributing

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

Copyright (c) 2017 Piotr Murach. See LICENSE for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].