Sha256: c114d332cd28a58c41f7e6de0014b48aae7db432aea1b2979db62cc6d3b1ba9b

Contents?: true

Size: 1.29 KB

Versions: 26

Compression:

Stored size: 1.29 KB

Contents

module XCPretty
  module ANSI

    attr_accessor :colorize

    FORMATTED_MATCHER = %r{\e\[(\d+)[;]?(\d+)?m(.*)\e\[0m}

    EFFECT = {
      reset: '0',
      bold: '1',
      underline: '4'
    }

    COLORS = {
      black: '30',
      red: '31',
      green: '32',
      yellow: '33',
      blue: '34',
      cyan: '36',
      white: '37',
      plain: '39'
    }

    def colorize?
      !!@colorize
    end

    def white(text)
      ansi_parse(text, :plain, :bold)
    end

    def red(text)
      ansi_parse(text, :red)
    end

    def green(text)
      ansi_parse(text, :green, :bold)
    end

    def cyan(text)
      ansi_parse(text, :cyan)
    end

    def yellow(text)
      ansi_parse(text, :yellow)
    end

    def applied_effects(text)
      effects = []
      if text =~ FORMATTED_MATCHER
        colors = COLORS.invert[$1]
        effect = EFFECT.invert[$2]
        effects << colors if colors
        effects << effect if effect
      end
      effects
    end

    def strip(text)
      text =~ FORMATTED_MATCHER ? $3 : text
    end

    def ansi_parse(text, color, effect=nil)
      return text unless colorize?
      colors_code = COLORS[color] || ''
      effect_code = EFFECT[effect] ? ';' + EFFECT[effect] : ''
      "\e[#{colors_code}#{effect_code}m#{text}\e[#{EFFECT[:reset]}m"
    end
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
xcpretty-0.4.0 lib/xcpretty/ansi.rb
xcpretty-security-patched-0.3.2 lib/xcpretty/ansi.rb
xcpretty-security-patched-0.3.0 lib/xcpretty/ansi.rb
xcpretty-0.3.0 lib/xcpretty/ansi.rb
xcpretty-0.2.8 lib/xcpretty/ansi.rb
xcpretty-0.2.7 lib/xcpretty/ansi.rb
xcpretty-0.2.6 lib/xcpretty/ansi.rb
xcpretty-0.2.4 lib/xcpretty/ansi.rb
xcpretty-0.2.3 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb14 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb13 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb12 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb11 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb10 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb9 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb8 lib/xcpretty/ansi.rb
xcpretty-0.2.2 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb7 lib/xcpretty/ansi.rb
xcpretty-0.2.1 lib/xcpretty/ansi.rb
xcpretty-bb-0.1.12.bb6 lib/xcpretty/ansi.rb