Class: SimpleCovMcp::Formatters::SourceFormatter
- Inherits:
-
Object
- Object
- SimpleCovMcp::Formatters::SourceFormatter
- Defined in:
- lib/simplecov_mcp/formatters/source_formatter.rb
Instance Attribute Summary collapse
-
#color_enabled ⇒ Object
readonly
Returns the value of attribute color_enabled.
Instance Method Summary collapse
- #build_source_payload(model, path, mode: nil, context: 2) ⇒ Object
- #build_source_rows(src_lines, cov_lines, mode:, context: 2) ⇒ Object
- #format_detailed_rows(rows) ⇒ Object
- #format_source_for(model, path, mode: nil, context: 2) ⇒ Object
- #format_source_rows(rows) ⇒ Object
-
#initialize(color_enabled: true) ⇒ SourceFormatter
constructor
A new instance of SourceFormatter.
Constructor Details
#initialize(color_enabled: true) ⇒ SourceFormatter
Returns a new instance of SourceFormatter.
6 7 8 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 6 def initialize(color_enabled: true) @color_enabled = color_enabled end |
Instance Attribute Details
#color_enabled ⇒ Object (readonly)
Returns the value of attribute color_enabled.
92 93 94 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 92 def color_enabled @color_enabled end |
Instance Method Details
#build_source_payload(model, path, mode: nil, context: 2) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 30 def build_source_payload(model, path, mode: nil, context: 2) raw = fetch_raw(model, path) return nil unless raw abs = raw['file'] lines_cov = raw['lines'] src = File.file?(abs) ? File.readlines(abs, chomp: true) : nil return nil unless src build_source_rows(src, lines_cov, mode: mode, context: context) end |
#build_source_rows(src_lines, cov_lines, mode:, context: 2) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 42 def build_source_rows(src_lines, cov_lines, mode:, context: 2) # Normalize inputs defensively to avoid type errors in formatting coverage_lines = cov_lines || [] context_line_count = begin context.to_i rescue 2 end raise ArgumentError, 'Context lines cannot be negative' if context_line_count.negative? n = src_lines.length include_line = Array.new(n, mode == :full) if mode == :uncovered include_line = mark_uncovered_lines_with_context(coverage_lines, context_line_count, n) end build_row_data(src_lines, coverage_lines, include_line) end |
#format_detailed_rows(rows) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 81 def format_detailed_rows(rows) # Simple aligned columns: line, hits, covered out = [] out << format('%6s %6s %7s', 'Line', 'Hits', 'Covered') out << format('%6s %6s %7s', '-----', '----', '-------') rows.each do |r| out << format('%6d %6d %5s', r['line'], r['hits'], r['covered'] ? 'yes' : 'no') end out.join("\n") end |
#format_source_for(model, path, mode: nil, context: 2) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 10 def format_source_for(model, path, mode: nil, context: 2) raw = fetch_raw(model, path) return '[source not available]' unless raw abs = raw['file'] lines_cov = raw['lines'] src = File.file?(abs) ? File.readlines(abs, chomp: true) : nil return '[source not available]' unless src begin rows = build_source_rows(src, lines_cov, mode: mode, context: context) format_source_rows(rows) rescue ArgumentError raise rescue # If any unexpected formatting/indexing error occurs, avoid crashing the CLI '[source not available]' end end |
#format_source_rows(rows) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/simplecov_mcp/formatters/source_formatter.rb', line 61 def format_source_rows(rows) marker = ->(covered, _hits) do case covered when true then colorize('✓', :green) when false then colorize('·', :red) else colorize(' ', :dim) end end lines = [] lines << format('%6s %2s | %s', 'Line', ' ', 'Source') lines << format('%6s %2s-+-%s', '------', '--', '-' * 60) rows.each do |r| m = marker.call(r['covered'], r['hits']) lines << format('%6d %2s | %s', r['line'], m, r['code']) end lines.join("\n") end |