tty-tree 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +24 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +240 -0
- data/Rakefile +8 -0
- data/appveyor.yml +23 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tty-tree.rb +3 -0
- data/lib/tty/tree.rb +113 -0
- data/lib/tty/tree/directory_renderer.rb +46 -0
- data/lib/tty/tree/hash_walker.rb +71 -0
- data/lib/tty/tree/node.rb +85 -0
- data/lib/tty/tree/number_renderer.rb +31 -0
- data/lib/tty/tree/path_walker.rb +109 -0
- data/lib/tty/tree/version.rb +7 -0
- data/tasks/console.rake +11 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +29 -0
- data/tty-tree.gemspec +27 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b7ed240a5a6239047b55b961fa170aaa28df6109
|
4
|
+
data.tar.gz: b5363c4033cf2218c69d4a0174d1348d2dca5ab7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c873142481cc06042c4d6f1e3de114f91b372ddd52bca933a84f1a2ba3abedcaa86b0d35d75e4ed5cffe6a13ae5040576dc29e3d04a806a81730e82b6e372dc
|
7
|
+
data.tar.gz: 18c3d908b3a7c8e0e0205ccf908e9594237ccaece938c567ed5dd7bf86e0f300993cc8e139b5953d405b488cad2c36340c77f55dcd6245033d01d2b4e0c8a9c2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
sudo: false
|
4
|
+
before_install: gem install bundler -v 1.14.6
|
5
|
+
bundler_args: --without tools
|
6
|
+
script: "bundle exec rake ci"
|
7
|
+
rvm:
|
8
|
+
- 2.0.0
|
9
|
+
- 2.1.10
|
10
|
+
- 2.2.6
|
11
|
+
- 2.3.3
|
12
|
+
- 2.4.0
|
13
|
+
- ruby-head
|
14
|
+
- jruby-9000
|
15
|
+
- jruby-head
|
16
|
+
matrix:
|
17
|
+
allow_failures:
|
18
|
+
- rvm: ruby-head
|
19
|
+
- rvm: jruby-head
|
20
|
+
fast_finish: true
|
21
|
+
branches:
|
22
|
+
only: master
|
23
|
+
notifications:
|
24
|
+
email: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at [email]. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'benchmark-ips', '~> 2.0.0'
|
7
|
+
gem 'simplecov', '~> 0.10.0'
|
8
|
+
gem 'coveralls', '~> 0.8.2'
|
9
|
+
gem 'term-ansicolor', '=1.3.2'
|
10
|
+
end
|
11
|
+
|
12
|
+
group :tools do
|
13
|
+
gem 'byebug', platform: :mri
|
14
|
+
end
|
15
|
+
|
16
|
+
group :metrics do
|
17
|
+
gem 'yard', '~> 0.8.7'
|
18
|
+
gem 'yardstick', '~> 0.9.9'
|
19
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Piotr Murach
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,240 @@
|
|
1
|
+
# TTY::Tree [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/tty-tree.svg)][gem]
|
4
|
+
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-tree.svg?branch=master)][travis]
|
5
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/q3s4gagj8xnospw4?svg=true)][appveyor]
|
6
|
+
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-tree/badges/gpa.svg)][codeclimate]
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-tree/badge.svg)][coverage]
|
8
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-tree.svg?branch=master)][inchpages]
|
9
|
+
|
10
|
+
[gitter]: https://gitter.im/piotrmurach/tty
|
11
|
+
[gem]: http://badge.fury.io/rb/tty-tree
|
12
|
+
[travis]: http://travis-ci.org/piotrmurach/tty-tree
|
13
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-tree
|
14
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-tree
|
15
|
+
[coverage]: https://coveralls.io/github/piotrmurach/tty-tree
|
16
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-tree
|
17
|
+
|
18
|
+
> Print directory or structured data in a tree like format.
|
19
|
+
|
20
|
+
**TTY::Prompt** provides independent directory or hash data rendering component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'tty-tree'
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install tty-tree
|
37
|
+
|
38
|
+
## Contents
|
39
|
+
|
40
|
+
* [1. Usage](#1-usage)
|
41
|
+
* [2. Interface](#2-interface)
|
42
|
+
* [2.1 new](#21-new)
|
43
|
+
* [2.1.1 level](#211-level)
|
44
|
+
* [2.1.2 file_limit](#212-level)
|
45
|
+
* [2.1.3 show_hidden](#213-show_hidden)
|
46
|
+
* [2.1.4 only_dirs](#214-only_dirs)
|
47
|
+
* [2.2 render](#22-render)
|
48
|
+
* [2.2.1 indent](#221-indent)
|
49
|
+
|
50
|
+
## 1. Usage
|
51
|
+
|
52
|
+
`TTY::Tree` accepts as input a directory path:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
tree = TTY::Tree.new(Dir.pwd)
|
56
|
+
tree = TTY::Tree.new('dir-name')
|
57
|
+
```
|
58
|
+
|
59
|
+
or can be given as its input a `hash` data structure with keys representing directories and values as `array`s representing directory contents:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
data = {
|
63
|
+
dir1: [
|
64
|
+
'config.dat',
|
65
|
+
{ dir2: [
|
66
|
+
{ dir3: [ 'file3-1.txt' ] },
|
67
|
+
'file2-1.txt'
|
68
|
+
]
|
69
|
+
},
|
70
|
+
'file1-1.txt',
|
71
|
+
'file1-2.txt'
|
72
|
+
]
|
73
|
+
}
|
74
|
+
|
75
|
+
tree = TTY::Tree.new(data)
|
76
|
+
```
|
77
|
+
|
78
|
+
You can also construct tree with a DSL:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
tree = TTY::Tree.new do
|
82
|
+
node 'dir1' do
|
83
|
+
node 'config.dat'
|
84
|
+
node 'dir2' do
|
85
|
+
node 'dir3' do
|
86
|
+
leaf 'file3-1.txt'
|
87
|
+
end
|
88
|
+
leaf 'file2-1.txt'
|
89
|
+
end
|
90
|
+
node 'file1-1.txt'
|
91
|
+
leaf 'file1-2.txt'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
The `TTY::Tree` can print the content in various formats. By default, a directory format is used by invoking `render`:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
puts tree.render
|
100
|
+
# =>
|
101
|
+
# dir1
|
102
|
+
# ├── config.dat
|
103
|
+
# ├── dir2,
|
104
|
+
# │ ├── dir3\n",
|
105
|
+
# │ │ └── file3-1.txt\n",
|
106
|
+
# │ └── file2-1.txt\n",
|
107
|
+
# ├── file1-1.txt\n",
|
108
|
+
# └── file1-2.txt\n",
|
109
|
+
```
|
110
|
+
|
111
|
+
The `render` call returns a string and leaves it up to api consumer how to handle the tree like output.
|
112
|
+
|
113
|
+
## 2. Interface
|
114
|
+
|
115
|
+
### 2.1 new
|
116
|
+
|
117
|
+
In order to create `TTY::Tree` you need to provide either a path to directory which can be a String, Pathname or Dir:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
tree = TTY::Tree.new(Dir.pwd)
|
121
|
+
tree = TTY::Tree.new('dir-name')
|
122
|
+
tree = TTY::Tree.new(Pathname.pwd)
|
123
|
+
```
|
124
|
+
|
125
|
+
or hash data structure:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
data = {
|
129
|
+
dir1: [
|
130
|
+
'config.dat',
|
131
|
+
...
|
132
|
+
]
|
133
|
+
}
|
134
|
+
|
135
|
+
tree = TTY::Tree.new(data)
|
136
|
+
```
|
137
|
+
|
138
|
+
As as shortcut notation you can call `[]` like so:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
tree = TTY::Tree[Dir.pwd]
|
142
|
+
```
|
143
|
+
|
144
|
+
You can also use DSL to build tree by using `node` & `leaf`:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
tree = TTY::Tree.new do
|
148
|
+
node 'dir1' do
|
149
|
+
node 'config.dat'
|
150
|
+
node 'dir2' do
|
151
|
+
node 'dir3' do
|
152
|
+
leaf 'file3-1.txt'
|
153
|
+
end
|
154
|
+
leaf 'file2-1.txt'
|
155
|
+
end
|
156
|
+
node 'file1-1.txt'
|
157
|
+
leaf 'file1-2.txt'
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
#### 2.1.1 level
|
163
|
+
|
164
|
+
The maximum level of depth for this tree when parsing directory. The initial directory is treated as index `0`.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
tree = TTY::Tree.new('dir-name', level: 2)
|
168
|
+
# => parse directories as deep as 2 levels
|
169
|
+
```
|
170
|
+
|
171
|
+
#### 2.1.2 file_limit
|
172
|
+
|
173
|
+
Prevent `TTY::Tree` descending directories more than `#` entries:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
tree = TTY::Tree.new('dir-name', file_limit: 2)
|
177
|
+
```
|
178
|
+
|
179
|
+
#### 2.1.3 show_hidden
|
180
|
+
|
181
|
+
In order to for `TTY::Tree` to include hidden files in its output use `:show_hidden` option like so:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
tree = TTY::Tree.new('dir-name', show_hidden: true)
|
185
|
+
```
|
186
|
+
|
187
|
+
#### 2.1.4 only_dirs
|
188
|
+
|
189
|
+
To only display directory entries in the output use `:only_dirs` option:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
tree = TTY::Tree.new('dir-name', only_dirs: true)
|
193
|
+
```
|
194
|
+
|
195
|
+
Listing directories does not inlucde hidden ones. If you wish to show hidden directories as well do:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
tree = TTY::Tree.new('dir-name', only_dirs: true, show_hidden: true)
|
199
|
+
```
|
200
|
+
|
201
|
+
### 2.2 render
|
202
|
+
|
203
|
+
By deafult content is printed using `TTY::PathRenderer`.
|
204
|
+
|
205
|
+
If you prefer a numeric notation of nested content you can use `TTY::NumberRenderer` to enumerates each nested node like so:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
puts tree.render(as: :number)
|
209
|
+
# =>
|
210
|
+
# dir1\n",
|
211
|
+
# 1.1 config.dat\n",
|
212
|
+
# 1.2 dir2\n",
|
213
|
+
# 2.3 dir3\n",
|
214
|
+
# 3.4 file3-1.txt\n",
|
215
|
+
# 2.5 file2-1.txt\n",
|
216
|
+
# 1.6 file1-1.txt\n",
|
217
|
+
# 1.7 file1-2.txt\n",
|
218
|
+
```
|
219
|
+
|
220
|
+
#### 2.2.1 indent
|
221
|
+
|
222
|
+
The number of spaces to use when indenting nested directories. By default `4` spaces are used.
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
tree.render(as: :dir, indent: 2)
|
226
|
+
```
|
227
|
+
|
228
|
+
## Development
|
229
|
+
|
230
|
+
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.
|
231
|
+
|
232
|
+
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](https://rubygems.org).
|
233
|
+
|
234
|
+
## Contributing
|
235
|
+
|
236
|
+
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](http://contributor-covenant.org) code of conduct.
|
237
|
+
|
238
|
+
## Copyright
|
239
|
+
|
240
|
+
Copyright (c) 2017 Piotr Murach. See LICENSE for further details.
|
data/Rakefile
ADDED
data/appveyor.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
---
|
2
|
+
install:
|
3
|
+
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
|
4
|
+
- ruby --version
|
5
|
+
- gem --version
|
6
|
+
- bundle install
|
7
|
+
build: off
|
8
|
+
test_script:
|
9
|
+
- bundle exec rake ci
|
10
|
+
environment:
|
11
|
+
matrix:
|
12
|
+
- ruby_version: "193"
|
13
|
+
- ruby_version: "200"
|
14
|
+
- ruby_version: "200-x64"
|
15
|
+
- ruby_version: "21"
|
16
|
+
- ruby_version: "21-x64"
|
17
|
+
- ruby_version: "22"
|
18
|
+
- ruby_version: "22-x64"
|
19
|
+
- ruby_version: "23"
|
20
|
+
- ruby_version: "23-x64"
|
21
|
+
matrix:
|
22
|
+
allow_failures:
|
23
|
+
- ruby_version: "193"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tty/tree"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/tty-tree.rb
ADDED
data/lib/tty/tree.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'tree/node'
|
5
|
+
require_relative 'tree/directory_renderer'
|
6
|
+
require_relative 'tree/number_renderer'
|
7
|
+
require_relative 'tree/hash_walker'
|
8
|
+
require_relative 'tree/path_walker'
|
9
|
+
require_relative 'tree/version'
|
10
|
+
|
11
|
+
module TTY
|
12
|
+
class Tree
|
13
|
+
# @api public
|
14
|
+
def self.[](data)
|
15
|
+
new(data)
|
16
|
+
end
|
17
|
+
|
18
|
+
# The list of nodes in this tree.
|
19
|
+
attr_reader :nodes
|
20
|
+
|
21
|
+
# Create a Tree
|
22
|
+
#
|
23
|
+
# @param [String,Dir,Hash] data
|
24
|
+
#
|
25
|
+
# @api public
|
26
|
+
def initialize(data = nil, options = {}, &block)
|
27
|
+
@data = data ? data.dup.freeze : nil
|
28
|
+
@walker = select_walker.new(options)
|
29
|
+
@nodes = []
|
30
|
+
|
31
|
+
if @data
|
32
|
+
@walker.traverse(data)
|
33
|
+
@nodes = @walker.nodes
|
34
|
+
end
|
35
|
+
|
36
|
+
@nodes_stack = []
|
37
|
+
|
38
|
+
instance_eval(&block) if block_given?
|
39
|
+
|
40
|
+
freeze
|
41
|
+
end
|
42
|
+
|
43
|
+
# Add node to this tree.
|
44
|
+
#
|
45
|
+
# @param [Symbol,String] name
|
46
|
+
# the name for the node
|
47
|
+
#
|
48
|
+
# @param [Node, LeafNode] type
|
49
|
+
# the type of node to add
|
50
|
+
#
|
51
|
+
# @example
|
52
|
+
# TTY::Tree.new do
|
53
|
+
# node '...' do
|
54
|
+
# node '...'
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
def node(name, type = Node, &block)
|
60
|
+
parent = @nodes_stack.empty? ? Node::ROOT : @nodes_stack.last
|
61
|
+
level = [0, @nodes_stack.size - 1].max
|
62
|
+
prefix = ':pipe' * level
|
63
|
+
if parent.class == LeafNode
|
64
|
+
prefix = ':space' * level
|
65
|
+
end
|
66
|
+
node = type.new(name, parent.full_path, prefix, @nodes_stack.size)
|
67
|
+
@nodes << node
|
68
|
+
|
69
|
+
return unless block_given?
|
70
|
+
|
71
|
+
@nodes_stack << node
|
72
|
+
if block.arity.zero?
|
73
|
+
instance_eval(&block)
|
74
|
+
else
|
75
|
+
instance_eval(&(->(*_args) { block[node] }))
|
76
|
+
end
|
77
|
+
@nodes_stack.pop
|
78
|
+
end
|
79
|
+
|
80
|
+
# Add leaf node
|
81
|
+
#
|
82
|
+
# @api public
|
83
|
+
def leaf(name, &block)
|
84
|
+
node(name, LeafNode, &block)
|
85
|
+
end
|
86
|
+
|
87
|
+
# @api public
|
88
|
+
def render(options = {})
|
89
|
+
as = options.delete(:as) || :dir
|
90
|
+
renderer = select_renderer(as).new(nodes, options)
|
91
|
+
renderer.render
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
# @api private
|
97
|
+
def select_walker
|
98
|
+
if @data.is_a?(Hash) || @data.nil?
|
99
|
+
HashWalker
|
100
|
+
else
|
101
|
+
@data ||= Dir.pwd
|
102
|
+
PathWalker
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def select_renderer(as)
|
107
|
+
case as
|
108
|
+
when :dir, :directory then DirectoryRenderer
|
109
|
+
when :num, :number then NumberRenderer
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end # Tree
|
113
|
+
end # TTY
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module TTY
|
5
|
+
class Tree
|
6
|
+
# Render nodes as files paths explorer
|
7
|
+
class DirectoryRenderer
|
8
|
+
MARKERS = {
|
9
|
+
unicode: {
|
10
|
+
branch: '├──',
|
11
|
+
leaf: '└──',
|
12
|
+
pipe: '│'
|
13
|
+
},
|
14
|
+
ansi: {
|
15
|
+
branch: '|--',
|
16
|
+
leaf: '`--',
|
17
|
+
pipe: '|'
|
18
|
+
}
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
def initialize(nodes, options = {})
|
22
|
+
@nodes = nodes
|
23
|
+
@indent = options.fetch(:indent, 4)
|
24
|
+
@pipe_mark = MARKERS[:unicode][:pipe] + ' ' * [@indent - 1, 0].max
|
25
|
+
@space_mark = ' ' * @indent
|
26
|
+
end
|
27
|
+
|
28
|
+
def render
|
29
|
+
@nodes.reduce([]) do |acc, node|
|
30
|
+
render_node(acc, node, @pipe_mark, @space_mark)
|
31
|
+
end.join('')
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def render_node(acc, node, pipe_mark, space_mark)
|
37
|
+
acc << node.prefix.gsub(/:pipe/, pipe_mark).gsub(/:space/, space_mark)
|
38
|
+
unless node.root?
|
39
|
+
acc << MARKERS[:unicode][node.leaf? ? :leaf : :branch]
|
40
|
+
acc << ' '
|
41
|
+
end
|
42
|
+
acc << node.name.to_s + "\n"
|
43
|
+
end
|
44
|
+
end # DirRenderer
|
45
|
+
end # Tree
|
46
|
+
end # TTY
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
require_relative 'node'
|
7
|
+
|
8
|
+
module TTY
|
9
|
+
class Tree
|
10
|
+
# Walk and collect nodes from hash data strcture.
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
class HashWalker
|
14
|
+
attr_reader :nodes
|
15
|
+
|
16
|
+
attr_reader :files_count
|
17
|
+
|
18
|
+
attr_reader :dirs_count
|
19
|
+
|
20
|
+
def initialize(options = {})
|
21
|
+
@nodes = []
|
22
|
+
@files_count = 0
|
23
|
+
@dirs_count = 0
|
24
|
+
@level = options.fetch(:level) { -1 }
|
25
|
+
@file_limit = options.fetch(:file_limit) { - 1 }
|
26
|
+
end
|
27
|
+
|
28
|
+
def traverse(data)
|
29
|
+
walk(data, Pathname.new(''), '', 0, false)
|
30
|
+
end
|
31
|
+
|
32
|
+
def walk(data, parent_path, prefix, level, is_last)
|
33
|
+
node = is_last ? LeafNode : Node
|
34
|
+
|
35
|
+
case data
|
36
|
+
when Hash
|
37
|
+
return if @level != -1 && level + 1 > @level
|
38
|
+
|
39
|
+
data.each do |dir, item|
|
40
|
+
dir_node = node.new(dir.to_s, parent_path, prefix, level)
|
41
|
+
@nodes << dir_node
|
42
|
+
@dirs_count += 1 unless dir_node.root?
|
43
|
+
|
44
|
+
if level > 0
|
45
|
+
postfix = ':pipe'
|
46
|
+
postfix = ':space' if is_last
|
47
|
+
else
|
48
|
+
postfix = ''
|
49
|
+
end
|
50
|
+
|
51
|
+
walk(item, parent_path + dir.to_s,
|
52
|
+
prefix + postfix, level + 1, false)
|
53
|
+
end
|
54
|
+
when Array
|
55
|
+
return if @file_limit != -1 && data.size > @file_limit
|
56
|
+
|
57
|
+
last_data_index = data.size - 1
|
58
|
+
|
59
|
+
data.each_with_index do |item, i|
|
60
|
+
last = (last_data_index == i)
|
61
|
+
|
62
|
+
walk(item, parent_path, prefix, level, last)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
@nodes << node.new(data.to_s, parent_path, prefix, level)
|
66
|
+
@files_count += 1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end # HashWalker
|
70
|
+
end # Tree
|
71
|
+
end # TTY
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'forwardable'
|
5
|
+
require 'pathname'
|
6
|
+
|
7
|
+
module TTY
|
8
|
+
class Tree
|
9
|
+
# A representation of tree node
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
class Node
|
13
|
+
extend Forwardable
|
14
|
+
|
15
|
+
# The base name for the directory or file
|
16
|
+
attr_reader :name
|
17
|
+
|
18
|
+
# The parent directory path
|
19
|
+
attr_reader :parent
|
20
|
+
|
21
|
+
# The require path prefix
|
22
|
+
attr_reader :prefix
|
23
|
+
|
24
|
+
# The directory depth
|
25
|
+
attr_reader :level
|
26
|
+
|
27
|
+
# The file stat
|
28
|
+
attr_reader :stat
|
29
|
+
|
30
|
+
# The current path
|
31
|
+
attr_reader :path
|
32
|
+
|
33
|
+
def_delegators :@path, :directory?, :executable?, :file?,
|
34
|
+
:symlink?, :socket?, :pipe?
|
35
|
+
|
36
|
+
def initialize(path, parent, prefix, level)
|
37
|
+
@path = Pathname.new(path)
|
38
|
+
@name = @path.basename
|
39
|
+
@parent = Pathname.new(parent)
|
40
|
+
@prefix = prefix
|
41
|
+
@level = level
|
42
|
+
end
|
43
|
+
|
44
|
+
def full_path
|
45
|
+
return parent if name.to_s.empty?
|
46
|
+
parent.join(name)
|
47
|
+
end
|
48
|
+
|
49
|
+
def root?
|
50
|
+
parent.to_s.empty?
|
51
|
+
end
|
52
|
+
|
53
|
+
def hidden?
|
54
|
+
name.to_s.start_with?('.')
|
55
|
+
end
|
56
|
+
|
57
|
+
def leaf?
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_s
|
62
|
+
@name
|
63
|
+
end
|
64
|
+
|
65
|
+
def ==(other)
|
66
|
+
other.is_a?(self.class) && other.state_attrs == state_attrs
|
67
|
+
end
|
68
|
+
alias eql? ==
|
69
|
+
|
70
|
+
protected
|
71
|
+
|
72
|
+
def state_attrs
|
73
|
+
[@name, @path, @parent, @prefix, @level]
|
74
|
+
end
|
75
|
+
|
76
|
+
ROOT = Node.new('', Pathname.new(''), '', 0).freeze
|
77
|
+
end # Node
|
78
|
+
|
79
|
+
class LeafNode < Node
|
80
|
+
def leaf?
|
81
|
+
true
|
82
|
+
end
|
83
|
+
end # LeafNode
|
84
|
+
end # Tree
|
85
|
+
end # TTY
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module TTY
|
5
|
+
class Tree
|
6
|
+
# Render nodes as numbered list
|
7
|
+
class NumberRenderer
|
8
|
+
def initialize(nodes, options = {})
|
9
|
+
@indent = options.fetch(:indent, 4)
|
10
|
+
@nodes = nodes
|
11
|
+
@mark = ' ' * @indent
|
12
|
+
end
|
13
|
+
|
14
|
+
def render
|
15
|
+
@nodes.each_with_index.reduce([]) do |acc, (node, i)|
|
16
|
+
render_node(acc, node, i, @mark)
|
17
|
+
end.join
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def render_node(acc, node, i, mark)
|
23
|
+
acc << node.prefix.gsub(/:pipe|:space/, mark)
|
24
|
+
unless node.root?
|
25
|
+
acc << "#{node.level}.#{i} "
|
26
|
+
end
|
27
|
+
acc << node.name.to_s + "\n"
|
28
|
+
end
|
29
|
+
end # NumberRenderer
|
30
|
+
end # Tree
|
31
|
+
end # TTY
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
require_relative 'node'
|
7
|
+
|
8
|
+
module TTY
|
9
|
+
class Tree
|
10
|
+
# Walk and collect nodes from directory.
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
class PathWalker
|
14
|
+
attr_reader :nodes
|
15
|
+
|
16
|
+
attr_reader :files_count
|
17
|
+
|
18
|
+
attr_reader :dirs_count
|
19
|
+
|
20
|
+
# Create a PathWalker
|
21
|
+
#
|
22
|
+
# @api public
|
23
|
+
def initialize(**options)
|
24
|
+
@files_count = 0
|
25
|
+
@dirs_count = 0
|
26
|
+
@nodes = []
|
27
|
+
@filters = []
|
28
|
+
@level = options.fetch(:level) { -1 }
|
29
|
+
@file_limit = options.fetch(:file_limit) { - 1 }
|
30
|
+
|
31
|
+
unless options[:show_hidden]
|
32
|
+
add_filter(-> (p) { !p.basename.to_s.start_with?('.') })
|
33
|
+
end
|
34
|
+
|
35
|
+
if options[:only_dirs]
|
36
|
+
add_filter(-> (p) { p.directory? })
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_filter(filter)
|
41
|
+
@filters << filter
|
42
|
+
end
|
43
|
+
|
44
|
+
# Traverse given path recursively
|
45
|
+
#
|
46
|
+
# @param [String] path
|
47
|
+
# the path to traverse
|
48
|
+
#
|
49
|
+
# @api public
|
50
|
+
def traverse(path)
|
51
|
+
root_path = Pathname.new(path)
|
52
|
+
empty_path = Pathname.new('')
|
53
|
+
|
54
|
+
unless root_path.directory?
|
55
|
+
raise ArgumentError, "#{root_path} is not a directory path"
|
56
|
+
end
|
57
|
+
|
58
|
+
@nodes << Node.new(root_path, empty_path, '', 0)
|
59
|
+
|
60
|
+
walk(root_path, root_path.children, '', 1)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
# Filter entries
|
66
|
+
#
|
67
|
+
# @api private
|
68
|
+
def filter_entries(entries, filters)
|
69
|
+
return entries if filters.nil? || filters.empty?
|
70
|
+
filter = filters[0]
|
71
|
+
filter_entries(entries.select(&filter), filters[1..-1])
|
72
|
+
end
|
73
|
+
|
74
|
+
# Walk paths recursively
|
75
|
+
#
|
76
|
+
# @api private
|
77
|
+
def walk(parent_path, entries, prefix, level)
|
78
|
+
if entries.empty? || (@level != -1 && @level < level)
|
79
|
+
return
|
80
|
+
else
|
81
|
+
return if @file_limit != -1 && entries.size > @file_limit
|
82
|
+
processed_paths = filter_entries(entries, @filters).sort
|
83
|
+
last_path_index = processed_paths.size - 1
|
84
|
+
|
85
|
+
processed_paths.each_with_index do |path, i|
|
86
|
+
sub_path = path.relative_path_from(parent_path)
|
87
|
+
|
88
|
+
node = last_path_index == i ? LeafNode : Node
|
89
|
+
|
90
|
+
if path.directory?
|
91
|
+
next if @level != -1 && level + 1 > @level
|
92
|
+
|
93
|
+
@nodes << node.new(sub_path, parent_path, prefix, level)
|
94
|
+
@dirs_count += 1
|
95
|
+
|
96
|
+
postfix = ':pipe'
|
97
|
+
postfix = ':space' if i == last_path_index
|
98
|
+
|
99
|
+
walk(path, path.children, prefix + postfix, level + 1)
|
100
|
+
elsif path.file?
|
101
|
+
@nodes << node.new(path, parent_path, prefix, level)
|
102
|
+
@files_count += 1
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end # PathWalker
|
108
|
+
end # Tree
|
109
|
+
end # TTY
|
data/tasks/console.rake
ADDED
data/tasks/coverage.rake
ADDED
data/tasks/spec.rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Run all specs'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
8
|
+
task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :spec do
|
12
|
+
desc 'Run unit specs'
|
13
|
+
RSpec::Core::RakeTask.new(:unit) do |task|
|
14
|
+
task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Run integration specs'
|
18
|
+
RSpec::Core::RakeTask.new(:integration) do |task|
|
19
|
+
task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
rescue LoadError
|
24
|
+
%w[spec spec:unit spec:integration].each do |name|
|
25
|
+
task name do
|
26
|
+
$stderr.puts "In order to run #{name}, do `gem install rspec`"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/tty-tree.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tty/tree/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tty-tree"
|
8
|
+
spec.version = TTY::Tree::VERSION
|
9
|
+
spec.authors = ["Piotr Murach"]
|
10
|
+
spec.email = [""]
|
11
|
+
|
12
|
+
spec.summary = %q{Print directory or structured data in a tree like format.}
|
13
|
+
spec.description = %q{Print directory or structured data in a tree like format.}
|
14
|
+
spec.homepage = "https://piotrmurach.github.io/tty"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tty-tree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Murach
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Print directory or structured data in a tree like format.
|
56
|
+
email:
|
57
|
+
- ''
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- appveyor.yml
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- lib/tty-tree.rb
|
75
|
+
- lib/tty/tree.rb
|
76
|
+
- lib/tty/tree/directory_renderer.rb
|
77
|
+
- lib/tty/tree/hash_walker.rb
|
78
|
+
- lib/tty/tree/node.rb
|
79
|
+
- lib/tty/tree/number_renderer.rb
|
80
|
+
- lib/tty/tree/path_walker.rb
|
81
|
+
- lib/tty/tree/version.rb
|
82
|
+
- tasks/console.rake
|
83
|
+
- tasks/coverage.rake
|
84
|
+
- tasks/spec.rake
|
85
|
+
- tty-tree.gemspec
|
86
|
+
homepage: https://piotrmurach.github.io/tty
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.5.1
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Print directory or structured data in a tree like format.
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|