cli-tree 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e0f13728de1349b02be11f2ae0bbcb2bc3b306a3fabe278899b21fa7bc9df4c
4
- data.tar.gz: e36b25b7f939f1bdbf2bd6cdc30ce9c0a0500785cd2b955999c19d9a2aca6346
3
+ metadata.gz: da90838e296f082ffc1c64eed162ea67d1b326e535467ae99f490cb07e03f84a
4
+ data.tar.gz: 8254103ce9f3f2f028da206874aa287aa4868847eb4669aaf1a425c84b068399
5
5
  SHA512:
6
- metadata.gz: 0ba42cb576127242e0a08e46d66364121715e14a699a0e9c3a427eaaae232be4ef58a345032db858772fd4c0ac059b51a6c6bea746ab608df3e00631b0b74de2
7
- data.tar.gz: 387b1fba15bd026f920a642133b883b3810a7029f1ee586d5b89e9e49679d85ecae2c74a7b22e61caa927471c2332a3ed989cbc33e1608e2dd9644bf021112a6
6
+ metadata.gz: a747e8dc304f108e57bca638fb3f94be3c291d03847d294a9a1123792da40a0540e52482f667abbf203d1ff386375bbcb8cf6f9f6593d3073ebc78c57595aba6
7
+ data.tar.gz: 49c150eb070e86c54305cb63b57c48e5651b7c2857c0adec500209b0bb8003413ad701b0b378f942ab12bb29d2175bf77ddd8c9b81f29bc9409b51c1050736c3
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require_relative '../lib/cli-tree'
5
+
6
+ if ARGV.size == 0
7
+ STDERR.puts "Usage: #{$0} input.json"
8
+ exit 1
9
+ end
10
+
11
+ json = File.read ARGV.first
12
+ tree = TreeNode.from_json json
13
+ tree.print
@@ -1,16 +1,76 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: utf-8
3
3
 
4
+ require 'getoptlong'
4
5
  require_relative '../lib/cli-tree'
5
6
 
6
- def get_tree_node(path)
7
- name = File.basename(path)
7
+ Program = File.basename $0
8
+
9
+ Usage = <<EOF
10
+ Usage: #{Program} [options] [dir]
11
+
12
+ Options:
13
+ -h, --help Print this help message and exit
14
+ -v, --version Print version information and exit
15
+ -a, --all All files are printed, including hidden files
16
+ -d, --dir-only List directories only
17
+ -f, --full-prefix Prints the full path prefix for each file
18
+
19
+ Arguments:
20
+ dir The directory to traverse (Default: .)
21
+ EOF
22
+
23
+ VersionInfo = "#{Program} v#{TreeNode::VERSION}"
24
+
25
+ def parse_arguments
26
+ opts = GetoptLong.new(
27
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
28
+ ['--version', '-v', GetoptLong::NO_ARGUMENT],
29
+ ['--all', '-a', GetoptLong::NO_ARGUMENT],
30
+ ['--dir-only', '-d', GetoptLong::NO_ARGUMENT],
31
+ ['--full-prefix', '-f', GetoptLong::NO_ARGUMENT],
32
+ )
33
+
34
+ args = {
35
+ show_all: false,
36
+ dir_only: false,
37
+ full_prefix: false,
38
+ }
39
+
40
+ begin
41
+ opts.each do |opt, arg|
42
+ case opt
43
+ when '--help'
44
+ puts Usage
45
+ exit 0
46
+ when '--version'
47
+ puts VersionInfo
48
+ exit 0
49
+ when '--all'
50
+ args[:show_all] = true
51
+ when '--dir-only'
52
+ args[:dir_only] = true
53
+ when '--full-prefix'
54
+ args[:full_prefix] = true
55
+ end
56
+ end
57
+ rescue GetoptLong::Error
58
+ exit 1
59
+ end
60
+
61
+ args
62
+ end
63
+
64
+ def get_tree_node(path, **kwargs)
65
+ name = kwargs[:full_prefix] ? path : File.basename(path)
8
66
  if File.directory?(path)
9
67
  children = []
10
68
  Dir.new(path).each do |entry|
11
69
  next if ['.', '..'].include?(entry)
70
+ next if entry.start_with?('.') && !kwargs[:show_all]
71
+ next if !File.directory?(entry) && kwargs[:dir_only]
12
72
  entry_path = File.join(path, entry)
13
- children << get_tree_node(entry_path)
73
+ children << get_tree_node(entry_path, **kwargs)
14
74
  end
15
75
  TreeNode.new(name, children)
16
76
  else
@@ -18,6 +78,7 @@ def get_tree_node(path)
18
78
  end
19
79
  end
20
80
 
21
- top_dir = ARGV.first || '.'
22
- tree = get_tree_node(top_dir)
81
+ args = parse_arguments
82
+ dir = ARGV.first || '.'
83
+ tree = get_tree_node(dir, **args)
23
84
  tree.print
@@ -1,5 +1,7 @@
1
+ require 'json'
2
+
1
3
  class TreeNode
2
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
3
5
 
4
6
  attr_accessor :name, :children
5
7
 
@@ -8,6 +10,32 @@ class TreeNode
8
10
  @children = children
9
11
  end
10
12
 
13
+ def TreeNode.from_h(hash)
14
+ if hash.is_a?(Hash)
15
+ raw_children = hash.has_key?(:children) ? hash[:children] : []
16
+ children = raw_children.map{|ch| TreeNode.from_h(ch)}
17
+ TreeNode.new hash[:name], children
18
+ else
19
+ TreeNode.new hash
20
+ end
21
+ end
22
+
23
+ def TreeNode.from_json(json)
24
+ hash = JSON.parse json, symbolize_names: true
25
+ TreeNode.from_h hash
26
+ end
27
+
28
+ def to_h
29
+ {
30
+ name: @name,
31
+ children: @children.map{|node| node.to_h}
32
+ }
33
+ end
34
+
35
+ def to_json(**kwargs)
36
+ JSON.generate(to_h, **kwargs)
37
+ end
38
+
11
39
  def render
12
40
  lines = [@name]
13
41
  @children.each_with_index do |child, index|
@@ -1,50 +1,28 @@
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
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 viewer
14
+ or something like that.
15
+ EOF
16
+
17
+ s.authors = ['physacco']
18
+ s.email = ['physacco@gmail.com']
19
+ s.homepage = 'https://github.com/physacco/ruby-cli-tree'
20
+ s.license = 'MIT'
21
+
22
+ s.files = Dir['lib/**/*.rb'] + Dir['bin/*'] + Dir['test/*'] +
23
+ ['README.md', 'LICENSE', 'ruby-cli-tree.gemspec']
24
+ s.executables = ['tree.rb', 'json2tree.rb']
25
+
26
+ s.platform = Gem::Platform::RUBY
27
+ s.required_ruby_version = '>= 2.0.0'
28
+ end
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "root",
3
+ "children": [
4
+ {
5
+ "name": "foo",
6
+ "children": [
7
+ {
8
+ "name": "bar",
9
+ "children": [
10
+
11
+ ]
12
+ },
13
+ {
14
+ "name": "baz",
15
+ "children": [
16
+
17
+ ]
18
+ }
19
+ ]
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "root",
3
+ "children": [
4
+ {
5
+ "name": "foo",
6
+ "children": [ "bar", "baz" ]
7
+ }
8
+ ]
9
+ }
@@ -0,0 +1,16 @@
1
+ require_relative '../lib/cli-tree'
2
+
3
+ tree = TreeNode.new("root", [
4
+ TreeNode.new("foo", [
5
+ TreeNode.new("bar"),
6
+ TreeNode.new("baz")
7
+ ])
8
+ ])
9
+
10
+ puts tree.render
11
+
12
+ json = tree.to_json(indent: " ", space: " ", object_nl: "\n", array_nl: "\n")
13
+ puts json
14
+
15
+ tree2 = TreeNode.from_json(json)
16
+ tree2.print
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - physacco
@@ -12,42 +12,25 @@ date: 2018-06-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
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
15
+ like an ASCII graph. It can be used to implement a directory tree viewer
16
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
17
  email:
40
18
  - physacco@gmail.com
41
19
  executables:
42
20
  - tree.rb
21
+ - json2tree.rb
43
22
  extensions: []
44
23
  extra_rdoc_files: []
45
24
  files:
46
25
  - LICENSE
47
26
  - README.md
27
+ - bin/json2tree.rb
48
28
  - bin/tree.rb
49
29
  - lib/cli-tree.rb
50
30
  - ruby-cli-tree.gemspec
31
+ - test/1.json
32
+ - test/2.json
33
+ - test/test1.rb
51
34
  homepage: https://github.com/physacco/ruby-cli-tree
52
35
  licenses:
53
36
  - MIT