Sha256: 8d6b545255cc8836137c93281c6cbd1b69026e98eec57da2d357c8dbb9a3e6b8

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# -*- encoding : utf-8 -*-

require "terminal-table"

module RedisScanner
  class Formatter
    def initialize(options)
      @options = options
    end

    def format(patterns)
      if @options[:format] == "simple"
        simple patterns
      else
        table patterns
      end
    end

    private

    def touch_limit?(count)
      @options[:limit] &&
        @options[:limit].to_i > 0 &&
        count >= @options[:limit].to_i
    end

    def simple(patterns)
      ret = ""
      count = 0
      patterns.each do |pattern|
        ret << pattern.to_s
        ret << "\n"
        count += 1
        break if touch_limit?(count)
      end
      ret
    end

    def table(patterns)
      with_detail = @options[:detail]
      rows = []
      count = 0

      patterns.each do |pattern|
        if with_detail
          pattern.sorted_items.each do |item|
            rows << [pattern.name, item.type, item.count, item.size, item.avg_size]
          end
        else
          rows << [pattern.name, pattern.total]
        end
        count += 1
        break if touch_limit?(count)
      end


      if with_detail
        headings = %w(Key Type Count Size AvgSize)
      else
        headings = %w(Key Count)
      end

      table = Terminal::Table.new headings: headings, rows: rows

      if with_detail
        4.times {|i| table.align_column(i+2, :right) }
      else
        table.align_column(1, :right)
      end

      table.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis_scanner-0.1.6 lib/redis_scanner/formatter.rb
redis_scanner-0.1.5 lib/redis_scanner/formatter.rb