Class: Rich::Table
- Inherits:
-
Object
- Object
- Rich::Table
- Defined in:
- lib/rich/table.rb
Overview
A table for displaying tabular data
Instance Attribute Summary collapse
-
#border_style ⇒ Style?
readonly
Border style.
-
#box ⇒ Box
readonly
Box style.
-
#caption ⇒ String?
readonly
Table caption.
-
#caption_style ⇒ Style?
readonly
Caption style.
-
#columns ⇒ Array<Column>
readonly
Columns.
-
#expand ⇒ Boolean
readonly
Expand to full width.
-
#header_style ⇒ Style?
readonly
Header style.
-
#padding ⇒ Integer
readonly
Padding.
-
#row_styles ⇒ Style?
readonly
Row styles (alternating).
-
#rows ⇒ Array<Array<String>>
readonly
Rows.
-
#show_edge ⇒ Boolean
readonly
Show edge (outer border).
-
#show_footer ⇒ Boolean
readonly
Show footer.
-
#show_header ⇒ Boolean
readonly
Show header.
-
#show_lines ⇒ Boolean
readonly
Show lines between rows.
-
#title ⇒ String?
readonly
Table title.
-
#title_style ⇒ Style?
readonly
Title style.
-
#width ⇒ Integer?
readonly
Fixed width.
Instance Method Summary collapse
-
#add_column(header = "", **kwargs) ⇒ self
Add a column.
-
#add_row(*cells) ⇒ self
Add a row.
-
#column_count ⇒ Integer
Number of columns.
-
#initialize(title: nil, caption: nil, box: Box::ROUNDED, border_style: nil, header_style: nil, title_style: nil, caption_style: nil, row_styles: nil, show_header: true, show_footer: false, show_edge: true, show_lines: false, padding: 1, expand: false, width: nil) ⇒ Table
constructor
A new instance of Table.
-
#print_to(console) ⇒ Object
Print table to console.
-
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render table to string.
-
#row_count ⇒ Integer
Number of rows.
-
#to_segments(max_width: 80) ⇒ Array<Segment>
Render table to segments.
Constructor Details
#initialize(title: nil, caption: nil, box: Box::ROUNDED, border_style: nil, header_style: nil, title_style: nil, caption_style: nil, row_styles: nil, show_header: true, show_footer: false, show_edge: true, show_lines: false, padding: 1, expand: false, width: nil) ⇒ Table
Returns a new instance of Table.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rich/table.rb', line 125 def initialize( title: nil, caption: nil, box: Box::ROUNDED, border_style: nil, header_style: nil, title_style: nil, caption_style: nil, row_styles: nil, show_header: true, show_footer: false, show_edge: true, show_lines: false, padding: 1, expand: false, width: nil ) @title = title @caption = caption @box = box @border_style = border_style.is_a?(String) ? Style.parse(border_style) : border_style @header_style = header_style.is_a?(String) ? Style.parse(header_style) : header_style @title_style = title_style.is_a?(String) ? Style.parse(title_style) : title_style @caption_style = caption_style.is_a?(String) ? Style.parse(caption_style) : caption_style @row_styles = row_styles @show_header = show_header @show_footer = @show_edge = show_edge @show_lines = show_lines @padding = padding @expand = @width = width @columns = [] @rows = [] end |
Instance Attribute Details
#border_style ⇒ Style? (readonly)
Returns Border style.
84 85 86 |
# File 'lib/rich/table.rb', line 84 def border_style @border_style end |
#box ⇒ Box (readonly)
Returns Box style.
81 82 83 |
# File 'lib/rich/table.rb', line 81 def box @box end |
#caption ⇒ String? (readonly)
Returns Table caption.
78 79 80 |
# File 'lib/rich/table.rb', line 78 def caption @caption end |
#caption_style ⇒ Style? (readonly)
Returns Caption style.
93 94 95 |
# File 'lib/rich/table.rb', line 93 def caption_style @caption_style end |
#columns ⇒ Array<Column> (readonly)
Returns Columns.
120 121 122 |
# File 'lib/rich/table.rb', line 120 def columns @columns end |
#expand ⇒ Boolean (readonly)
Returns Expand to full width.
114 115 116 |
# File 'lib/rich/table.rb', line 114 def @expand end |
#header_style ⇒ Style? (readonly)
Returns Header style.
87 88 89 |
# File 'lib/rich/table.rb', line 87 def header_style @header_style end |
#padding ⇒ Integer (readonly)
Returns Padding.
111 112 113 |
# File 'lib/rich/table.rb', line 111 def padding @padding end |
#row_styles ⇒ Style? (readonly)
Returns Row styles (alternating).
96 97 98 |
# File 'lib/rich/table.rb', line 96 def row_styles @row_styles end |
#rows ⇒ Array<Array<String>> (readonly)
Returns Rows.
123 124 125 |
# File 'lib/rich/table.rb', line 123 def rows @rows end |
#show_edge ⇒ Boolean (readonly)
Returns Show edge (outer border).
105 106 107 |
# File 'lib/rich/table.rb', line 105 def show_edge @show_edge end |
#show_footer ⇒ Boolean (readonly)
Returns Show footer.
102 103 104 |
# File 'lib/rich/table.rb', line 102 def @show_footer end |
#show_header ⇒ Boolean (readonly)
Returns Show header.
99 100 101 |
# File 'lib/rich/table.rb', line 99 def show_header @show_header end |
#show_lines ⇒ Boolean (readonly)
Returns Show lines between rows.
108 109 110 |
# File 'lib/rich/table.rb', line 108 def show_lines @show_lines end |
#title ⇒ String? (readonly)
Returns Table title.
75 76 77 |
# File 'lib/rich/table.rb', line 75 def title @title end |
#title_style ⇒ Style? (readonly)
Returns Title style.
90 91 92 |
# File 'lib/rich/table.rb', line 90 def title_style @title_style end |
#width ⇒ Integer? (readonly)
Returns Fixed width.
117 118 119 |
# File 'lib/rich/table.rb', line 117 def width @width end |
Instance Method Details
#add_column(header = "", **kwargs) ⇒ self
Add a column
166 167 168 169 |
# File 'lib/rich/table.rb', line 166 def add_column(header = "", **kwargs) @columns << Column.new(header, **kwargs) self end |
#add_row(*cells) ⇒ self
Add a row
174 175 176 177 178 179 180 181 182 |
# File 'lib/rich/table.rb', line 174 def add_row(*cells) # Ensure we have enough columns while @columns.length < cells.length @columns << Column.new end @rows << cells.map(&:to_s) self end |
#column_count ⇒ Integer
Returns Number of columns.
185 186 187 |
# File 'lib/rich/table.rb', line 185 def column_count @columns.length end |
#print_to(console) ⇒ Object
Print table to console
271 272 273 274 |
# File 'lib/rich/table.rb', line 271 def print_to(console) rendered = render(max_width: console.width, color_system: console.color_system) console.write(rendered) end |
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render table to string
265 266 267 |
# File 'lib/rich/table.rb', line 265 def render(max_width: 80, color_system: ColorSystem::TRUECOLOR) Segment.render(to_segments(max_width: max_width), color_system: color_system) end |
#row_count ⇒ Integer
Returns Number of rows.
190 191 192 |
# File 'lib/rich/table.rb', line 190 def row_count @rows.length end |
#to_segments(max_width: 80) ⇒ Array<Segment>
Render table to segments
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/rich/table.rb', line 197 def to_segments(max_width: 80) return [Segment.new("")] if @columns.empty? segments = [] col_widths = calculate_column_widths(max_width) table_width = col_widths.sum + (@columns.length + 1) + @columns.length * @padding * 2 # Title if @title && @show_edge segments.concat(render_title(table_width - 2)) segments << Segment.new("\n") end # Top border if @show_edge segments.concat(render_top_border(col_widths)) segments << Segment.new("\n") end # Header if @show_header segments.concat(render_header_row(col_widths)) segments << Segment.new("\n") # Header separator segments.concat(render_header_separator(col_widths)) segments << Segment.new("\n") end # Data rows @rows.each_with_index do |row, index| segments.concat(render_data_row(row, col_widths, index)) segments << Segment.new("\n") # Row separator if @show_lines && index < @rows.length - 1 segments.concat(render_row_separator(col_widths)) segments << Segment.new("\n") end end # Footer if @show_footer && @columns.any? { |c| c. } segments.concat((col_widths)) segments << Segment.new("\n") segments.concat((col_widths)) segments << Segment.new("\n") end # Bottom border if @show_edge segments.concat(render_bottom_border(col_widths)) segments << Segment.new("\n") end # Caption if @caption && @show_edge segments.concat(render_caption(table_width - 2)) segments << Segment.new("\n") end segments end |