class TablePrint::FixedWidthFormatter

taken from github.com/arches/table_print/pull/45

Public Instance Methods

format(value) click to toggle source
# File lib/mosespa.rb, line 19
def format(value)
  padding = width - strip_escape(value.to_s).each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+)
  truncate(value) + (padding < 0 ? '' : " " * padding)
end
strip_escape(value) click to toggle source
# File lib/mosespa.rb, line 15
def strip_escape(value)
  value.gsub(%r{\e[^m]*m}, '')
end
truncate(value) click to toggle source
# File lib/mosespa.rb, line 24
def truncate(value)
  return "" unless value
  value = value.to_s
  return value unless strip_escape(value).length > width
  "#{value[0..width-4]}..."
end