Sha256: 06c6d7c429a6054484b3aed50eed9b13240859591f242c82bd8ab81a860cc2d7

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module Cowsay
  module Character

    class Base
      MAX_LINE_LENGTH = 36

      def self.say(message)
        new.say(message)
      end

      def initialize
        @thoughts = '\\'
      end

      def say(message)
        render_balloon(message) + render_character
      end

      def template
        raise '#template should be subclassed'
      end

      private

      def render_character
        template
      end

      def render_balloon(message)
        message_lines = format_message(message)
        line_length = message_lines.max{ |a,b| a.length <=> b.length }.length

        output_lines = []

        output_lines << " #{'_' * (line_length + 2)} "

        message_lines.each do |line|
          # 'Here is your message: %s' % 'hello world'
          # is the same as
          # printf('Here is your message: %s', 'hello world')
          output_lines << "| %-#{line_length}s |" % line
        end

        output_lines << " #{'-' * (line_length + 2)} "
        output_lines << ''

        output_lines.join("\n")
      end

      def format_message(message)
        return [message] if message.length <= MAX_LINE_LENGTH

        lines = []
        words = message.split(/\s/).reject{ |word| word.length.zero? }
        new_line = ''

        words.each do |word|
          new_line << "#{word} "

          if new_line.length > MAX_LINE_LENGTH
            lines << new_line.chomp
            new_line = ''
          end
        end

        lines << new_line.chomp unless new_line.length.zero?

        lines
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cowsay-0.0.4 lib/cowsay/character/base.rb
cowsay-0.0.3 lib/cowsay/character/base.rb