class TreeRender
TreeRender
class build directory tree and print it in console
Attributes
final_hash[R]
Public Instance Methods
render(tree_data, root, files, directories)
click to toggle source
# 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