Skip to content

Commit 9ffde84

Browse files
author
Kevin Paulisse
committed
Add Special case: addition only, no truncation
1 parent aaae4d0 commit 9ffde84

File tree

1 file changed

+30
-1
lines changed
  • lib/octocatalog-diff/catalog-diff/display

1 file changed

+30
-1
lines changed

lib/octocatalog-diff/catalog-diff/display/text.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,17 @@ def self.make_trailing_whitespace_visible(string_in)
327327
# @param depth [Fixnum] Depth, for correct indentation
328328
# @param limit [Fixnum] Maximum string length
329329
# @param strip_diff [Boolean] Strip leading +/-/" "
330-
# @return Array<String> Displayable result
330+
# @return [Array<String>] Displayable result
331331
def self.diff_two_hashes_with_diffy(opts = {})
332332
depth = opts.fetch(:depth, 0)
333333
hash1 = opts.fetch(:hash1, {})
334334
hash2 = opts.fetch(:hash2, {})
335335
limit = opts[:limit]
336336
strip_diff = opts.fetch(:strip_diff, false)
337337

338+
# Special case: addition only, no truncation
339+
return addition_only_no_truncation(depth, hash2, strip_diff) if hash1 == {} && limit.nil?
340+
338341
json_old = stringify_for_diffy(hash1)
339342
json_new = stringify_for_diffy(hash2)
340343

@@ -354,6 +357,32 @@ def self.diff_two_hashes_with_diffy(opts = {})
354357
end
355358
end
356359

360+
# Special case: addition only, no truncation
361+
# @param depth [Fixnum] Depth, for correct indentation
362+
# @param hash [Hash] Added object
363+
# @param strip_diff [Boolean] Strip leading +/-/" "
364+
# @return [Array<String>] Displayable result
365+
def self.addition_only_no_truncation(depth, hash, strip_diff)
366+
plus = strip_diff ? '' : '+ '
367+
result = []
368+
369+
# Single line strings
370+
hash.keys.sort.map do |key|
371+
next if hash[key] =~ /\n/
372+
result << left_pad(2 * depth + 2, [plus, key.inspect, ': ', hash[key].inspect].join('')).green
373+
end
374+
375+
# Multi-line strings
376+
hash.keys.sort.map do |key|
377+
next if hash[key] !~ /\n/
378+
result << left_pad(2 * depth + 2, [plus, key.inspect, ': >>>'].join('')).green
379+
result.concat hash[key].split(/\n/).map(&:green)
380+
result << '<<<'
381+
end
382+
383+
result
384+
end
385+
357386
# Limit length of a string
358387
# @param str [String] String
359388
# @param limit [Fixnum] Limit (0=unlimited)

0 commit comments

Comments
 (0)