Sha256: 98179ce7baccba201c4056739ed788fa3d72e17f6371b2f4bf3e22c7485d64de

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

require 'whirled_peas/utils/formatted_string'
require 'whirled_peas/utils/title_font'

require_relative 'painter'
require_relative 'text_dimensions'

module WhirledPeas
  module Graphics
    class TextPainter < Painter
      attr_reader :content

      def paint(canvas, left, top, &block)
        return unless canvas.writable?
        formatting = [*settings.color, *settings.bg_color]
        formatting << Utils::Ansi::BOLD if settings.bold?
        if settings.underline? && settings.title_font.nil?
          formatting << Utils::Ansi::UNDERLINE
        end
        content.each.with_index do |line, index|
          canvas.stroke(left, top + index, line, formatting, &block)
        end
      end

      def dimensions
        TextDimensions.new(content)
      end

      def content=(content)
        @content = if settings.title_font
          Utils::TitleFont.to_s(
            content, settings.title_font
          ).split("\n")
        else
          content.split("\n")
        end
      end
    end
    private_constant :TextPainter
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
whirled_peas-0.8.0 lib/whirled_peas/graphics/text_painter.rb
whirled_peas-0.7.1 lib/whirled_peas/graphics/text_painter.rb
whirled_peas-0.7.0 lib/whirled_peas/graphics/text_painter.rb