colorful_tables 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/{column.rb → colorful_tables/column.rb} +2 -1
- data/lib/{enumerable.rb → colorful_tables/enumerable.rb} +0 -0
- data/lib/{errors.rb → colorful_tables/errors.rb} +0 -0
- data/lib/{misc.rb → colorful_tables/misc.rb} +0 -0
- data/lib/{object.rb → colorful_tables/object.rb} +3 -1
- data/lib/{table.rb → colorful_tables/table.rb} +47 -15
- data/lib/colorful_tables/util.rb +34 -0
- data/lib/colorful_tables/version.rb +1 -1
- data/lib/colorful_tables.rb +7 -6
- metadata +11 -10
| 
            File without changes
         | 
| 
            File without changes
         | 
| 
            File without changes
         | 
| @@ -1,6 +1,8 @@ | |
| 1 | 
            +
            # Copied from rails
         | 
| 2 | 
            +
            # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
         | 
| 3 | 
            +
             | 
| 1 4 | 
             
            class Object
         | 
| 2 5 |  | 
| 3 | 
            -
              # From https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
         | 
| 4 6 | 
             
              def try(*a, &b)
         | 
| 5 7 | 
             
                if a.empty? && block_given?
         | 
| 6 8 | 
             
                  yield self
         | 
| @@ -13,27 +13,37 @@ module ColorfulTables | |
| 13 13 | 
             
                attr_accessor :data, :options
         | 
| 14 14 |  | 
| 15 15 | 
             
                def initialize(data, options = {})
         | 
| 16 | 
            -
                  raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
         | 
| 17 | 
            -
                  @data | 
| 18 | 
            -
                  @options | 
| 19 | 
            -
                  @border_chars | 
| 20 | 
            -
                  @columns_width | 
| 16 | 
            +
                  #raise RuntimeError, "data must be an array of columns" unless is_an_array_of_columns?(data)
         | 
| 17 | 
            +
                  @data            = data
         | 
| 18 | 
            +
                  @options         = DEFAULT_OPTIONS.merge(options)
         | 
| 19 | 
            +
                  @border_chars    ||= calculate_border_chars
         | 
| 20 | 
            +
                  @columns_width   ||= calculate_columns_width
         | 
| 21 21 | 
             
                  @columns_options ||= calculate_columns_options
         | 
| 22 | 
            +
                  @terminal_heigth ||= ColorfulTables.detect_terminal_size[1]
         | 
| 23 | 
            +
                  @transposed_data ||= self.data.map(&:data).transpose
         | 
| 22 24 | 
             
                end
         | 
| 23 25 |  | 
| 24 | 
            -
                def  | 
| 25 | 
            -
                   | 
| 26 | 
            -
                   | 
| 27 | 
            -
                     | 
| 26 | 
            +
                def page(n)
         | 
| 27 | 
            +
                  return nil if n >= total_pages
         | 
| 28 | 
            +
                  page_output = @transposed_data[page_ranges[n]].inject("\r" + header) do |output, row|
         | 
| 29 | 
            +
                    output += format(row)
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                  page_output += separator
         | 
| 32 | 
            +
                  page_output
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                def paginate
         | 
| 36 | 
            +
                  total_pages.times do |n|
         | 
| 37 | 
            +
                    puts page(n)
         | 
| 38 | 
            +
                    print "-- Press ENTER --"
         | 
| 39 | 
            +
                    STDIN.getc
         | 
| 28 40 | 
             
                  end
         | 
| 29 | 
            -
                  t += separator
         | 
| 30 | 
            -
                  t
         | 
| 31 41 | 
             
                end
         | 
| 32 42 |  | 
| 33 43 | 
             
                private
         | 
| 34 44 |  | 
| 35 45 | 
             
                def is_an_array_of_columns?(data)
         | 
| 36 | 
            -
                  array_of_booleans = data.try(:map) { |i| i.is_a? Column }
         | 
| 46 | 
            +
                  array_of_booleans = data.try(:map) { |i| i.is_a? ColorfulTables::Column }
         | 
| 37 47 | 
             
                  array_of_booleans.nil? ? false : !(array_of_booleans.include? false)
         | 
| 38 48 | 
             
                end
         | 
| 39 49 |  | 
| @@ -51,13 +61,17 @@ module ColorfulTables | |
| 51 61 | 
             
                end
         | 
| 52 62 |  | 
| 53 63 | 
             
                def header
         | 
| 54 | 
            -
                  separator + format(@data.map(&:header)) + separator
         | 
| 64 | 
            +
                  separator + format(@data.map(&:header), :in_header) + separator
         | 
| 55 65 | 
             
                end
         | 
| 56 66 |  | 
| 57 | 
            -
                def format(row_elements)
         | 
| 67 | 
            +
                def format(row_elements, in_header = false)
         | 
| 58 68 | 
             
                  output = row_elements.inject_with_index(@border_chars[:vertical]) do |s, element, index|
         | 
| 59 69 | 
             
                    value = ColorfulTables.truncate_if_float(element)
         | 
| 60 | 
            -
                    color =  | 
| 70 | 
            +
                    color = if in_header
         | 
| 71 | 
            +
                      @columns_options[index][:header_color]
         | 
| 72 | 
            +
                    else
         | 
| 73 | 
            +
                      @columns_options[index][:data_color].is_a?(Proc) ? @columns_options[index][:data_color].call(value) : @columns_options[index][:data_color]
         | 
| 74 | 
            +
                    end
         | 
| 61 75 | 
             
                    s += " #{value.to_s.rjust(@columns_width[index]).colorize(color)} #{@border_chars[:vertical]}"
         | 
| 62 76 | 
             
                  end
         | 
| 63 77 | 
             
                  output + "\n"
         | 
| @@ -70,6 +84,24 @@ module ColorfulTables | |
| 70 84 | 
             
                    :cross =>      '+'.colorize(color) }
         | 
| 71 85 | 
             
                end
         | 
| 72 86 |  | 
| 87 | 
            +
                def page_ranges
         | 
| 88 | 
            +
                  ranges = []
         | 
| 89 | 
            +
                  Range.new(0,total_rows,:exclude_last).each_slice(rows_per_page) {|r| ranges << Range.new(r.first,r.last) }
         | 
| 90 | 
            +
                  ranges
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                def rows_per_page
         | 
| 94 | 
            +
                  @terminal_heigth - 5
         | 
| 95 | 
            +
                end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                def total_rows
         | 
| 98 | 
            +
                  @transposed_data.size
         | 
| 99 | 
            +
                end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                def total_pages
         | 
| 102 | 
            +
                  Rational(total_rows, rows_per_page).ceil
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 73 105 | 
             
              end
         | 
| 74 106 |  | 
| 75 107 | 
             
            end
         | 
| @@ -0,0 +1,34 @@ | |
| 1 | 
            +
            # Copied from hirb gem
         | 
| 2 | 
            +
            # https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module ColorfulTables
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class << self
         | 
| 7 | 
            +
                
         | 
| 8 | 
            +
                # Determines if a shell command exists by searching for it in ENV['PATH'].
         | 
| 9 | 
            +
                def command_exists?(command)
         | 
| 10 | 
            +
                  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                # Returns [width, height] of terminal when detected, nil if not detected.
         | 
| 14 | 
            +
                # Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
         | 
| 15 | 
            +
                def detect_terminal_size
         | 
| 16 | 
            +
                  if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
         | 
| 17 | 
            +
                    [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
         | 
| 18 | 
            +
                  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
         | 
| 19 | 
            +
                    [`tput cols`.to_i, `tput lines`.to_i]
         | 
| 20 | 
            +
                  elsif STDIN.tty? && command_exists?('stty')
         | 
| 21 | 
            +
                    `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
         | 
| 22 | 
            +
                  else
         | 
| 23 | 
            +
                    nil
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                rescue
         | 
| 26 | 
            +
                  nil
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
             | 
| 32 | 
            +
             | 
| 33 | 
            +
              
         | 
| 34 | 
            +
            end
         | 
    
        data/lib/colorful_tables.rb
    CHANGED
    
    | @@ -1,11 +1,12 @@ | |
| 1 1 | 
             
            module ColorfulTables
         | 
| 2 2 |  | 
| 3 3 | 
             
              require 'colorize'
         | 
| 4 | 
            -
              require 'object'
         | 
| 5 | 
            -
              require 'enumerable'
         | 
| 6 | 
            -
              require 'errors'
         | 
| 7 | 
            -
              require 'table'
         | 
| 8 | 
            -
              require 'column'
         | 
| 9 | 
            -
              require 'misc'
         | 
| 4 | 
            +
              require 'colorful_tables/object'
         | 
| 5 | 
            +
              require 'colorful_tables/enumerable'
         | 
| 6 | 
            +
              require 'colorful_tables/errors'
         | 
| 7 | 
            +
              require 'colorful_tables/table'
         | 
| 8 | 
            +
              require 'colorful_tables/column'
         | 
| 9 | 
            +
              require 'colorful_tables/misc'
         | 
| 10 | 
            +
              require 'colorful_tables/util'
         | 
| 10 11 |  | 
| 11 12 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: colorful_tables
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 4 | 
            +
              hash: 15
         | 
| 5 5 | 
             
              prerelease: false
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 | 
            -
              -  | 
| 8 | 
            +
              - 4
         | 
| 9 9 | 
             
              - 0
         | 
| 10 | 
            -
              version: 0. | 
| 10 | 
            +
              version: 0.4.0
         | 
| 11 11 | 
             
            platform: ruby
         | 
| 12 12 | 
             
            authors: 
         | 
| 13 13 | 
             
            - Javier Vidal
         | 
| @@ -15,7 +15,7 @@ autorequire: | |
| 15 15 | 
             
            bindir: bin
         | 
| 16 16 | 
             
            cert_chain: []
         | 
| 17 17 |  | 
| 18 | 
            -
            date: 2011-10- | 
| 18 | 
            +
            date: 2011-10-14 00:00:00 +02:00
         | 
| 19 19 | 
             
            default_executable: 
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -47,13 +47,14 @@ files: | |
| 47 47 | 
             
            - Rakefile
         | 
| 48 48 | 
             
            - colorful_tables.gemspec
         | 
| 49 49 | 
             
            - lib/colorful_tables.rb
         | 
| 50 | 
            +
            - lib/colorful_tables/column.rb
         | 
| 51 | 
            +
            - lib/colorful_tables/enumerable.rb
         | 
| 52 | 
            +
            - lib/colorful_tables/errors.rb
         | 
| 53 | 
            +
            - lib/colorful_tables/misc.rb
         | 
| 54 | 
            +
            - lib/colorful_tables/object.rb
         | 
| 55 | 
            +
            - lib/colorful_tables/table.rb
         | 
| 56 | 
            +
            - lib/colorful_tables/util.rb
         | 
| 50 57 | 
             
            - lib/colorful_tables/version.rb
         | 
| 51 | 
            -
            - lib/column.rb
         | 
| 52 | 
            -
            - lib/enumerable.rb
         | 
| 53 | 
            -
            - lib/errors.rb
         | 
| 54 | 
            -
            - lib/misc.rb
         | 
| 55 | 
            -
            - lib/object.rb
         | 
| 56 | 
            -
            - lib/table.rb
         | 
| 57 58 | 
             
            has_rdoc: true
         | 
| 58 59 | 
             
            homepage: ""
         | 
| 59 60 | 
             
            licenses: []
         |