class Cobi::Screen
Attributes
screen_height[R]
Public Class Methods
new()
click to toggle source
# File lib/cobi/screen.rb, line 12 def initialize @elements = [] @screen_height = `tput lines`.to_i @running = false end
Public Instance Methods
field(text, length = 10)
click to toggle source
# File lib/cobi/screen.rb, line 41 def field(text, length = 10) @elements << Field.new(text, length) self end
heading(text)
click to toggle source
# File lib/cobi/screen.rb, line 36 def heading(text) @elements << Heading.new(text) self end
on_submit(&block)
click to toggle source
# File lib/cobi/screen.rb, line 31 def on_submit(&block) @on_submit = block self end
run()
click to toggle source
# File lib/cobi/screen.rb, line 18 def run # Focus the first field @elements.find { |e| e.is_a?(Field) }.focused = true @running = true while @running draw get_command end values end
Private Instance Methods
draw()
click to toggle source
# File lib/cobi/screen.rb, line 52 def draw # first clear the screen, in case the terminal size calculation is offset by footers/tmux separators puts pastel.black("\e[H\e[2J") @elements.each do |e| puts e.to_s end puts pastel.black("\n" * (screen_height - @elements.length - 4)) puts pastel.green("Tab to switch fields | Enter to submit") end
get_command()
click to toggle source
# File lib/cobi/screen.rb, line 64 def get_command cmd = STDIN.getch exit(1) if cmd == "\u0003" # exit on Ctrl+C if cmd == "\t" # tab handle_switch_focus elsif cmd == "\n" || cmd == "\r" # TODO handle_submit elsif cmd =~/[[:alpha:]]/ || cmd =~ /[[:digit:]]/ handle_character(cmd) end end
handle_character(char)
click to toggle source
# File lib/cobi/screen.rb, line 87 def handle_character(char) @elements.find { |e| e.focused? }.value += char end
handle_submit()
click to toggle source
# File lib/cobi/screen.rb, line 77 def handle_submit @running = false end
handle_switch_focus()
click to toggle source
# File lib/cobi/screen.rb, line 91 def handle_switch_focus focused_index = @elements.find_index { |e| e.focused? } next_index = focused_index + 1 until @elements[next_index] && @elements[next_index].is_a?(Field) next_index += 1 next_index = 0 unless @elements[next_index] end @elements[focused_index].focused = false @elements[next_index].focused = true end
pastel()
click to toggle source
# File lib/cobi/screen.rb, line 48 def pastel @pastel ||= Pastel.new.on_black end
values()
click to toggle source
# File lib/cobi/screen.rb, line 81 def values @elements.select { |e| e.is_a?(Field) } .map { |e| [e.text, e.value] } .to_h end