cli-tree 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e0f13728de1349b02be11f2ae0bbcb2bc3b306a3fabe278899b21fa7bc9df4c
4
+ data.tar.gz: e36b25b7f939f1bdbf2bd6cdc30ce9c0a0500785cd2b955999c19d9a2aca6346
5
+ SHA512:
6
+ metadata.gz: 0ba42cb576127242e0a08e46d66364121715e14a699a0e9c3a427eaaae232be4ef58a345032db858772fd4c0ac059b51a6c6bea746ab608df3e00631b0b74de2
7
+ data.tar.gz: 387b1fba15bd026f920a642133b883b3810a7029f1ee586d5b89e9e49679d85ecae2c74a7b22e61caa927471c2332a3ed989cbc33e1608e2dd9644bf021112a6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 ZQ
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # ruby-cli-tree
2
+
3
+ A command line printer of tree structures (e.g. directory tree) written in Ruby.
4
+
5
+ This library can be used to build a tree structure, and render or print it
6
+ like an ASCII graph.
7
+
8
+ e.g. it can be used to implement a directory tree printer
9
+ just like the Linux utility [tree](http://mama.indstate.edu/users/ice/tree/). See [bin/tree.rb](bin/tree.rb)
10
+
11
+ ## Installation
12
+
13
+ ```shell
14
+ gem install cli-tree
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ 1. `require 'cli-tree'`
20
+ 2. Create a **TreeNode**: `tree = TreeNode.new(node_name, children = [])`
21
+ 3. Add more **TreeNode** objects to `children`: `tree.children << child_node`
22
+ 4. Add more **TreeNode** objects to `child_node`, and so on.
23
+ 5. Call `puts tree.render` or `tree.print` to print the tree.
24
+
25
+ ## Example
26
+
27
+ ```ruby
28
+ require 'cli-tree'
29
+
30
+ tree = TreeNode.new("root", [
31
+ TreeNode.new("foo", [
32
+ TreeNode.new("bar"),
33
+ TreeNode.new("baz")
34
+ ])
35
+ ])
36
+
37
+ puts tree.render
38
+ # or simply
39
+ tree.print
40
+ ```
41
+
42
+ Output:
43
+
44
+ ```
45
+ root
46
+ └── foo
47
+ ├── bar
48
+ └── baz
49
+ ```
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require_relative '../lib/cli-tree'
5
+
6
+ def get_tree_node(path)
7
+ name = File.basename(path)
8
+ if File.directory?(path)
9
+ children = []
10
+ Dir.new(path).each do |entry|
11
+ next if ['.', '..'].include?(entry)
12
+ entry_path = File.join(path, entry)
13
+ children << get_tree_node(entry_path)
14
+ end
15
+ TreeNode.new(name, children)
16
+ else
17
+ TreeNode.new(name)
18
+ end
19
+ end
20
+
21
+ top_dir = ARGV.first || '.'
22
+ tree = get_tree_node(top_dir)
23
+ tree.print
@@ -0,0 +1,33 @@
1
+ class TreeNode
2
+ VERSION = '1.0.0'
3
+
4
+ attr_accessor :name, :children
5
+
6
+ def initialize(name, children = [])
7
+ @name = name
8
+ @children = children
9
+ end
10
+
11
+ def render
12
+ lines = [@name]
13
+ @children.each_with_index do |child, index|
14
+ child_lines = child.render
15
+ if index < @children.size - 1
16
+ child_lines.each_with_index do |line, idx|
17
+ prefix = (idx == 0) ? "├── " : "| "
18
+ lines << "#{prefix}#{line}"
19
+ end
20
+ else
21
+ child_lines.each_with_index do |line, idx|
22
+ prefix = (idx == 0) ? "└── " : " "
23
+ lines << "#{prefix}#{line}"
24
+ end
25
+ end
26
+ end
27
+ lines
28
+ end
29
+
30
+ def print(stream: STDOUT, prefix: '')
31
+ stream.puts render.map{|line| "#{prefix}#{line}\n"}
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'lib/cli-tree'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'cli-tree'
7
+ s.version = TreeNode::VERSION
8
+ s.date = '2018-06-02'
9
+
10
+ s.summary = 'A command line printer of tree structures (e.g. directory tree) written in Ruby.'
11
+ s.description = <<EOF
12
+ This library can be used to build a tree structure, and render or print it
13
+ like an ASCII graph. It can be used to implement a directory tree printer
14
+ or something like that.
15
+
16
+ Example:
17
+
18
+ require 'cli-tree'
19
+
20
+ tree = TreeNode.new("root", [
21
+ TreeNode.new("foo", [
22
+ TreeNode.new("bar"),
23
+ TreeNode.new("baz")
24
+ ])
25
+ ])
26
+
27
+ puts tree.render
28
+ # or simply
29
+ tree.print
30
+
31
+ Output:
32
+
33
+ root
34
+ └── foo
35
+ ├── bar
36
+ └── baz
37
+ EOF
38
+
39
+ s.authors = ['physacco']
40
+ s.email = ['physacco@gmail.com']
41
+ s.homepage = 'https://github.com/physacco/ruby-cli-tree'
42
+ s.license = 'MIT'
43
+
44
+ s.files = Dir['lib/**/*.rb'] + Dir['bin/*'] +
45
+ ['README.md', 'LICENSE', 'ruby-cli-tree.gemspec']
46
+ s.executables = ['tree.rb']
47
+
48
+ s.platform = Gem::Platform::RUBY
49
+ s.required_ruby_version = '>= 2.0.0'
50
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - physacco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ This library can be used to build a tree structure, and render or print it
15
+ like an ASCII graph. It can be used to implement a directory tree printer
16
+ or something like that.
17
+
18
+ Example:
19
+
20
+ require 'cli-tree'
21
+
22
+ tree = TreeNode.new("root", [
23
+ TreeNode.new("foo", [
24
+ TreeNode.new("bar"),
25
+ TreeNode.new("baz")
26
+ ])
27
+ ])
28
+
29
+ puts tree.render
30
+ # or simply
31
+ tree.print
32
+
33
+ Output:
34
+
35
+ root
36
+ └── foo
37
+ ├── bar
38
+ └── baz
39
+ email:
40
+ - physacco@gmail.com
41
+ executables:
42
+ - tree.rb
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - LICENSE
47
+ - README.md
48
+ - bin/tree.rb
49
+ - lib/cli-tree.rb
50
+ - ruby-cli-tree.gemspec
51
+ homepage: https://github.com/physacco/ruby-cli-tree
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.0.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.7.6
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A command line printer of tree structures (e.g. directory tree) written in
75
+ Ruby.
76
+ test_files: []