Class: DiffColorize::Result
- Inherits:
-
Object
- Object
- DiffColorize::Result
- Defined in:
- lib/diff_colorize/result.rb
Instance Attribute Summary collapse
-
#color1 ⇒ Object
Returns the value of attribute color1.
-
#color2 ⇒ Object
Returns the value of attribute color2.
-
#t1 ⇒ Object
readonly
Returns the value of attribute t1.
-
#t2 ⇒ Object
readonly
Returns the value of attribute t2.
Instance Method Summary collapse
- #color_t1 ⇒ Object
- #color_t2 ⇒ Object
- #diff ⇒ Object
-
#initialize(t1, t2) ⇒ Result
constructor
A new instance of Result.
- #superimpose ⇒ Object
- #t1_diff ⇒ Object
- #t2_diff ⇒ Object
Constructor Details
#initialize(t1, t2) ⇒ Result
Returns a new instance of Result.
7 8 9 10 11 |
# File 'lib/diff_colorize/result.rb', line 7 def initialize(t1, t2) @t1 = t1 @t2 = t2 @color1, @color2 = "\e[31m", "\e[32m" end |
Instance Attribute Details
#color1 ⇒ Object
Returns the value of attribute color1.
5 6 7 |
# File 'lib/diff_colorize/result.rb', line 5 def color1 @color1 end |
#color2 ⇒ Object
Returns the value of attribute color2.
5 6 7 |
# File 'lib/diff_colorize/result.rb', line 5 def color2 @color2 end |
#t1 ⇒ Object (readonly)
Returns the value of attribute t1.
4 5 6 |
# File 'lib/diff_colorize/result.rb', line 4 def t1 @t1 end |
#t2 ⇒ Object (readonly)
Returns the value of attribute t2.
4 5 6 |
# File 'lib/diff_colorize/result.rb', line 4 def t2 @t2 end |
Instance Method Details
#color_t1 ⇒ Object
25 26 27 |
# File 'lib/diff_colorize/result.rb', line 25 def color_t1 colorize_diff_text(t1, t1_diff) end |
#color_t2 ⇒ Object
29 30 31 |
# File 'lib/diff_colorize/result.rb', line 29 def color_t2 colorize_diff_text(t2, t2_diff) end |
#diff ⇒ Object
13 14 15 |
# File 'lib/diff_colorize/result.rb', line 13 def diff t1.diff(t2).flatten end |
#superimpose ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/diff_colorize/result.rb', line 33 def superimpose cursor = { t1: 0, t2: 0 } text = diff.inject('') do |c_text, d| case d.action when '-' s = t1[cursor[:t1]..d.position-1] c_s = colorized_string(d) cursor[:t1] += d.element.length when '+' s = t2[cursor[:t2]..d.position-1] c_s = colorized_string(d) cursor[:t2] += d.element.length end cursor[:t1] += s.length cursor[:t2] += s.length c_text << s + c_s end text << t1[cursor[:t1]..-1] end |
#t1_diff ⇒ Object
17 18 19 |
# File 'lib/diff_colorize/result.rb', line 17 def t1_diff diff.select { |d| d.action == '-' } end |
#t2_diff ⇒ Object
21 22 23 |
# File 'lib/diff_colorize/result.rb', line 21 def t2_diff diff.select { |d| d.action == '+' } end |