Sha256: 412d5f1040f633e74ff65b84c80f664f21203261bcb624fad8a19afc65b9e803

Contents?: true

Size: 1.97 KB

Versions: 98

Compression:

Stored size: 1.97 KB

Contents

require 'pact/provider/help/content'
require 'fileutils'
require 'pact/consumer/configuration'
require 'pact/provider/help/write'
require 'term/ansicolor'

module Pact
  module Provider
    module Help
      class ConsoleText

        C = ::Term::ANSIColor

        def self.call reports_dir = Pact.configuration.reports_dir, options = {color: true}
          new(reports_dir || Pact.configuration.reports_dir, options).call
        end

        def initialize reports_dir, options
          @reports_dir = File.expand_path(reports_dir)
          @options = options
        end

        def call
          begin
            options[:color] ? ColorizeMarkdown.(help_text) : help_text
          rescue Errno::ENOENT
            options[:color] ? error_text_coloured : error_text_plain
          end
        end

        private

        attr_reader :reports_dir, :options

        def help_text
          File.read(help_file_path)
        end

        def help_file_path
          File.join(reports_dir, Write::HELP_FILE_NAME)
        end

        def error_text_plain
          "Sorry, could not find help file at #{help_file_path}. Please ensure you have run `rake pact:verify`.\n" +
           "If this does not fix the problem, please raise a github issues for this bug."
        end

        def error_text_coloured
          C.red(error_text_plain)
        end

        class ColorizeMarkdown

          C = ::Term::ANSIColor

          def self.call markdown
            markdown.split("\n").collect do | line |
              if line.start_with?("# ")
                yellow_underling line.gsub(/^# /, '')
              elsif line.start_with?("* ")
                green("* ") + line.gsub(/^\* /, '')
              else
                line
              end
            end.join("\n")
          end

          def self.yellow_underling string
            C.underline(C.yellow(string))
          end

          def self.green string
            C.green(string)
          end

        end
      end
    end
  end
end

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
pact-1.42.1 lib/pact/provider/help/console_text.rb
pact-1.42.0 lib/pact/provider/help/console_text.rb
pact-1.41.2 lib/pact/provider/help/console_text.rb
pact-1.41.1 lib/pact/provider/help/console_text.rb
pact-1.41.0 lib/pact/provider/help/console_text.rb
pact-1.40.0 lib/pact/provider/help/console_text.rb
pact-1.39.0 lib/pact/provider/help/console_text.rb
pact-1.38.0 lib/pact/provider/help/console_text.rb
pact-1.37.0 lib/pact/provider/help/console_text.rb
pact-1.36.2 lib/pact/provider/help/console_text.rb
pact-1.36.0 lib/pact/provider/help/console_text.rb
pact-1.34.0 lib/pact/provider/help/console_text.rb
pact-1.33.2 lib/pact/provider/help/console_text.rb
pact-1.33.1 lib/pact/provider/help/console_text.rb
pact-1.33.0 lib/pact/provider/help/console_text.rb
pact-1.32.0 lib/pact/provider/help/console_text.rb
pact-1.31.0 lib/pact/provider/help/console_text.rb
pact-1.30.0 lib/pact/provider/help/console_text.rb
pact-1.29.0 lib/pact/provider/help/console_text.rb
pact-1.28.0 lib/pact/provider/help/console_text.rb