Class: Tailor::Formatters::Text
- Inherits:
-
Tailor::Formatter
- Object
- Tailor::Formatter
- Tailor::Formatters::Text
- 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
-
#file_header(file) ⇒ String
The portion of the header that displays the file info.
-
#file_report(problems, file_set_label) ⇒ Object
Prints the report on the file that just got checked.
-
#file_set_header(file_set) ⇒ String
The portion of the header that displays the file_set label info.
-
#line(length = 78) ⇒ String
A line of “#—-#-”, with length determined by
length
. -
#position(line, column) ⇒ String
The position the problem was found at.
-
#problems_header(problem_list) ⇒ String
The portion of the report that displays all of the problems for the file.
- #summary_file_line(file, problem_list) ⇒ Object
- #summary_first_col(path, string_size = MAX_STRING_SIZE) ⇒ Object
- #summary_header ⇒ Object
- #summary_level_totals(problems) ⇒ Object
-
#summary_report(problems) ⇒ Object
Prints the report on all of the files that just got checked.
- #total_problems(problems) ⇒ Object
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.
22 23 24 25 26 27 28 29 30 |
# File 'lib/tailor/formatters/text.rb', line 22 def file_header(file) file = Pathname(file) = '# ' << underscore { "File:\n" } << "# #{file.relative_path_from(@pwd)}\n" << "#\n" end |
#file_report(problems, file_set_label) ⇒ Object
Prints the report on the file that just got checked.
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 = line << file_header(file) << file_set_header(file_set_label) << problems_header(problem_list) << "#\n" << line puts end |
#file_set_header(file_set) ⇒ String
Returns 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) = '# ' << underscore { "File Set:\n" } << "# #{file_set}\n" << "#\n" end |
#line(length = 78) ⇒ String
Returns 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.
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.
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) = '# ' << underscore { "Problems:\n" } problem_list.each_with_index do |problem, i| color = PROBLEM_LEVEL_COLORS[problem[:level]] || 'white' position = position(problem[:line], problem[:column]) << '# ' + bold { "#{(i + 1)}." } + "\n" << '# * position: ' << bold { instance_eval("#{color} position") } + "\n" << '# * property: ' << instance_eval("#{color} problem[:type].to_s") + "\n" << '# * message: ' << instance_eval("#{color} problem[:message].to_s") + "\n" end 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_header ⇒ Object
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.
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 |