Class: Aidp::Skills::Wizard::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/skills/wizard/differ.rb

Overview

Generates and displays diffs between skills

Shows differences between an original skill and a modified version, or between a project skill and its template.

Examples:

Showing a diff

differ = Differ.new
diff = differ.diff(original_skill, modified_skill)
differ.display(diff)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDiffer

Returns a new instance of Differ.



21
22
23
# File 'lib/aidp/skills/wizard/differ.rb', line 21

def initialize
  @pastel = Pastel.new
end

Instance Attribute Details

#pastelObject (readonly)

Returns the value of attribute pastel.



19
20
21
# File 'lib/aidp/skills/wizard/differ.rb', line 19

def pastel
  @pastel
end

Instance Method Details

#compare_with_template(project_skill, template_skill) ⇒ Hash

Compare a project skill with its template

Parameters:

  • project_skill (Skill)

    Project skill

  • template_skill (Skill)

    Template skill

Returns:

  • (Hash)

    Comparison information



113
114
115
116
117
118
119
120
# File 'lib/aidp/skills/wizard/differ.rb', line 113

def compare_with_template(project_skill, template_skill)
  {
    skill_id: project_skill.id,
    overrides: detect_overrides(project_skill, template_skill),
    additions: detect_additions(project_skill, template_skill),
    diff: diff(template_skill, project_skill)
  }
end

#diff(original, modified) ⇒ Hash

Generate a diff between two skills

Parameters:

  • original (Skill, String)

    Original skill or content

  • modified (Skill, String)

    Modified skill or content

Returns:

  • (Hash)

    Diff information



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aidp/skills/wizard/differ.rb', line 30

def diff(original, modified)
  original_content = skill_to_content(original)
  modified_content = skill_to_content(modified)

  {
    original: original_content,
    modified: modified_content,
    lines: generate_line_diff(original_content, modified_content),
    has_changes: original_content != modified_content
  }
end

#display(diff, output: $stdout) ⇒ Object

Display a diff to the terminal

Parameters:

  • diff (Hash)

    Diff information from #diff

  • output (IO) (defaults to: $stdout)

    Output stream (default: $stdout)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aidp/skills/wizard/differ.rb', line 46

def display(diff, output: $stdout)
  unless diff[:has_changes]
    output.puts pastel.dim("No differences found")
    return
  end

  output.puts pastel.bold("\n" + "=" * 60)
  output.puts pastel.bold("Skill Diff")
  output.puts pastel.bold("=" * 60)

  diff[:lines].each do |line_info|
    case line_info[:type]
    when :context
      output.puts pastel.dim("  #{line_info[:line]}")
    when :add
      output.puts pastel.green("+ #{line_info[:line]}")
    when :remove
      output.puts pastel.red("- #{line_info[:line]}")
    end
  end

  output.puts pastel.bold("=" * 60 + "\n")
end

#unified_diff(original, modified) ⇒ String

Generate a unified diff string

Parameters:

  • original (Skill, String)

    Original skill or content

  • modified (Skill, String)

    Modified skill or content

Returns:

  • (String)

    Unified diff format



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
101
102
103
104
105
106
# File 'lib/aidp/skills/wizard/differ.rb', line 75

def unified_diff(original, modified)
  original_content = skill_to_content(original)
  modified_content = skill_to_content(modified)

  original_lines = original_content.lines
  modified_lines = modified_content.lines

  diff_lines = []
  diff_lines << "--- original"
  diff_lines << "+++ modified"

  # Simple line-by-line diff (not optimal but functional)
  max_lines = [original_lines.size, modified_lines.size].max

  (0...max_lines).each do |i|
    orig_line = original_lines[i]
    mod_line = modified_lines[i]

    if orig_line && !mod_line
      diff_lines << "-#{orig_line}"
    elsif !orig_line && mod_line
      diff_lines << "+#{mod_line}"
    elsif orig_line != mod_line
      diff_lines << "-#{orig_line}"
      diff_lines << "+#{mod_line}"
    else
      diff_lines << " #{orig_line}"
    end
  end

  diff_lines.join
end