class Moonshot::UnicodeTable
A class for drawing hierarchical information using unicode lines.
Public Class Methods
new(name)
click to toggle source
# File lib/moonshot/unicode_table.rb, line 8 def initialize(name) @name = name @lines = [] @children = [] end
Public Instance Methods
add_leaf(name)
click to toggle source
# File lib/moonshot/unicode_table.rb, line 14 def add_leaf(name) new_leaf = UnicodeTable.new(name) @children << new_leaf new_leaf end
add_line(line)
click to toggle source
# File lib/moonshot/unicode_table.rb, line 20 def add_line(line) @lines << line self end
add_table(table)
click to toggle source
# File lib/moonshot/unicode_table.rb, line 25 def add_table(table) # Calculate widths widths = [] table.each do |line| line.each_with_index do |col, i| col = '?' unless col.respond_to?(:length) widths[i] = [widths[i] || 0, col.length].max end end format = widths.collect { |n| "%-#{n}s" }.join(' ') << "\n" table.each { |line| add_line(format(format, *line)) } end
draw(depth = 1, first = true)
click to toggle source
# File lib/moonshot/unicode_table.rb, line 39 def draw(depth = 1, first = true) print first ? '┌' : '├' print '─' * depth puts ' ' << @name.light_black @lines = [''] + @lines + [''] @lines.each do |line| puts '│' << (' ' * depth) << line end @children.each do |child| child.draw(depth + 1, false) end end
draw_children()
click to toggle source
Draw all children at the same level, for having multiple top-level peer leaves.
# File lib/moonshot/unicode_table.rb, line 54 def draw_children first = true @children.each do |child| child.draw(1, first) first = false end puts '└──' end