Class: Bash_Visual::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/bash-visual/console.rb

Constant Summary collapse

OUTPUT_STRING =
0
OUTPUT_WITHOUT_BLOCK =
1
OUTPUT_WITH_BLOCK =
2
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"]
@@mutex =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font = Font.new, way_outnput = OUTPUT_WITHOUT_BLOCK, builder = Builder.new) ⇒ Console

Returns a new instance of Console.



23
24
25
26
27
28
29
# File 'lib/bash-visual/console.rb', line 23

def initialize (font = Font.new, way_outnput = OUTPUT_WITHOUT_BLOCK, builder = Builder.new)
  @current_x = 0
  @current_y = 0
  @font = font
  @way_output = way_outnput
  @builder = builder
end

Instance Attribute Details

#current_xObject (readonly)

Returns the value of attribute current_x.



21
22
23
# File 'lib/bash-visual/console.rb', line 21

def current_x
  @current_x
end

#current_yObject (readonly)

Returns the value of attribute current_y.



21
22
23
# File 'lib/bash-visual/console.rb', line 21

def current_y
  @current_y
end

#fontObject

Returns the value of attribute font.



21
22
23
# File 'lib/bash-visual/console.rb', line 21

def font
  @font
end

Instance Method Details

#clearObject

Clear the screen and move to (0,0)



117
118
119
120
121
# File 'lib/bash-visual/console.rb', line 117

def clear
  @current_x = 0
  @current_y = 0
  print @builder.clear
end

#draw_border(x, y, width, height, font = nil, border = BORDER_UTF) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bash-visual/console.rb', line 73

def draw_border(x, y, width, height, font = nil, border = BORDER_UTF)
  raise 'width,height must be great than 1' if (width < 2 or height < 2)

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

  bash <<  @builder.write(border[0] + border[1] * (width - 2) +  border[2], font)
  (height - 2).times do
    bash << @builder.move_position(-width, 1)
    bash << @builder.write(border[3] + ' ' * (width - 2) + border[4], font)
  end
  bash << @builder.move_position(-width, 1)
  bash << @builder.write(border[5] + border[6] * (width - 2) + border[7], font)

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

#draw_rectangle(x, y, width, height, font = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bash-visual/console.rb', line 61

def draw_rectangle(x, y, width, height, font = nil)
  bash = ''
  bash << @builder.save_position()
  bash << @builder.set_position(x,y)
  height.times do
    bash << @builder.write(' ' * width, font)
    bash << @builder.move_position(-width, 1)
  end
  bash << @builder.restore_position()
  print @builder.write(bash, font)
end

#draw_window(x, y, width, height, text = '', font = nil, border = BORDER_UTF_DOUBLE, text_wrap = ["\u2561","\u255E"]) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bash-visual/console.rb', line 92

def draw_window(x, y, width, height, text = '', font = nil, border = BORDER_UTF_DOUBLE, text_wrap = ["\u2561","\u255E"])
  raise 'width,height must be great than 2' if (width < 3 or height < 3)

  text = text.slice(0, width - 2)
  if text.size < (width - 3) && (text_wrap.instance_of? Array)
    text = text_wrap[0] + text + text_wrap[1]
  end
  text = text.center(width - 2, border[1])
  bash = ''
  bash << @builder.save_position()
  bash << @builder.set_position(x,y)

  bash << @builder.write(border[0] + text +  border[2], font)
  (height - 2).times do
    bash << @builder.move_position(-width, 1)
    bash << @builder.write(border[3] + ' ' * (width - 2) + border[4], font)
  end
  bash << @builder.move_position(-width, 1)
  bash << @builder.write(border[5] + border[6] * (width - 2) + border[7], font)

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

#erase_to_end_lineObject



57
58
59
# File 'lib/bash-visual/console.rb', line 57

def erase_to_end_line
  print @builder.erase_to_end_line
end

#move_position(offset_x, offset_y) ⇒ Object



36
37
38
39
40
# File 'lib/bash-visual/console.rb', line 36

def move_position(offset_x, offset_y)
  @current_x += offset_x
  @current_y += offset_y
  print @builder.move_position(offset_x, offset_y)
end

#position=(coord) ⇒ Object



31
32
33
34
# File 'lib/bash-visual/console.rb', line 31

def position= coord
  @current_x , @current_y = *coord
  print @builder.set_position(@current_x, @current_y)
end

#write(text, font = @font) ⇒ Object



49
50
51
# File 'lib/bash-visual/console.rb', line 49

def write (text, font = @font)
  print @builder.write(text, font)
end

#write_ln(text, font = @font) ⇒ Object



53
54
55
# File 'lib/bash-visual/console.rb', line 53

def write_ln (text, font = @font)
  print @builder.write_ln(text, font)
end

#write_to_position(x, y, text, font = @font) ⇒ Object

Записать что-то в определенной позиции, а потом вернуться на текущую Если необходимо сохранить позицию после записи - используйте связку move_position/position= и write



45
46
47
# File 'lib/bash-visual/console.rb', line 45

def write_to_position (x, y, text, font = @font)
  print @builder.write_to_position(x, y, text, font)
end