Sha256: 02193876d63921097813c71b03b6e59becd8386d8696c54a999b368da3d0da9f

Contents?: true

Size: 1 KB

Versions: 33

Compression:

Stored size: 1 KB

Contents

module Gemdiff
  module Colorize
    COLORS =
      {
        red:     31,
        green:   32,
        yellow:  33,
        blue:    34,
        magenta: 35,
      }

    # works with `git show` and `git diff`
    def colorize_git_output(lines)
      out = []
      lines.split("\n").each do |line|
        out <<
          if line.start_with?("---") || line.start_with?("+++") || line.start_with?("diff") || line.start_with?("index")
            colorize line, :blue
          elsif line.start_with?("@@")
            colorize line, :magenta
          elsif line.start_with?("commit")
            colorize line, :yellow
          elsif line.start_with?("-")
            colorize line, :red
          elsif line.start_with?("+")
            colorize line, :green
          else
            line
          end
      end
      out.join("\n")
    end

    def colorize(string, color)
      "\e[#{to_color_code(color)}m#{string}\e[0m"
    end

  private

    def to_color_code(color)
      COLORS[color] || 30
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
gemdiff-0.6.6 lib/gemdiff/colorize.rb
gemdiff-0.6.5 lib/gemdiff/colorize.rb
gemdiff-0.6.4 lib/gemdiff/colorize.rb
gemdiff-0.6.3 lib/gemdiff/colorize.rb
gemdiff-0.6.2 lib/gemdiff/colorize.rb
gemdiff-0.6.1 lib/gemdiff/colorize.rb
gemdiff-0.6.0 lib/gemdiff/colorize.rb
gemdiff-0.5.2 lib/gemdiff/colorize.rb
gemdiff-0.5.1 lib/gemdiff/colorize.rb
gemdiff-0.5.0 lib/gemdiff/colorize.rb
gemdiff-0.4.2 lib/gemdiff/colorize.rb
gemdiff-0.4.1 lib/gemdiff/colorize.rb
gemdiff-0.4.0 lib/gemdiff/colorize.rb