Class: XCPerfect::Formatter

Inherits:
Object
  • Object
show all
Includes:
ANSI
Defined in:
lib/xcperfect/formatters/formatter.rb

Overview

Formatter is the base adopted by ‘all.rb` and `simple.rb`

Direct Known Subclasses

All, Simple

Constant Summary

Constants included from ANSI

ANSI::COLORS, ANSI::EFFECT, ANSI::FORMATTED_MATCHER

Instance Attribute Summary collapse

Attributes included from ANSI

#colorize

Instance Method Summary collapse

Methods included from ANSI

#ansi_parse, #applied_effects, #color_for, #colorize?, #cyan, #green, #red, #strip, #white, #yellow

Constructor Details

#initialize(use_ascii, colorize, json) ⇒ Formatter

Returns a new instance of Formatter.



12
13
14
15
16
17
# File 'lib/xcperfect/formatters/formatter.rb', line 12

def initialize(use_ascii, colorize, json)
  @use_ascii = use_ascii
  @colorize = colorize
  @parser = Parser.new(json)
  @terminal_width = Integer(`tput cols`)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



10
11
12
# File 'lib/xcperfect/formatters/formatter.rb', line 10

def parser
  @parser
end

Instance Method Details

#align(arrays) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/xcperfect/formatters/formatter.rb', line 52

def align(arrays)
  widths = align_formats(arrays)
  arrays.map do |row|
    row.each_with_index.map do |column, idx|
      column.ljust(widths[idx])
    end
  end
end

#align_formats(arrays) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xcperfect/formatters/formatter.rb', line 38

def align_formats(arrays)
  column_sizes = arrays.map(&:length)
  common_column_size = column_sizes[0]
  if column_sizes.to_set.length != 1
    puts column_sizes
    raise 'All rows must have same number of columns to be aligned'
  end

  (0...common_column_size).map do |column|
    width = arrays.map { |row| strip(row[column]).length }.max
    (width * 1.5).round
  end
end

#color_percentage(percentage) ⇒ Object



27
28
29
30
31
# File 'lib/xcperfect/formatters/formatter.rb', line 27

def color_percentage(percentage)
  color_func = color_for(percentage)
  str_rep = (percentage * 100).to_s + ' %'
  color_func.call(str_rep)
end

#pretty_coverage_info(target) ⇒ Object



33
34
35
36
# File 'lib/xcperfect/formatters/formatter.rb', line 33

def pretty_coverage_info(target)
  name, covered, total, percentage = @parser.extract_coverage_info(target)
  [name, covered.to_s, total.to_s, color_percentage(percentage)]
end

#pretty_format(___) ⇒ Object



19
20
21
# File 'lib/xcperfect/formatters/formatter.rb', line 19

def pretty_format(___)
  puts @json
end

#spaces(cnt) ⇒ Object



23
24
25
# File 'lib/xcperfect/formatters/formatter.rb', line 23

def spaces(cnt)
  ' ' * cnt
end