Class: RubyInstaller::Runtime::ConsoleUi::ButtonMatrix
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Colors
|
|
| Inherits: | Object |
| Defined in: | lib/ruby_installer/runtime/console_ui.rb |
Constant Summary
-
BUTTON_BORDERS =
# File 'lib/ruby_installer/runtime/console_ui.rb', line 91{ thin: "┌─┐" + "│ │" + "└─┘" , fat: "┏━┓" + "┃ ┃" + "┗━┛" , double: "╔═╗" + "║ ║" + "╚═╝" , }
Class Method Summary
- .new(ncols: 3) ⇒ ButtonMatrix constructor
Instance Attribute Summary
Instance Method Summary
- #add_button(text, &action)
- #click(x, y)
- #cursor(direction)
-
#repaint(width: , height: )
Paint the boxes.
- #select
- #box_border(ncol, nrow) private
Constructor Details
.new(ncols: 3) ⇒ ButtonMatrix
# File 'lib/ruby_installer/runtime/console_ui.rb', line 52
def initialize(ncols: 3) @ncols = ncols @boxes = [] @con = IO.console @selected = 0 @headline = "" enable_colors end
Instance Attribute Details
#headline (rw)
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 50
attr_accessor :headline
#selected (rw)
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 49
attr_accessor :selected
Instance Method Details
#add_button(text, &action)
[ GitHub ]#box_border(ncol, nrow) (private)
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 103
private def box_border(ncol, nrow) box_idx = ncol + nrow * @ncols selected = box_idx == @selected border = BUTTON_BORDERS[selected ? :double : :thin] border = border.each_char.map do |ch| selected ? green(ch) : blue(ch) end box = @boxes[box_idx]&.text return box, border, box_idx end
#click(x, y)
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 83
def click(x, y) s = @slines&.[](y)&.[](x) if s self.selected = s true end end
#cursor(direction)
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 66
def cursor(direction) case direction when :left @selected -= 1 if @selected > 0 when :up @selected -= @ncols if @selected - @ncols >= 0 when :right @selected += 1 if @selected + 1 < @boxes.size when :down @selected += @ncols if @selected + @ncols < @boxes.size end end
#repaint(width: , height: )
Paint the boxes
# File 'lib/ruby_installer/runtime/console_ui.rb', line 115
def repaint(width: @con.winsize[1], height: @con.winsize[0]) obw = (width.to_f / @ncols).floor spw = obw - 4 nrows = (@boxes.size.to_f / @ncols).ceil obh = (height.to_f / nrows).floor sph = obh - 2 line = +"" slines = [[]] nrows.times do |nrow| @ncols.times do |ncol| box, border, box_idx = box_border(ncol, nrow) if box line += " #{border[0]}" + "#{border[1]}" * spw + "#{border[2]} " slines.last.append(*([box_idx] * obw)) end end line += "\n" slines << [] sph.times do |spy| @ncols.times do |ncol| box, border, box_idx = box_border(ncol, nrow) if box text_lines = box.lines.map(&:chomp) text_pos = spy - (sph - text_lines.size) / 2 text_line = text_lines[text_pos] if text_pos >= 0 text_line ||= "" spl = (spw - text_line.size) / 2 spr = spw - spl - text_line.size line += " #{border[3]}" + " " * spl + text_line + " " * spr + "#{border[5]} " slines.last.append(*([box_idx] * obw)) end end line += "\n" slines << [] end @ncols.times do |ncol| box, border, box_idx = box_border(ncol, nrow) if box line += " #{border[6]}" + "#{border[7]}" * spw + "#{border[8]} " slines.last.append(*([box_idx] * obw)) end end line += "\n" slines << [] end print "\n#{headline}\n"+line.chomp @slines = slines end
#select
[ GitHub ]# File 'lib/ruby_installer/runtime/console_ui.rb', line 79
def select @boxes[@selected].action.call end