4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/data_tree/tree.rb', line 4
def make_tree(path_arr, root)
tree_data = path_arr
tree_hash = {}
branch = '├── '
pipe = '│ '
leaf = '└── '
space = ' '
tree_data.sort!
i = tree_data.length
while i != 1
i -= 1
level = tree_data[i].to_s.count('/') - root.to_s.count('/')
tree_hash[i] = []
if level == 1
tree_hash[i] << branch
tree_hash[i] << File.basename(tree_data[i])
else
tree_hash[i] << space
(level - 2).times { tree_hash[i] << space }
tree_hash[i] << branch
tree_hash[i] << File.basename(tree_data[i])
end
Parallel.map((0..tree_hash[i].size), in_threads: 3) do |index|
if tree_hash[i + 1].nil? && tree_hash[i][index] == branch
tree_hash[i][index] = leaf
elsif tree_hash[i + 1].nil?
next
elsif tree_hash[i][index] == space &&
(tree_hash[i + 1][index] == branch ||
tree_hash[i + 1][index] == pipe ||
tree_hash[i + 1][index] == leaf)
tree_hash[i][index] = pipe
elsif tree_hash[i][index] == branch &&
(tree_hash[i + 1][index] != branch &&
tree_hash[i + 1][index] != pipe &&
tree_hash[i + 1][index] != leaf)
tree_hash[i][index] = leaf
end
end
end
puts root
tree_hash = tree_hash.each.sort_by { |k, _v| k }
tree_hash.each { |_k, v| puts v.join }
end
|