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.65.0 lib/pact/provider/help/console_text.rb
pact-1.64.0 lib/pact/provider/help/console_text.rb
pact-1.63.0 lib/pact/provider/help/console_text.rb
pact-1.62.0 lib/pact/provider/help/console_text.rb
pact-1.61.0 lib/pact/provider/help/console_text.rb
pact-1.60.0 lib/pact/provider/help/console_text.rb
pact-1.59.0 lib/pact/provider/help/console_text.rb
pact-1.58.0 lib/pact/provider/help/console_text.rb
pact-1.57.0 lib/pact/provider/help/console_text.rb
pact-1.56.0 lib/pact/provider/help/console_text.rb
pact-1.55.7 lib/pact/provider/help/console_text.rb
pact-1.55.6 lib/pact/provider/help/console_text.rb
pact-1.55.5 lib/pact/provider/help/console_text.rb
pact-1.55.4 lib/pact/provider/help/console_text.rb
pact-1.55.3 lib/pact/provider/help/console_text.rb
pact-1.55.2 lib/pact/provider/help/console_text.rb
pact-1.55.1 lib/pact/provider/help/console_text.rb
pact-1.55.0 lib/pact/provider/help/console_text.rb
pact-1.54.0 lib/pact/provider/help/console_text.rb
pact-1.53.0 lib/pact/provider/help/console_text.rb