Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.77 KB | None | 0 0
  1. module Diff
  2.   class Printer
  3.  
  4.     TAGS = {eql: " ", del: "-", ins: "+"}
  5.  
  6.     COLORS = {
  7.       del:     "\e[31m",
  8.       ins:     "\e[32m",
  9.       default: "\e[39m"
  10.     }
  11.  
  12.     LINE_WIDTH = 4
  13.  
  14.     def initialize(output: $stdout)
  15.       @output = output
  16.       @colors = output.isatty ? COLORS : {}
  17.     end
  18.  
  19.     def print(diff)
  20.       diff.each { |edit| print_edit(edit) }
  21.     end
  22.  
  23.     def print_edit(edit)
  24.       col   = @colors.fetch(edit.type, "")
  25.       reset = @colors.fetch(:default, "")
  26.       tag   = TAGS[edit.type]
  27.  
  28.       old_line = edit.old_number.rjust(LINE_WIDTH, " ")
  29.       new_line = edit.new_number.rjust(LINE_WIDTH, " ")
  30.       text     = edit.text.rstrip
  31.  
  32.       @output.puts "#{col}#{tag} #{old_line} #{new_line}    #{text}#{reset}"
  33.     end
  34.  
  35.   end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement