Class: Rich::Table

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

Overview

A table for displaying tabular data

Instance Attribute Summary collapse

Instance Method Summary collapse

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_footer
  @show_edge = show_edge
  @show_lines = show_lines
  @padding = padding
  @expand = expand
  @width = width

  @columns = []
  @rows = []
end

Instance Attribute Details

#border_styleStyle? (readonly)

Returns Border style.

Returns:

  • (Style, nil)

    Border style



84
85
86
# File 'lib/rich/table.rb', line 84

def border_style
  @border_style
end

#boxBox (readonly)

Returns Box style.

Returns:

  • (Box)

    Box style



81
82
83
# File 'lib/rich/table.rb', line 81

def box
  @box
end

#captionString? (readonly)

Returns Table caption.

Returns:

  • (String, nil)

    Table caption



78
79
80
# File 'lib/rich/table.rb', line 78

def caption
  @caption
end

#caption_styleStyle? (readonly)

Returns Caption style.

Returns:

  • (Style, nil)

    Caption style



93
94
95
# File 'lib/rich/table.rb', line 93

def caption_style
  @caption_style
end

#columnsArray<Column> (readonly)

Returns Columns.

Returns:



120
121
122
# File 'lib/rich/table.rb', line 120

def columns
  @columns
end

#expandBoolean (readonly)

Returns Expand to full width.

Returns:

  • (Boolean)

    Expand to full width



114
115
116
# File 'lib/rich/table.rb', line 114

def expand
  @expand
end

#header_styleStyle? (readonly)

Returns Header style.

Returns:

  • (Style, nil)

    Header style



87
88
89
# File 'lib/rich/table.rb', line 87

def header_style
  @header_style
end

#paddingInteger (readonly)

Returns Padding.

Returns:

  • (Integer)

    Padding



111
112
113
# File 'lib/rich/table.rb', line 111

def padding
  @padding
end

#row_stylesStyle? (readonly)

Returns Row styles (alternating).

Returns:

  • (Style, nil)

    Row styles (alternating)



96
97
98
# File 'lib/rich/table.rb', line 96

def row_styles
  @row_styles
end

#rowsArray<Array<String>> (readonly)

Returns Rows.

Returns:

  • (Array<Array<String>>)

    Rows



123
124
125
# File 'lib/rich/table.rb', line 123

def rows
  @rows
end

#show_edgeBoolean (readonly)

Returns Show edge (outer border).

Returns:

  • (Boolean)

    Show edge (outer border)



105
106
107
# File 'lib/rich/table.rb', line 105

def show_edge
  @show_edge
end

Returns Show footer.

Returns:

  • (Boolean)

    Show footer



102
103
104
# File 'lib/rich/table.rb', line 102

def show_footer
  @show_footer
end

#show_headerBoolean (readonly)

Returns Show header.

Returns:

  • (Boolean)

    Show header



99
100
101
# File 'lib/rich/table.rb', line 99

def show_header
  @show_header
end

#show_linesBoolean (readonly)

Returns Show lines between rows.

Returns:

  • (Boolean)

    Show lines between rows



108
109
110
# File 'lib/rich/table.rb', line 108

def show_lines
  @show_lines
end

#titleString? (readonly)

Returns Table title.

Returns:

  • (String, nil)

    Table title



75
76
77
# File 'lib/rich/table.rb', line 75

def title
  @title
end

#title_styleStyle? (readonly)

Returns Title style.

Returns:

  • (Style, nil)

    Title style



90
91
92
# File 'lib/rich/table.rb', line 90

def title_style
  @title_style
end

#widthInteger? (readonly)

Returns Fixed width.

Returns:

  • (Integer, nil)

    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

Parameters:

  • header (String) (defaults to: "")

    Column header

  • kwargs (Hash)

    Column options

Returns:

  • (self)


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

Parameters:

  • cells (Array)

    Cell contents

Returns:

  • (self)


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_countInteger

Returns Number of columns.

Returns:

  • (Integer)

    Number of columns



185
186
187
# File 'lib/rich/table.rb', line 185

def column_count
  @columns.length
end

Print table to console

Parameters:

  • console (Console)

    Console to print to



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

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

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

    Color system

Returns:

  • (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_countInteger

Returns Number of rows.

Returns:

  • (Integer)

    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

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

Returns:



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.footer }
    segments.concat(render_footer_separator(col_widths))
    segments << Segment.new("\n")
    segments.concat(render_footer_row(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