Class: Hawktui::StreamingTable::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/hawktui/streaming_table/column.rb

Overview

Public: Represents a single column in a table. Knows how to truncate or pad a cell’s text to fit within its width.

Examples

column = Hawktui::StreamingTable::Column.new(name: :message, width: 50)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, width:) ⇒ Column

Public: Create a new Column with a name and width.

name - A Symbol or String representing the column’s name. width - An Integer specifying how wide the column is.

Examples

column = Hawktui::StreamingTable::Column.new(name: :message, width: 50)

Returns a new Column instance.



25
26
27
28
# File 'lib/hawktui/streaming_table/column.rb', line 25

def initialize(name:, width:)
  @name = name
  @width = width
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/hawktui/streaming_table/column.rb', line 13

def name
  @name
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/hawktui/streaming_table/column.rb', line 13

def width
  @width
end

Instance Method Details

#format_cell(cell) ⇒ Object

Public: Format the cell’s textual value to fit within the column width. If the text is longer than ‘width`, it will be truncated with an ellipsis (“…”). Otherwise, it’s left-padded with spaces to fill the width.

cell - A Hawktui::StreamingTable::Cell containing the raw value and color.

Examples

cell = Hawktui::StreamingTable::Cell.new("Some message")
column.format_cell(cell)
# => ["Some message        ", nil] # => example if width is bigger

Returns an Array [formatted_string, color].



43
44
45
46
47
48
49
50
# File 'lib/hawktui/streaming_table/column.rb', line 43

def format_cell(cell)
  str_value = cell.value.to_s
  if str_value.size > width
    [str_value[0...width - 1] + "", cell.color]
  else
    [str_value.ljust(width), cell.color]
  end
end