Class: RubyJard::RowRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/row_renderer.rb

Overview

Generate bitmap lines from a row’s data

Constant Summary collapse

ELLIPSIS =
' »'

Instance Method Summary collapse

Constructor Details

#initialize(row:, width:, height:, color_scheme:) ⇒ RowRenderer

Returns a new instance of RowRenderer.



9
10
11
12
13
14
# File 'lib/ruby_jard/row_renderer.rb', line 9

def initialize(row:, width:, height:, color_scheme:)
  @row = row
  @width = width
  @height = height
  @color_decorator = RubyJard::Decorators::ColorDecorator.new(color_scheme)
end

Instance Method Details

#draw_content(drawing_content, styles) ⇒ Object

rubocop:enable Metrics/MethodLength



86
87
88
89
90
91
92
# File 'lib/ruby_jard/row_renderer.rb', line 86

def draw_content(drawing_content, styles)
  return if @y < 0 || @y >= @height

  @content_map[@y] ||= []
  @content_map[@y][@x] = [styles, drawing_content]
  @x += drawing_content.length
end

#generate_bitmapObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_jard/row_renderer.rb', line 94

def generate_bitmap
  @row.content = []
  @content_map.each do |line|
    line_content = ''
    pending_content = ''

    cell_index = 0
    while cell_index < @width
      cell = line[cell_index]
      if cell.nil? || cell[1].empty?
        pending_content += ' '
        cell_index += 1
      else
        line_content += @color_decorator.decorate(:background, pending_content)
        line_content += @color_decorator.decorate(cell[0], cell[1])
        pending_content = ''
        cell_index += cell[1].length
      end
    end

    line_content += @color_decorator.decorate(:background, pending_content) unless pending_content.empty?
    @row.content << line_content
  end
end

#renderObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_jard/row_renderer.rb', line 16

def render
  @row.reset_rendered

  @x = 0
  @y = 0
  @original_x = 0
  @original_y = 0
  @content_map = []

  @row.columns.each do |column|
    @x = @original_x
    @y = @original_y

    @drawing_width = 0
    @drawing_lines = 1

    column.spans.each do |span|
      to_continue = render_span(column, span)
      break unless to_continue
    end

    @original_x += column.width
  end

  generate_bitmap

  @row.mark_rendered
end

#render_span(column, span) ⇒ Object

rubocop:disable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_jard/row_renderer.rb', line 46

def render_span(column, span)
  line_content = span.content

  until line_content.nil? || line_content.empty?
    if column.word_wrap == RubyJard::Column::WORD_WRAP_NORMAL
      if column.content_width - @drawing_width < line_content.length && @drawing_width != 0
        @drawing_width = 0
        @drawing_lines += 1
        @y += 1
        @x = @original_x
      end
    elsif column.word_wrap == RubyJard::Column::WORD_WRAP_BREAK_WORD
      if column.content_width - @drawing_width <= 0
        @drawing_width = 0
        @drawing_lines += 1
        @y += 1
        @x = @original_x
      end
    elsif column.content_width - @drawing_width <= 0
      return false
    end
    drawing_content = line_content[0..column.content_width - @drawing_width - 1]
    line_content = line_content[column.content_width - @drawing_width..-1]
    @drawing_width += drawing_content.length

    if !@row.line_limit.nil? &&
       @drawing_lines >= @row.line_limit &&
       !line_content.nil? &&
       !line_content.empty?
      drawing_content[drawing_content.length - ELLIPSIS.length..-1] = ELLIPSIS
      draw_content(drawing_content, span.styles)
      return false
    else
      draw_content(drawing_content, span.styles)
    end
  end
  true
end