Sha256: 1607fc28de7047ce4a8564c195d28dcfa4fd228ed05309b6fb253592609a4bd3

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module TTY
  class Tree
    # Render nodes as files paths explorer
    class DirectoryRenderer
      MARKERS = {
        unicode: {
          branch: '├──',
          leaf: '└──',
          pipe: '│'
        },
        ansi: {
          branch: '|--',
          leaf: '`--',
          pipe: '|'
        }
      }.freeze

      def initialize(nodes, options = {})
        @nodes  = nodes
        @indent = options.fetch(:indent, 4)
        @pipe_mark  = MARKERS[:unicode][:pipe] + ' ' * [@indent - 1, 0].max
        @space_mark = ' ' * @indent
      end

      def render
        @nodes.reduce([]) do |acc, node|
          render_node(acc, node, @pipe_mark, @space_mark)
        end.join('')
      end

      private

      def render_node(acc, node, pipe_mark, space_mark)
        acc << node.prefix.gsub(/:pipe/, pipe_mark).gsub(/:space/, space_mark)
        unless node.root?
          acc << MARKERS[:unicode][node.leaf? ? :leaf : :branch]
          acc << ' '
        end
        acc << node.name.to_s + "\n"
      end
    end # DirRenderer
  end # Tree
end # TTY

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tty-tree-0.4.0 lib/tty/tree/directory_renderer.rb
tty-tree-0.3.0 lib/tty/tree/directory_renderer.rb