module Enumerable
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
Public Instance Methods
checkbox()
click to toggle source
the checkbox widget.
A checkbox allows a user to select same value from Enumerable
. @example
['a', 'b', 7].checkbox
@return [Array] the selected of values
# File lib/term/checkbox.rb, line 14 def checkbox size.times { print $/ } @begin_pos = IO.up(size).hide.pos @checkbox_res = [] process_input_checkbox IO.show @checkbox_res end
redio()
click to toggle source
the redio Component.
@example redio
['a', 'b', 7].redio
@return [Object] the redio val
# File lib/term/redio.rb, line 13 def redio size.times { print $/ } @begin_pos = IO.up(size).hide.pos process_input_redio IO.show @redio_res end
Private Instance Methods
print_checkbox(index = 0)
click to toggle source
print checkbox.
# File lib/term/checkbox.rb, line 33 def print_checkbox(index = 0) IO.to @begin_pos @res = index.to_s.to_i % size @item_ys = [] each_with_index do |item, num| print_checkbox_item item, num end end
print_checkbox_item(item, num)
click to toggle source
print checkbox item.
# File lib/term/checkbox.rb, line 43 def print_checkbox_item(item, num) @item_ys[num] = IO.pos[:y] num == @res ? print('➡️ ') : print(' ') if @checkbox_res.include? num print " ☑️ #{num}. #{item}#{$/}" else print " ⬜️ #{num}. #{item}#{$/}" end end
print_redio(index = 0)
click to toggle source
print redio.
# File lib/term/redio.rb, line 24 def print_redio(index = 0) IO.to @begin_pos @redio_res = index.to_i % size @item_ys = [] each_with_index do |item, num| print_redio_item item, num end end
print_redio_item(item, num)
click to toggle source
print redio item.
# File lib/term/redio.rb, line 34 def print_redio_item(item, num) @item_ys[num] = IO.pos[:y] print "#{@redio_res == num ? '🔘' : '⭕️'} #{num}. #{item}#{$/}" end
process_checkbox_keyboard_input(key)
click to toggle source
process checkbox keyboard input event.
# File lib/term/checkbox.rb, line 75 def process_checkbox_keyboard_input(key) case key when :enter then @checkbox_loop = false when /\d/ then select_checkbox_item(key) when :down then print_checkbox(@res.succ) when :up then print_checkbox(@res.pred) when :space, :right, :left, :tab select_checkbox_item @res end end
process_checkbox_mouse_click(input)
click to toggle source
process checkbox mouse click event.
# File lib/term/checkbox.rb, line 65 def process_checkbox_mouse_click(input) type = input[:type] pos = input[:pos] if type == :left_pressed list_item = @item_ys.find_index pos[:y] select_checkbox_item list_item if list_item end end
process_input_checkbox()
click to toggle source
process checkbox input.
# File lib/term/checkbox.rb, line 54 def process_input_checkbox print_checkbox @checkbox_loop = true while @checkbox_loop input = IO.input process_checkbox_mouse_click input process_checkbox_keyboard_input input[:key] end end
process_input_redio()
click to toggle source
process redio input.
# File lib/term/redio.rb, line 40 def process_input_redio print_redio @redio_loop = true while @redio_loop input = IO.input process_redio_mouse_click input process_redio_keyboard_input input[:key] end end
process_redio_keyboard_input(key)
click to toggle source
process redio keyboard input event.
# File lib/term/redio.rb, line 60 def process_redio_keyboard_input(key) case key when :enter then @redio_loop = false when /\d/ then print_redio(key.to_s) when :tab, :down, :left, :space then print_redio(@redio_res.succ) when :up, :right then print_redio(@redio_res.pred) end end
process_redio_mouse_click(input)
click to toggle source
process redio mouse click event.
# File lib/term/redio.rb, line 51 def process_redio_mouse_click(input) if input[:type] == :left_pressed list_item = @item_ys.find_index input[:pos][:y] print_redio list_item if list_item @redio_loop = false end end
select_checkbox_item(index)
click to toggle source
select checkbox item.
# File lib/term/checkbox.rb, line 26 def select_checkbox_item(index) index = index.to_s.to_i % size @checkbox_res.delete(index) || @checkbox_res.push(index) print_checkbox index end