Sha256: 07360cbe19a3ae5ac12d72a4ee07a7e7623c268f28e8302e096b0763962b9d25

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

require 'cucumber/term/ansicolor'

module Cucumber
  module Term
    module Banner
      def display_banner(lines, io, border_modifiers = nil)
        BannerMaker.new.display_banner(lines, io, border_modifiers || %i[green bold])
      end

      class BannerMaker
        include Term::ANSIColor

        def display_banner(lines, io, border_modifiers)
          lines = lines.split("\n") if lines.is_a? String
          longest_line_length = lines.map { |line| line_length(line) }.max

          io.puts apply_modifiers("┌#{'─' * (longest_line_length + 2)}┐", border_modifiers)
          lines.map do |line|
            padding = ' ' * (longest_line_length - line_length(line))
            io.puts "#{apply_modifiers('│', border_modifiers)} #{display_line(line)}#{padding} #{apply_modifiers('│', border_modifiers)}"
          end
          io.puts apply_modifiers("└#{'─' * (longest_line_length + 2)}┘", border_modifiers)
        end

        private

        def display_line(line)
          line.is_a?(Array) ? line.map { |span| display_span(span) }.join : line
        end

        def display_span(span)
          return apply_modifiers(span.shift, span) if span.is_a?(Array)

          span
        end

        def apply_modifiers(str, modifiers)
          display = str
          modifiers.each { |modifier| display = send(modifier, display) }
          display
        end

        def line_length(line)
          if line.is_a?(Array)
            line.map { |span| span_length(span) }.sum
          else
            line.length
          end
        end

        def span_length(span)
          span.is_a?(Array) ? span[0].length : span.length
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
cucumber-9.0.1 lib/cucumber/term/banner.rb
cucumber-9.0.0 lib/cucumber/term/banner.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/term/banner.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/term/banner.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/term/banner.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/cucumber-8.0.0/lib/cucumber/term/banner.rb
cucumber-8.0.0 lib/cucumber/term/banner.rb
cucumber-8.0.0.rc.1 lib/cucumber/term/banner.rb