Module: Canon::DiffFormatter::Legend

Defined in:
lib/canon/diff_formatter/legend.rb

Overview

Module for building Unicode character visualization legends

Class Method Summary collapse

Class Method Details

.build_diff_symbol_legend(use_color: true) ⇒ String

Build diff symbol legend

Parameters:

  • use_color (Boolean) (defaults to: true)

    Whether to use colors

Returns:

  • (String)

    Formatted diff symbol legend



95
96
97
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
125
126
127
128
129
130
131
# File 'lib/canon/diff_formatter/legend.rb', line 95

def self.build_diff_symbol_legend(use_color: true)
  output = []
  separator = "" * 60

  output << colorize("Diff Symbol Legend:", :cyan, :bold, use_color)
  output << colorize(separator, :cyan, :bold, use_color)

  # Formatting-only changes
  output << colorize("Formatting Changes (cosmetic only):", :yellow,
                     :bold, use_color)
  output << "  #{colorize('[', :black, :bold,
                          use_color)}: Line removed (formatting only - dark gray)"
  output << "  #{colorize(']', :white, :bold,
                          use_color)}: Line added (formatting only - light gray)"
  output << ""

  # Informative changes
  output << colorize("Informative Changes (do not affect equivalence):",
                     :yellow, :bold, use_color)
  output << "  #{colorize('<', :blue, :bold,
                          use_color)}: Line removed (informative - blue)"
  output << "  #{colorize('>', :cyan, :bold,
                          use_color)}: Line added (informative - cyan)"
  output << ""

  # Normative changes
  output << colorize("Normative Changes (affect equivalence):", :yellow,
                     :bold, use_color)
  output << "  #{colorize('-', :red, :bold,
                          use_color)}: Line removed (normative difference - red)"
  output << "  #{colorize('+', :green, :bold,
                          use_color)}: Line added (normative difference - green)"
  output << ""

  output << colorize(separator, :cyan, :bold, use_color)
  output.join("\n")
end

.build_legend(detected_chars, use_color: true) ⇒ String?

Build formatted legend from detected characters

Parameters:

  • detected_chars (Hash)

    Hash from detect_non_ascii

  • use_color (Boolean) (defaults to: true)

    Whether to use colors

Returns:

  • (String, nil)

    Formatted legend or nil if no characters



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/canon/diff_formatter/legend.rb', line 51

def self.build_legend(detected_chars, use_color: true)
  return nil if detected_chars.empty?

  # Group characters by category
  grouped = detected_chars.group_by { |_char, info| info[:category] }

  output = []
  separator = "" * 60

  output << colorize("Character Visualization Legend:", :cyan, :bold,
                     use_color)
  output << colorize(separator, :cyan, :bold, use_color)

  # Display each category
  category_names = DiffFormatter::CHARACTER_CATEGORY_NAMES
  category_names.each do |category_key, category_name|
    chars = grouped[category_key]
    next unless chars

    output << colorize("#{category_name}:", :yellow, :bold, use_color)

    chars.sort_by { |char, _info| char.ord }.each do |char, info|
      # Format: '⏓': U+2005 (' ') Four-Per-Em Space
      vis = info[:visualization]
      code = info[:codepoint]
      name = format_name(info[:name])

      # Show original character in quotes, handling special cases
      original = format_original_char(char)

      line = "  '#{vis}': #{code} ('#{original}') #{name}"
      output << (use_color ? line : line)
    end
    output << ""
  end

  output << colorize(separator, :cyan, :bold, use_color)
  output.join("\n")
end

.detect_non_ascii(text, visualization_map) ⇒ Hash

Detect non-ASCII characters in text and return their information

Parameters:

  • text (String)

    Text to analyze

  • visualization_map (Hash)

    Character visualization map

Returns:

  • (Hash)

    Hash of characters with their metadata



14
15
16
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
# File 'lib/canon/diff_formatter/legend.rb', line 14

def self.detect_non_ascii(text, visualization_map)
  detected = {}
  category_map = DiffFormatter::CHARACTER_CATEGORY_MAP
   = DiffFormatter::CHARACTER_METADATA

  text.each_char do |char|
    next if char.ord <= 127
    next if detected.key?(char)

    visualization = visualization_map.fetch(char, char)
    next if visualization == char # Skip if no visualization mapping

    codepoint = format("U+%04X", char.ord)

    # Use name from metadata if available, otherwise use Unicode::Name
    name = if [char] && [char][:name]
             [char][:name]
           else
             Unicode::Name.of(char) || "UNKNOWN"
           end

    detected[char] = {
      visualization: visualization,
      codepoint: codepoint,
      name: name,
      category: category_map.fetch(char, :control),
    }
  end

  detected
end