Module: Bash_Visual::Painter

Included in:
Console
Defined in:
lib/bash-visual/painter.rb

Constant Summary collapse

BORDER_UTF =
["\u250C", "\u2500", "\u2510",
"\u2502", "\u2502",
"\u2514", "\u2500", "\u2518"]
BORDER_UTF_ROUND =
["\u256D", "\u2500", "\u256E",
"\u2502", "\u2502",
"\u2570", "\u2500", "\u256F"]
BORDER_UTF_DOUBLE =
["\u2554", "\u2550", "\u2557",
"\u2551", "\u2551",
"\u255A", "\u2550", "\u255D"]
@@default_window_wrap =
["\u257C ", " \u257E"]

Instance Method Summary collapse

Instance Method Details

#draw_border(*positioning, &extra) ⇒ Object

Parameters:

  • positioning (Array)


45
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
# File 'lib/bash-visual/painter.rb', line 45

def draw_border(*positioning, &extra)
  x, y, width, height = prepare_positioning positioning do |x2, y2, width2, height2|
    next x2, y2, width2 + 2, height2 + 2
  end

  raise 'width,height must be great than 2' if (width < 2 or height < 2)

  params = block_given? ? yield : {}
  border = params[:border] ? params[:border] : BORDER_UTF
  font   = params[:font] ? params[:font] : @font

  bash = @builder.save_position
  bash << @builder.set_position(x, y)
  body_width = width - 2

  bash << @builder.write(border[0] + border[1] * body_width + border[2])

  row = @builder.move_position(-width, 1)
  row << @builder.write(border[3] + ' ' * body_width + border[4])
  bash << row*(height - 2)

  bash << @builder.move_position(-width, 1)
  bash << @builder.write(border[5] + border[6] * body_width + border[7])

  bash << @builder.restore_position
  print @builder.write(bash, font)
end

#draw_filled_rectangle(*positioning, color) ⇒ Object

Parameters:

  • positioning (Array)
  • color (Integer)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bash-visual/painter.rb', line 26

def draw_filled_rectangle(*positioning, color)
  x, y, width, height = prepare_positioning positioning
  raise 'width,height must be great than 1' if (width < 1 or height < 1)

  color = color.background if color.is_a? Font
  font = Font.new :std, :white, color

  bash = @builder.save_position
  bash << @builder.set_position(x+1, y+1)

  row = @builder.write(' ' * width)
  row << @builder.move_position(-width, 1)
  bash << row*height

  bash << @builder.restore_position
  print @builder.write(bash, font)
end

#draw_window(*positioning, text, &params) ⇒ Object

Parameters:

  • positioning (Array)
  • text (String)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bash-visual/painter.rb', line 76

def draw_window(*positioning, text, &params)

  x, y, width, height = prepare_positioning positioning do |x2, y2, width2, height2|
    next x2, y2, width2 + 2, height2 + 2
  end

  raise 'width,height must be great than 2' if (width < 3 or height < 3)

  params = block_given? ? yield : {}


  wrap   = params[:wrap].nil? ? @@default_window_wrap : params[:wrap]
  border = params[:border] ? params[:border] : BORDER_UTF
  font   = params[:font] ? params[:font] : @font

  body_width = width - 2
  text = if wrap
           wrap_size  = wrap[0].size + wrap[1].size
           text_width = body_width - wrap_size
           if text_width < 0
             text.to_s.slice(0, body_width)
           else
             wrap[0] + text.to_s[0, text_width] + wrap[1]
           end
         else
           text.to_s.slice(0, body_width)
         end

  text = text.center(body_width, border[1])

  bash = @builder.save_position
  bash << @builder.set_position(x, y)

  bash << @builder.write(border[0] + text + border[2])

  row = @builder.move_position(-width, 1)
  row << @builder.write(border[3] + ' ' * body_width + border[4])
  bash << row*(height - 2)

  bash << @builder.move_position(-width, 1)
  bash << @builder.write(border[5] + border[6] * body_width + border[7])

  bash << @builder.restore_position
  print @builder.write(bash, font)
end