class DirFriend::Graph

Public Class Methods

new(dir) click to toggle source
# File lib/dir_friend/graph.rb, line 3
def initialize(dir)
  @dir = dir
end

Public Instance Methods

build(opt) click to toggle source

Build a graphviz dot data using Gviz opt keys: :layout, :colorscheme, :dir_shape, :file_shape,

:graph(or global), :nodes, :edges

ex. layout:'fdp', colorscheme:'blues', graph:{splines:'ortho'}

# File lib/dir_friend/graph.rb, line 15
def build(opt)
  global_opt, nodes_opt, edges_opt, dir_shape, file_shape = opt_parser(opt)

  dirs = [@dir] + @dir.select(&:directory?)
  
  gv = ::Gviz.new
  gv.global global_opt
  gv.nodes nodes_opt
  gv.edges edges_opt
  dirs.each do |d|
    # files
    d.entries.each do |ent|
      c, fc = color_id(ent.level, nodes_opt[:style])
      gv.node ent.path.to_id, label:ent.name, shape:file_shape,
                              color:c, fontcolor:fc
    end

    # directory
    c, fc = color_id(d.level, nodes_opt[:style])
    gv.node d.path.to_id, label:d.name, shape:dir_shape,
                          color:c, fontcolor:fc

    # route dir => children
    gv.route d.path.to_id => d.entries.map{ |ch| ch.path.to_id }
  end
  gv
end
render(opt={}) click to toggle source
# File lib/dir_friend/graph.rb, line 7
def render(opt={})
  build(opt).to_s
end

Private Instance Methods

color_depth() click to toggle source
# File lib/dir_friend/graph.rb, line 68
def color_depth
  depth = @dir.depth + 1
  if depth > 9 then 9
  elsif depth < 3 then 3
  else depth
  end
end
color_id(lv, node_style) click to toggle source
# File lib/dir_friend/graph.rb, line 76
def color_id(lv, node_style)
  color = @dir.depth + 1 - lv
  fontc =
    if node_style && node_style.match(/filled/)
      lv < ((@dir.depth+1)/2) ? 'white' : 'black'
    else
      'black'
    end
  [color, fontc]
end
opt_color_parser(color) click to toggle source
# File lib/dir_friend/graph.rb, line 61
def opt_color_parser(color)
  unless color.match(/\w\d$/) #end with one digit number
    color = "#{color}#{color_depth}"
  end
  color
end
opt_parser(opt) click to toggle source
# File lib/dir_friend/graph.rb, line 44
def opt_parser(opt)
  global = opt[:global] || opt[:graph] || {layout:'dot'}
  global = global.merge(layout:opt[:layout]) if opt[:layout]

  nodes  = opt[:nodes] || {}
  if cs = (nodes[:colorscheme] || opt[:colorscheme])
    cs = opt_color_parser(cs)
    nodes.update(style:'filled', colorscheme:cs)
  end

  edges  = opt[:edges] || {}
  dir_shape = opt[:dir_shape] || nodes[:shape] || 'ellipse'
  file_shape = opt[:file_shape] || nodes[:shape] || 'ellipse'

  [global, nodes, edges, dir_shape, file_shape]
end