class TablePrint::FixedWidthFormatter
Public Instance Methods
format(value)
click to toggle source
# File lib/table_print-patch-pr70/formatter.rb, line 6 def format(value) padding = width - length(escape_strip(value).to_s) truncate(value) + (padding < 0 ? '' : " " * padding) end
Private Instance Methods
escape_strip(string, return_stripped_stuff = false)
click to toggle source
# File lib/table_print-patch-pr70/formatter.rb, line 22 def escape_strip(string, return_stripped_stuff = false) return string unless string.class == String stripped_stuff = '' string_stripped = string.gsub(/\e\[([0-9]{1,2};){0,2}[0-9]{1,2}m/) do |s| stripped_stuff << s s = '' end return string_stripped, stripped_stuff if return_stripped_stuff == true string_stripped end
truncate(value)
click to toggle source
# File lib/table_print-patch-pr70/formatter.rb, line 13 def truncate(value) return "" unless value value = value.to_s value_stripped, stripped_stuff = escape_strip(value, true) return value unless value_stripped.length > width "#{value[0..(width + stripped_stuff.length) - 4]}..." end