Class: TreeRender
- Inherits:
-
Object
- Object
- TreeRender
- Defined in:
- lib/tree/tree_render.rb
Overview
TreeRender class build directory tree and print it in console
Instance Attribute Summary collapse
-
#final_hash ⇒ Object
readonly
Returns the value of attribute final_hash.
Instance Method Summary collapse
Instance Attribute Details
#final_hash ⇒ Object (readonly)
Returns the value of attribute final_hash.
7 8 9 |
# File 'lib/tree/tree_render.rb', line 7 def final_hash @final_hash end |
Instance Method Details
#render(tree_data, root, files, directories) ⇒ Object
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 48 49 50 51 52 53 |
# File 'lib/tree/tree_render.rb', line 9 def render(tree_data, root, files, directories) @final_hash = {} branch = '├── ' pipe = '│ ' turn = '└── ' space = ' ' tree_data.sort! i = tree_data.length - 1 while i != -1 leaf_level = tree_data[i].to_s.count('/') - root.to_s.count('/') @final_hash[i] = [] if leaf_level == 1 @final_hash[i] << branch @final_hash[i] << File.basename(tree_data[i]) else @final_hash[i] << space (leaf_level - 2).times { @final_hash[i] << space } @final_hash[i] << branch @final_hash[i] << File.basename(tree_data[i]) end (0..@final_hash[i].size).each do |index| if @final_hash[i + 1].nil? && @final_hash[i][index] == branch @final_hash[i][index] = turn elsif @final_hash[i + 1].nil? next elsif @final_hash[i][index] == space && (@final_hash[i + 1][index] == branch || @final_hash[i + 1][index] == pipe || @final_hash[i + 1][index] == turn) @final_hash[i][index] = pipe elsif @final_hash[i][index] == branch && (@final_hash[i + 1][index] != branch && @final_hash[i + 1][index] != pipe && @final_hash[i + 1][index] != turn) @final_hash[i][index] = turn end end i -= 1 end puts root @final_hash = @final_hash.each.sort_by { |k, _| k } @final_hash.each { |_, v| puts v.join } puts puts "#{files} files, #{directories} directories" end |