Class: TTY::File::CompareFiles
- Inherits:
-
Object
- Object
- TTY::File::CompareFiles
- Extended by:
- Forwardable
- Defined in:
- lib/tty/file/compare_files.rb
Instance Method Summary collapse
-
#call(file_a, file_b, file_a_path, file_b_path) ⇒ Object
Compare files.
-
#initialize(format: :unified, header: true, context_lines: 5, verbose: true, color: :green, noop: false, diff_colors: nil) ⇒ CompareFiles
constructor
A new instance of CompareFiles.
Constructor Details
#initialize(format: :unified, header: true, context_lines: 5, verbose: true, color: :green, noop: false, diff_colors: nil) ⇒ CompareFiles
Returns a new instance of CompareFiles.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tty/file/compare_files.rb', line 10 def initialize(format: :unified, header: true, context_lines: 5, verbose: true, color: :green, noop: false, diff_colors: nil) @format = format @header = header @context_lines = context_lines @verbose = verbose @color = color @noop = noop @diff_colors = diff_colors end |
Instance Method Details
#call(file_a, file_b, file_a_path, file_b_path) ⇒ Object
Compare files
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tty/file/compare_files.rb', line 24 def call(file_a, file_b, file_a_path, file_b_path) differ = Differ.new(format: @format, context_lines: @context_lines) block_size = file_a.lstat.blksize file_a_chunk = file_a.read(block_size) file_b_chunk = file_b.read(block_size) hunks = differ.(file_a_chunk, file_b_chunk) return "" if file_a_chunk.empty? && file_b_chunk.empty? return "No differences found\n" if hunks.empty? output = [] if %i[unified context old].include?(@format) && @header output << "#{differ.delete_char * 3} #{file_a_path}\n" output << "#{differ.add_char * 3} #{file_b_path}" end output << "\n" unless hunks =~ /\A\n+@@/ output << hunks while !file_a.eof? && !file_b.eof? output << differ.(file_a.read(block_size), file_b.read(block_size)) end color_diff_lines(output.join) end |