Sha256: 5dcc3864b7ab3a75c7f498fb761178af1bc3550b24cccdef67593faab194edaa

Contents?: true

Size: 801 Bytes

Versions: 6

Compression:

Stored size: 801 Bytes

Contents

module TablePrint
  class TimeFormatter
    def initialize(time_format=nil)
      @format = time_format
      @format ||= TablePrint::Config.time_format
    end

    def format(value)
      return value unless value.is_a? Time
      value.strftime @format
    end
  end

  class NoNewlineFormatter
    def format(value)
      value.to_s.gsub(/\r\n/, "\n").gsub(/\n/, " ")
    end
  end

  class FixedWidthFormatter
    def initialize(width)
      @width = width
    end

    def format(value)
      "%-#{width}s" % truncate(value)
    end

    def width
      [@width, TablePrint::Config.max_width].min
    end

    private
    def truncate(value)
      return "" unless value

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
table_print-1.1.3 lib/table_print/formatter.rb
table_print-1.1.2 lib/table_print/formatter.rb
table_print-1.1.1 lib/table_print/formatter.rb
table_print-1.1.0 lib/table_print/formatter.rb
table_print-1.0.1 lib/table_print/formatter.rb
table_print-1.0.0 lib/table_print/formatter.rb