Sha256: ef14c1a9250dd92c8ebb24f1102d09060a208f2715cf3b0c5c4efb4be1432a72

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module CobraCommander
  module Output
    # Prints the tree in a nice tree form
    class AsciiTree
      SPACE  = "    "
      BAR    = "│   "
      TEE    = "├── "
      CORNER = "└── "

      def initialize(component)
        @component = component
      end

      def to_s
        StringIO.new.tap do |io|
          io.puts @component.name
          list_dependencies(io, @component)
        end.string
      end

    private

      def list_dependencies(io, component, outdents = [])
        component.dependencies.each do |dep|
          decide_on_line(io, component, dep, outdents)
        end
        nil
      end

      def decide_on_line(io, parent, dep, outdents)
        if parent.dependencies.last != dep
          add_tee(io, outdents, dep)
        else
          add_corner(io, outdents, dep)
        end
      end

      def add_tee(io, outdents, dep)
        io.puts line(outdents, TEE, dep.name)
        list_dependencies(io, dep, (outdents + [BAR]))
      end

      def add_corner(io, outdents, dep)
        io.puts line(outdents, CORNER, dep.name)
        list_dependencies(io, dep, (outdents + [SPACE]))
      end

      def line(outdents, sym, name)
        (outdents + [sym] + [name]).join
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cobra_commander-0.11.0 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.10.0 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.9.2 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.9.1 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.9.0 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.8.1 lib/cobra_commander/output/ascii_tree.rb
cobra_commander-0.8.0 lib/cobra_commander/output/ascii_tree.rb