2014-10-26 18:32:46 +01:00
|
|
|
# Formatter for diff output
|
|
|
|
module Differ
|
|
|
|
module Format
|
|
|
|
# Try to format as unified diff, using - and +
|
|
|
|
module Patch
|
|
|
|
class << self
|
|
|
|
def format(change)
|
2016-05-21 12:02:19 +02:00
|
|
|
if change.change?
|
2014-10-26 18:32:46 +01:00
|
|
|
as_change change
|
2016-05-21 12:02:19 +02:00
|
|
|
elsif change.delete?
|
2014-10-26 18:32:46 +01:00
|
|
|
as_delete change
|
2016-05-21 12:02:19 +02:00
|
|
|
elsif change.insert?
|
2014-10-26 18:32:46 +01:00
|
|
|
as_insert change
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def as_insert(change)
|
2017-11-02 21:41:18 +01:00
|
|
|
if change.insert.start_with? ' '
|
|
|
|
change.insert[0] = '+'
|
|
|
|
change.insert
|
|
|
|
else
|
|
|
|
'+ ' + change.insert
|
|
|
|
end
|
2014-10-26 18:32:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_delete(change)
|
2017-11-02 21:41:18 +01:00
|
|
|
if change.delete.start_with? ' '
|
|
|
|
change.delete[0] = '-'
|
|
|
|
change.delete
|
|
|
|
else
|
|
|
|
'- ' + change.delete
|
|
|
|
end
|
2014-10-26 18:32:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_change(change)
|
2014-10-27 11:17:17 +01:00
|
|
|
[as_delete(change), as_insert(change)].join "\n"
|
2014-10-26 18:32:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|