class DumbDownViewer::TreeViewBuilder::PlainTextFormat

Constants

LINE_PATTERNS

Attributes

line[RW]

Public Class Methods

new(line_pattern=:default, col_sep=nil, node_format=nil) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 47
def initialize(line_pattern=:default, col_sep=nil, node_format=nil)
  # col_sep is just for having common interface
  @line = LINE_PATTERNS[line_pattern]
  @node_format = node_format || NodeFormat.new
end

Public Instance Methods

draw_last_line(line) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 83
def draw_last_line(line)
  line.each_index do |i|
    node = line[i]
    line[i] = @node_format[node] if node.kind_of?(DirNode)
  end
end
draw_lines(fr, sr, f_node, i) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 67
def draw_lines(fr, sr, f_node, i)
  sub_count = f_node.sub_nodes.size
  j = i
  while sub_count > 0
    j += 1
    s_node = sr[j]
    if s_node
      fr[j] = sub_count == 1 ? @line[:corner] : @line[:branch]
      sub_count -= 1
    else
      fr[j] = @line[:v_line]
    end
  end
  fr[i] = @node_format[f_node]
end
fill_spaces(table) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 90
def fill_spaces(table)
  table.map do |row|
    (row.size - 1).downto(0) do |i|
      row[i] = @line[:spacer] if row[i + 1] and row[i].nil?
    end
    row
  end
end
format_table(tree_table) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 53
def format_table(tree_table)
  t = tree_table.transpose
  t.each_cons(2) do |fr, sr|
    fr.each_with_index do |f, i|
      next unless f.kind_of? Node
      draw_lines(fr, sr, f, i)
    end
  end

  draw_last_line(t[-1])
  update_root_directory_name(tree_table[0][0], t)
  table_to_output_format(t.transpose)
end
table_to_output_format(table) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 105
def table_to_output_format(table)
  fill_spaces(table).map {|r| r.join }.join($/) + $/
end
update_root_directory_name(root, table) click to toggle source
# File lib/dumb_down_viewer/tree_view_builder.rb, line 99
def update_root_directory_name(root, table)
  if root.directory and not root.directory.empty?
    table[0][0] = "[#{File.join(root.directory, root.name)}]"
  end
end