Class: Rich::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/rich/tree.rb

Overview

A tree display for hierarchical data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, style: nil, guide: TreeGuide::UNICODE, guide_style: nil, hide_root: false) ⇒ Tree

Returns a new instance of Tree.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rich/tree.rb', line 120

def initialize(
  label,
  style: nil,
  guide: TreeGuide::UNICODE,
  guide_style: nil,
  hide_root: false
)
  @root = TreeNode.new(label, style: style)
  @guide = guide
  @guide_style = guide_style.is_a?(String) ? Style.parse(guide_style) : guide_style
  @hide_root = hide_root
end

Instance Attribute Details

#guideHash (readonly)

Returns Guide characters.

Returns:

  • (Hash)

    Guide characters



112
113
114
# File 'lib/rich/tree.rb', line 112

def guide
  @guide
end

#guide_styleStyle? (readonly)

Returns Guide style.

Returns:

  • (Style, nil)

    Guide style



115
116
117
# File 'lib/rich/tree.rb', line 115

def guide_style
  @guide_style
end

#hide_rootBoolean (readonly)

Returns Hide root node.

Returns:

  • (Boolean)

    Hide root node



118
119
120
# File 'lib/rich/tree.rb', line 118

def hide_root
  @hide_root
end

#rootTreeNode (readonly)

Returns Root node.

Returns:



109
110
111
# File 'lib/rich/tree.rb', line 109

def root
  @root
end

Class Method Details

.add_data_to_node(node, data) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rich/tree.rb', line 199

def self.add_data_to_node(node, data)
  case data
  when Hash
    data.each do |key, value|
      child = node.add(key.to_s)
      add_data_to_node(child, value)
    end
  when Array
    data.each_with_index do |item, index|
      if item.is_a?(Hash) || item.is_a?(Array)
        child = node.add("[#{index}]")
        add_data_to_node(child, item)
      else
        node.add(item.to_s)
      end
    end
  else
    node.add(data.to_s) unless data.nil?
  end
end

.from_data(data, label: "root", **kwargs) ⇒ Tree

Build tree from nested hash/array structure

Parameters:

  • data (Hash, Array)

    Nested data

  • label (String) (defaults to: "root")

    Root label

Returns:



169
170
171
172
173
# File 'lib/rich/tree.rb', line 169

def self.from_data(data, label: "root", **kwargs)
  tree = new(label, **kwargs)
  add_data_to_node(tree.root, data)
  tree
end

Instance Method Details

#add(label, **kwargs) ⇒ TreeNode

Add a child to root

Parameters:

  • label (String)

    Child label

  • kwargs (Hash)

    Node options

Returns:



137
138
139
# File 'lib/rich/tree.rb', line 137

def add(label, **kwargs)
  @root.add(label, **kwargs)
end

#render(color_system: ColorSystem::TRUECOLOR) ⇒ String

Render to string

Parameters:

  • color_system (Symbol) (defaults to: ColorSystem::TRUECOLOR)

    Color system

Returns:

  • (String)


161
162
163
# File 'lib/rich/tree.rb', line 161

def render(color_system: ColorSystem::TRUECOLOR)
  Segment.render(to_segments, color_system: color_system)
end

#to_segmentsArray<Segment>

Render tree to segments

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rich/tree.rb', line 143

def to_segments
  segments = []

  unless @hide_root
    segments << Segment.new(@root.label, style: @root.style)
    segments << Segment.new("\n")
  end

  if @root.expanded
    render_children(@root.children, [], segments)
  end

  segments
end