Class: BorderWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/delve/widgets/border.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height, heading = nil, fg = :white, bg = :black) ⇒ BorderWidget

Returns a new instance of BorderWidget.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/delve/widgets/border.rb', line 2

def initialize(x, y, width, height, heading=nil, fg=:white, bg=:black)
  raise 'Cannot initialize border widget when x is nil' unless x
  raise 'Cannot initialize border widget when y is nil' unless y
  raise 'Cannot initailize border widget when width is nil' unless width
  raise 'Cannot initialize border widget when height is nil' unless height
  raise 'Cannot initialize border widget when heading is wider than width + 4' if heading and heading.length > width + 4

  @x = x
  @y = y
  @width = width
  @height = height
  @heading = heading
  @fg = fg
  @bg = bg
end

Instance Method Details

#draw(display) ⇒ Object



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/delve/widgets/border.rb', line 18

def draw(display)
  raise 'Cannot draw text when display is nil' unless display

  header_text = " #{@heading} "
  if header_text.length.odd?
    header_text = "#{header_text} "
  end
  center_point = ((@x + @width) / 2).floor
  half_text = (header_text.length / 2).ceil
  header_start = center_point - half_text
  header_end = center_point + half_text
  
  (@x..(@x+@width-1)).each do |x|
    char = (x == @x || x == (@x+@width-1)) ? '+' : '-'
    if @heading and x > header_start and x <= header_end
      char = header_text[x-header_start-1]
    end
    display.draw x, @y, char, @fg, @bg
    display.draw x, (@y+@height-1), (x == @x || x == (@x+@width-1)) ? '+' : '-', @fg, @bg
  end

  ((@y+1)..(@y+@height-2)).each do |y|
    display.draw @x, y, '|', @fg, @bg
    display.draw (@x+@width-1), y, '|', @fg, @bg
  end
end