Sha256: 5d3a2772da30f65a8872a0de80dc6c0d20889abb0a8cc4d9629be85eef4a9652

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

require 'term/ansicolor'
require "uri"
module PactBroker
  module Client
    class ColorizeNotices
      def self.call(notices)
        notices.collect do | notice |
          colorized_message(notice)
        end
      end

      def self.colorized_message(notice)
        color = color_for_type(notice.type)
        uri_strings = ::URI.extract(notice.text, %w(http https))
        if color && uri_strings.any?
          color_for_url(::Term::ANSIColor.color(color, notice.text || ''), uri_strings)
        elsif color
          ::Term::ANSIColor.color(color, notice.text || '')
        elsif uri_strings.any?
          color_for_url(notice.text, uri_strings)
        else
          notice.text
        end
      end

      def self.color_for_url(text, uris)
        uris.inject(text) do | new_text, uri  |
          new_text.gsub(uri, ::Term::ANSIColor.magenta(uri))
        end
      end

      def self.color_for_type(type)
        case type
        when "warning", "prompt" then :yellow
        when "error", "danger" then :red
        when "success" then :green
        else nil
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pact_broker-client-1.77.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.76.2 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.76.1 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.76.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.75.4 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.75.3 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.75.1 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.75.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.74.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.73.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.72.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.71.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.70.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.69.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.68.0 lib/pact_broker/client/colorize_notices.rb
pact_broker-client-1.67.0 lib/pact_broker/client/colorize_notices.rb