class TablePrint::FixedWidthFormatter

Attributes

width[RW]

Public Class Methods

new(width) click to toggle source
# File lib/table_print/formatter.rb, line 23
def initialize(width)
  self.width = width
end

Public Instance Methods

format(value) click to toggle source
# File lib/table_print/formatter.rb, line 27
def format(value)
  padding = width - length(value.to_s)
  truncate(value) + (padding < 0 ? '' : " " * padding)
end

Private Instance Methods

length(str) click to toggle source
# File lib/table_print/formatter.rb, line 42
def length(str)
  if TablePrint::Config.multibyte
    str.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+)
  else
    str.length
  end
end
truncate(value) click to toggle source
# File lib/table_print/formatter.rb, line 33
def truncate(value)
  return "" unless value

  value = value.to_s
  return value unless value.length > width

  "#{value[0..width-4]}..."
end