Module: Canon::DiffFormatter::Legend
- Defined in:
- lib/canon/diff_formatter/legend.rb
Overview
Module for building Unicode character visualization legends
Class Method Summary collapse
-
.build_diff_symbol_legend(use_color: true) ⇒ String
Build diff symbol legend.
-
.build_legend(detected_chars, use_color: true) ⇒ String?
Build formatted legend from detected characters.
-
.detect_non_ascii(text, visualization_map) ⇒ Hash
Detect non-ASCII characters in text and return their information.
Class Method Details
.build_diff_symbol_legend(use_color: true) ⇒ String
Build diff symbol legend
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 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/canon/diff_formatter/legend.rb', line 106 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
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 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/canon/diff_formatter/legend.rb', line 62 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
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 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/canon/diff_formatter/legend.rb', line 14 def self.detect_non_ascii(text, visualization_map) # each_char allocates a 1-char String per character — over two # full documents per render that is the presentation stage's # biggest allocation source. ASCII-only text (the common case) # needs no scan at all; otherwise scan codepoints and # materialize the character String only at non-ASCII offsets. return {} if text.ascii_only? detected = {} category_map = DiffFormatter::CHARACTER_CATEGORY_MAP = DiffFormatter::CHARACTER_METADATA index = -1 text.each_codepoint do |codepoint| index += 1 next if codepoint <= 127 char = text[index, 1] 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 |