Sha256: f93611916385db705d149b56b30f10774d03b72cdcc6056f4527bc2170b65a3f

Contents?: true

Size: 1.16 KB

Versions: 89

Compression:

Stored size: 1.16 KB

Contents

module LCSDiff
protected

  # Overwrite show_diff to show diff with colors if Diff::LCS is
  # available.
  def show_diff(destination, content) #:nodoc:
    if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil?
      actual  = File.binread(destination).to_s.split("\n")
      content = content.to_s.split("\n")

      Diff::LCS.sdiff(actual, content).each do |diff|
        output_diff_line(diff)
      end
    else
      super
    end
  end

private

  def output_diff_line(diff) #:nodoc:
    case diff.action
    when "-"
      say "- #{diff.old_element.chomp}", :red, true
    when "+"
      say "+ #{diff.new_element.chomp}", :green, true
    when "!"
      say "- #{diff.old_element.chomp}", :red, true
      say "+ #{diff.new_element.chomp}", :green, true
    else
      say "  #{diff.old_element.chomp}", nil, true
    end
  end

  # Check if Diff::LCS is loaded. If it is, use it to create pretty output
  # for diff.
  def diff_lcs_loaded? #:nodoc:
    return true if defined?(Diff::LCS)
    return @diff_lcs_loaded unless @diff_lcs_loaded.nil?

    @diff_lcs_loaded = begin
      require "diff/lcs"
      true
    rescue LoadError
      false
    end
  end

end

Version data entries

89 entries across 89 versions & 9 rubygems

Version Path
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/shell/lcs_diff.rb