Class: Rich::Tree
- Inherits:
-
Object
- Object
- Rich::Tree
- Defined in:
- lib/rich/tree.rb
Overview
A tree display for hierarchical data
Instance Attribute Summary collapse
-
#guide ⇒ Hash
readonly
Guide characters.
-
#guide_style ⇒ Style?
readonly
Guide style.
-
#hide_root ⇒ Boolean
readonly
Hide root node.
-
#root ⇒ TreeNode
readonly
Root node.
Class Method Summary collapse
- .add_data_to_node(node, data) ⇒ Object
-
.from_data(data, label: "root", **kwargs) ⇒ Tree
Build tree from nested hash/array structure.
Instance Method Summary collapse
-
#add(label, **kwargs) ⇒ TreeNode
Add a child to root.
-
#initialize(label, style: nil, guide: TreeGuide::UNICODE, guide_style: nil, hide_root: false) ⇒ Tree
constructor
A new instance of Tree.
-
#render(color_system: ColorSystem::TRUECOLOR) ⇒ String
Render to string.
-
#to_segments ⇒ Array<Segment>
Render tree to segments.
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
#guide ⇒ Hash (readonly)
Returns Guide characters.
112 113 114 |
# File 'lib/rich/tree.rb', line 112 def guide @guide end |
#guide_style ⇒ Style? (readonly)
Returns Guide style.
115 116 117 |
# File 'lib/rich/tree.rb', line 115 def guide_style @guide_style end |
#hide_root ⇒ Boolean (readonly)
Returns Hide root node.
118 119 120 |
# File 'lib/rich/tree.rb', line 118 def hide_root @hide_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
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
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
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_segments ⇒ Array<Segment>
Render tree to segments
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. render_children(@root.children, [], segments) end segments end |