tree_slava 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/data_tree.rb +58 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 10e8b8a006bd0f3453422cf958bfc1d4fba0750fc79f08733bca2cb404d649fa
4
+ data.tar.gz: 1fa59309fd9dcdb62795aca49255a8c1e04cf7dd826e05f90f1fc72154d7bc9d
5
+ SHA512:
6
+ metadata.gz: 51f6f82ac779be12f186e94b2cbb30d0fe734a0cf21c01956525e430ac693728b32ccb25403d548f1a91e1a7a1136404fdee07193827ad93455369a007690c86
7
+ data.tar.gz: '08c78f3d71ff2fd2c78ed49e30bf5ecc5a36beacfe6e30c1308357c278fc32db5ef76914ae321702bd129717bee2d031f0758bdb5fe1cf2505efd10cd1f15904'
data/lib/data_tree.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'pathname'
2
+ class Scanner
3
+ def scan(root = Dir.pwd)
4
+ path_arr = []
5
+ root = Pathname.new(root)
6
+ root.find { |v| path_arr << v }
7
+ Tree.new.make_tree(path_arr, root)
8
+ end
9
+ end
10
+
11
+ class Tree
12
+ def make_tree(path_arr, root)
13
+ tree_data = path_arr
14
+ tree_hash = {}
15
+ branch = '├── '
16
+ pipe = '│ '
17
+ leaf = '└── '
18
+ space = ' '
19
+ tree_data.sort!
20
+ i = tree_data.length
21
+ while i > -1
22
+ i -= 1
23
+ level = tree_data[i].to_s.count('/') - root.to_s.count('/')
24
+ tree_hash[i] = []
25
+ if level == 1
26
+ tree_hash[i] << branch
27
+ tree_hash[i] << File.basename(tree_data[i])
28
+ else
29
+ tree_hash[i] << space
30
+ (level - 2).times { tree_hash[i] << space }
31
+ tree_hash[i] << branch
32
+ tree_hash[i] << File.basename(tree_data[i])
33
+ end
34
+ (0..tree_hash[i].size).each do |index|
35
+ if tree_hash[i + 1].nil? && tree_hash[i][index] == branch
36
+ tree_hash[i][index] = leaf
37
+ elsif tree_hash[i + 1].nil?
38
+ next
39
+ elsif tree_hash[i][index] == space &&
40
+ (tree_hash[i + 1][index] == branch ||
41
+ tree_hash[i + 1][index] == pipe ||
42
+ tree_hash[i + 1][index] == leaf)
43
+ tree_hash[i][index] = pipe
44
+ elsif tree_hash[i][index] == branch &&
45
+ (tree_hash[i + 1][index] != branch &&
46
+ tree_hash[i + 1][index] != pipe &&
47
+ tree_hash[i + 1][index] != leaf)
48
+ tree_hash[i][index] = leaf
49
+ end
50
+ end
51
+ end
52
+ puts root
53
+ tree_hash = tree_hash.each.sort_by { |k, _v| k }
54
+ tree_hash.each { |_k, v| puts v.join }
55
+ end
56
+ end
57
+ tree = Scanner.new
58
+ tree.scan
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_slava
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - somestyl3
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Data tree utility gem
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/data_tree.rb
20
+ homepage:
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Data tree
43
+ test_files: []