Class: Tailor::Formatters::Text

Inherits:
Tailor::Formatter show all
Includes:
Term::ANSIColor
Defined in:
lib/tailor/formatters/text.rb

Constant Summary collapse

PROBLEM_LEVEL_COLORS =
{
  error: 'red',
  warn: 'yellow'
}
MAX_STRING_SIZE =
68

Instance Method Summary collapse

Methods inherited from Tailor::Formatter

#initialize, #problem_levels, #problems_at_level

Constructor Details

This class inherits a constructor from Tailor::Formatter

Instance Method Details

#file_header(file) ⇒ String

Returns The portion of the header that displays the file info.

Returns:

  • (String)

    The portion of the header that displays the file info.



22
23
24
25
26
27
28
29
30
# File 'lib/tailor/formatters/text.rb', line 22

def file_header(file)
  file = Pathname(file)
  message = '# '
  message << underscore { "File:\n" }
  message << "#   #{file.relative_path_from(@pwd)}\n"
  message << "#\n"

  message
end

#file_report(problems, file_set_label) ⇒ Object

Prints the report on the file that just got checked.

Parameters:

  • problems (Hash)

    Value should be the file name; keys should be problems with the file.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tailor/formatters/text.rb', line 76

def file_report(problems, file_set_label)
  return if problems.values.first.empty?

  file = problems.keys.first
  problem_list = problems.values.first
  message = line
  message << file_header(file)
  message << file_set_header(file_set_label)
  message << problems_header(problem_list)

  message << "#\n"
  message << line

  puts message
end

#file_set_header(file_set) ⇒ String

Returns The portion of the header that displays the file_set label info.

Returns:

  • (String)

    The portion of the header that displays the file_set label info.



34
35
36
37
38
39
40
41
# File 'lib/tailor/formatters/text.rb', line 34

def file_set_header(file_set)
  message = '# '
  message << underscore { "File Set:\n" }
  message << "#   #{file_set}\n"
  message << "#\n"

  message
end

#line(length = 78) ⇒ String

Returns A line of “#—-#-”, with length determined by length.

Returns:

  • (String)

    A line of “#—-#-”, with length determined by length.



17
18
19
# File 'lib/tailor/formatters/text.rb', line 17

def line(length=78)
  "##{'-' * length}#\n"
end

#position(line, column) ⇒ String

Returns The position the problem was found at.

Parameters:

  • line (Fixnum)
  • column (Fixnum)

Returns:

  • (String)

    The position the problem was found at.



68
69
70
# File 'lib/tailor/formatters/text.rb', line 68

def position(line, column)
  line == '<EOF>' ? '<EOF>' : "#{line}:#{column}"
end

#problems_header(problem_list) ⇒ String

Returns The portion of the report that displays all of the problems for the file.

Returns:

  • (String)

    The portion of the report that displays all of the problems for the file.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tailor/formatters/text.rb', line 45

def problems_header(problem_list)
  message = '# '
  message << underscore { "Problems:\n" }

  problem_list.each_with_index do |problem, i|
    color = PROBLEM_LEVEL_COLORS[problem[:level]] || 'white'

    position = position(problem[:line], problem[:column])
    message << '#  ' + bold { "#{(i + 1)}." } + "\n"
    message << '#    * position:  '
    message << bold { instance_eval("#{color} position") } + "\n"
    message << '#    * property:  '
    message << instance_eval("#{color} problem[:type].to_s") + "\n"
    message << '#    * message:   '
    message << instance_eval("#{color} problem[:message].to_s") + "\n"
  end

  message
end

#summary_file_line(file, problem_list) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/tailor/formatters/text.rb', line 140

def summary_file_line(file, problem_list)
  file = Pathname(file)
  relative_path = file.relative_path_from(@pwd)
  problem_count = problem_list.size

  "#{summary_first_col(relative_path)} | " +
    problem_count.to_s.rjust(5) + ' '
end

#summary_first_col(path, string_size = MAX_STRING_SIZE) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/tailor/formatters/text.rb', line 149

def summary_first_col(path, string_size=MAX_STRING_SIZE)
  fp = path.to_s.ljust(string_size)
  offset = fp.size - string_size
  end_of_string = fp[offset..-1]
  end_of_string.sub!(/^.{3}/, '...') unless offset.zero?

  end_of_string
end

#summary_headerObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tailor/formatters/text.rb', line 126

def summary_header
  summary_table = line
  summary_table << '# '
  summary_table << bold { 'Tailor Summary'.rjust(40)  }
  summary_table << "|\n".rjust(39)
  summary_table << line
  summary_table << '#   ' << summary_first_col('File', 67) + '| '
  summary_table << 'Probs'.rjust(1)
  summary_table << " |\n".rjust(2)
  summary_table << line

  summary_table
end

#summary_level_totals(problems) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tailor/formatters/text.rb', line 158

def summary_level_totals(problems)
  return '' if total_problems(problems).zero?

  output = problem_levels(problems).inject('') do |result, level|
    color = PROBLEM_LEVEL_COLORS[level] || 'white'

    result << '#   '
    result << instance_eval("#{color} { summary_first_col(level.capitalize, 67) }")
    result << '|'
    result << instance_eval("#{color} { problems_at_level(problems, level).size.to_s.rjust(6) }")
    result << " |\n"
  end

  output << line

  output
end

#summary_report(problems) ⇒ Object

Prints the report on all of the files that just got checked.

Parameters:

  • problems (Hash)

    Values are filenames; keys are problems for each of those files.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tailor/formatters/text.rb', line 98

def summary_report(problems)
  summary_table = summary_header
  i = 0

  problems.each do |file, problem_list|
    report_line = summary_file_line(file, problem_list)

    report_line = if i % 2 == 1
      on_intense_black { report_line }
    else
      bold { report_line }
    end

    summary_table << '# ' << report_line << reset << "|\n"
    i += 1
  end

  summary_table << line
  summary_table << summary_level_totals(problems)
  summary_table << '#   ' << bold{ summary_first_col('TOTAL', 67) }
  summary_table << '|'
  summary_table << bold { total_problems(problems).to_s.rjust(6) }
  summary_table << " |\n"
  summary_table << line

  puts summary_table
end

#total_problems(problems) ⇒ Object



176
177
178
# File 'lib/tailor/formatters/text.rb', line 176

def total_problems(problems)
  problems.values.flatten.size
end