Sha256: 1e97963b6c2d2ea94d226a04313ba26bdd63fdc500da10df14754d7a462ab4b3

Contents?: true

Size: 701 Bytes

Versions: 2

Compression:

Stored size: 701 Bytes

Contents

module Treedent
  class NodeDrawing < Struct.new(:node)
    SPACE  = "    "
    BAR    = "│   "
    TEE    = "├── "
    CORNER = "└── "

    def to_s
      [drawings, node.value.content].compact.join
    end

    def drawings
      return [] if node.orphan?
      [outer_indentation, inner_indentation].flatten
    end

    private

    def inner_indentation
      if node.final_sibling?
        CORNER
      else
        TEE
      end
    end

    def outer_indentation
      node.ancestors.reverse_each.map do |parent|
        next if parent.root? || parent.orphan?

        if parent.final_sibling?
          SPACE
        else
          BAR
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
treedent-1.1.0 lib/treedent/node_drawing.rb
treedent-1.0.0 lib/treedent/node_drawing.rb