Class: CommandKit::Printing::Tables::CellBuilder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/command_kit/printing/tables/cell_builder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Build's a cell and calculates it's dimensions.

Since:

  • 0.4.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ CellBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the cell.

Parameters:

  • value (#to_s) (defaults to: nil)

    The value for the cell.

Since:

  • 0.4.0



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 34

def initialize(value=nil)
  @lines  = []
  @height = 0
  @width  = 0

  if value
    value.to_s.each_line(chomp: true) do |line|
      self << line
    end
  end
end

Instance Attribute Details

#heightInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The height (in lines) of the cell.

Returns:

  • (Integer)

Since:

  • 0.4.0



21
22
23
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 21

def height
  @height
end

#linesArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The lines within the cell.

Returns:

  • (Array<String>)

Since:

  • 0.4.0



16
17
18
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 16

def lines
  @lines
end

#widthInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The with (in characters) of the cell.

Returns:

  • (Integer)

Since:

  • 0.4.0



26
27
28
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 26

def width
  @width
end

Class Method Details

.line_width(string) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculates the width of a string, sans any ASNI escape sequences.

Parameters:

  • string (String)

    The string to calculate the width of.

Returns:

  • (Integer)

    The display width of the string.

Since:

  • 0.4.0



55
56
57
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 55

def self.line_width(string)
  string.gsub(/\e\[\d+m/,'').length
end

Instance Method Details

#<<(line) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a line to the cell.

Parameters:

  • line (String)

    A line to add to the cell.

Returns:

  • (self)

Since:

  • 0.4.0



67
68
69
70
71
72
73
74
75
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 67

def <<(line)
  line_width = self.class.line_width(line)

  @height += 1
  @width   = line_width if line_width > @width

  @lines << line
  return self
end

#[](line_index) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches a line from the cell.

Parameters:

  • line_index (Integer)

    The line index to fetch.

Returns:

  • (String)

    The line at the line index or an empty String if the cell is empty.

Since:

  • 0.4.0



86
87
88
# File 'lib/command_kit/printing/tables/cell_builder.rb', line 86

def [](line_index)
  @lines.fetch(line_index,'')
end