Class: Canon::DiffFormatter::ByLine::SimpleFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- Canon::DiffFormatter::ByLine::SimpleFormatter
- Defined in:
- lib/canon/diff_formatter/by_line/simple_formatter.rb
Overview
Simple line-based formatter (fallback) Uses basic LCS diff without format-specific intelligence
Instance Attribute Summary
Attributes inherited from BaseFormatter
#context_lines, #diff_grouping_lines, #diff_mode, #legacy_terminal, #show_diffs, #use_color, #visualization_map
Instance Method Summary collapse
-
#format(doc1, doc2) ⇒ String
Format simple line-by-line diff.
Methods inherited from BaseFormatter
#apply_bg, #apply_color, #apply_theme_style, #changed_content_styles, #compute_line_num_width, #content_style, #display_mode, for_format, #initialize, #marker_style, #normalize_color_for_rainbow, #structure_color, #structure_styles, #styled_marker, #theme, #theme_color, #theme_style, #unchanged_content_style, #visualization_chars
Constructor Details
This class inherits a constructor from Canon::DiffFormatter::ByLine::BaseFormatter
Instance Method Details
#format(doc1, doc2) ⇒ String
Format simple line-by-line diff
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/canon/diff_formatter/by_line/simple_formatter.rb', line 17 def format(doc1, doc2) compute_line_num_width(doc1, doc2) output = [] # Use split with -1 to preserve trailing empty strings (from trailing \n) lines1 = doc1.split("\n", -1) lines2 = doc2.split("\n", -1) # Detect non-ASCII characters in the diff all_text = (lines1 + lines2).join non_ascii = Legend.detect_non_ascii(all_text, @visualization_map) # Add Unicode legend if any non-ASCII characters detected unless non_ascii.empty? output << Legend.build_legend(non_ascii, use_color: @use_color) output << "" end # Get LCS diff diffs = ::Diff::LCS.sdiff(lines1, lines2) # Group into hunks with context hunks = build_hunks(diffs, lines1, lines2, context_lines: @context_lines) # Format each hunk hunks.each do |hunk| output << format_hunk(hunk) end output.join("\n") end |