Class: PatienceDiff::Formatter
- Inherits:
-
Object
- Object
- PatienceDiff::Formatter
- Defined in:
- lib/patience_diff/formatter.rb
Overview
Formats a plaintext unified diff.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#left_name ⇒ Object
Returns the value of attribute left_name.
-
#left_timestamp ⇒ Object
Returns the value of attribute left_timestamp.
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#right_name ⇒ Object
Returns the value of attribute right_name.
-
#right_timestamp ⇒ Object
Returns the value of attribute right_timestamp.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #format {|context| ... } ⇒ Object
-
#initialize(differ, title = nil) ⇒ Formatter
constructor
A new instance of Formatter.
- #render_header(left_name = nil, right_name = nil, left_timestamp = nil, right_timestamp = nil) ⇒ Object
- #render_hunk(a, b, opcodes, last_line_shown) ⇒ Object
- #render_hunk_marker(opcodes) ⇒ Object
Constructor Details
#initialize(differ, title = nil) ⇒ Formatter
Returns a new instance of Formatter.
9 10 11 12 13 |
# File 'lib/patience_diff/formatter.rb', line 9 def initialize(differ, title = nil) @differ = differ @names = [] @title = title || "Diff generated on #{Time.now.strftime('%c')}" end |
Instance Attribute Details
#left_name ⇒ Object
Returns the value of attribute left_name.
7 8 9 |
# File 'lib/patience_diff/formatter.rb', line 7 def left_name @left_name end |
#left_timestamp ⇒ Object
Returns the value of attribute left_timestamp.
7 8 9 |
# File 'lib/patience_diff/formatter.rb', line 7 def @left_timestamp end |
#names ⇒ Object (readonly)
Returns the value of attribute names.
6 7 8 |
# File 'lib/patience_diff/formatter.rb', line 6 def names @names end |
#right_name ⇒ Object
Returns the value of attribute right_name.
7 8 9 |
# File 'lib/patience_diff/formatter.rb', line 7 def right_name @right_name end |
#right_timestamp ⇒ Object
Returns the value of attribute right_timestamp.
7 8 9 |
# File 'lib/patience_diff/formatter.rb', line 7 def @right_timestamp end |
#title ⇒ Object
Returns the value of attribute title.
7 8 9 |
# File 'lib/patience_diff/formatter.rb', line 7 def title @title end |
Instance Method Details
#format {|context| ... } ⇒ Object
15 16 17 18 19 |
# File 'lib/patience_diff/formatter.rb', line 15 def format context = FormattingContext.new(@differ, self) yield context context.format end |
#render_header(left_name = nil, right_name = nil, left_timestamp = nil, right_timestamp = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/patience_diff/formatter.rb', line 21 def render_header(left_name=nil, right_name=nil, =nil, =nil) @names << right_name @left_name = left_name || "Original" @right_name = right_name || "Current" @left_timestamp = || Time.now @right_timestamp = || Time.now [ left_header_line(@left_name, @left_timestamp), right_header_line(@right_name, @right_timestamp) ] end |
#render_hunk(a, b, opcodes, last_line_shown) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/patience_diff/formatter.rb', line 42 def render_hunk(a, b, opcodes, last_line_shown) lines = [render_hunk_marker(opcodes)] lines << opcodes.collect do |(code, a_start, a_end, b_start, b_end)| case code when :equal b[b_start..b_end].map { |line| ' ' + line } when :delete a[a_start..a_end].map { |line| '-' + line } when :insert b[b_start..b_end].map { |line| '+' + line } end end lines end |
#render_hunk_marker(opcodes) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/patience_diff/formatter.rb', line 33 def render_hunk_marker(opcodes) a_start = opcodes.first[1] + 1 a_end = opcodes.last[2] + 2 b_start = opcodes.first[3] + 1 b_end = opcodes.last[4] + 2 "@@ -%d,%d +%d,%d @@" % [a_start, a_end-a_start, b_start, b_end-b_start] end |