class FullscreenTui
Constants
- ARROWS
- VERSION
Attributes
header[R]
lines[RW]
renderer[R]
selection[RW]
Public Class Methods
new(headers:, renderer: nil, enable_jk: true)
click to toggle source
# File lib/fullscreen_tui.rb, line 13 def initialize headers:, renderer: nil, enable_jk: true @header = Header.new headers @footer = Footer.new @selection = 0 @renderer = renderer || Renderer.new @up_actions = [:arrow_up, enable_jk ? 'k' : nil].reject(&:nil?) @down_actions = [:arrow_down, enable_jk ? 'j' : nil].reject(&:nil?) end
Public Instance Methods
go_down()
click to toggle source
# File lib/fullscreen_tui.rb, line 84 def go_down footer.message = '' return if selection >= lines.size - 1 self.selection += 1 end
go_up()
click to toggle source
# File lib/fullscreen_tui.rb, line 77 def go_up footer.message = '' return if selection.zero? self.selection -= 1 end
key_press(key)
click to toggle source
# File lib/fullscreen_tui.rb, line 48 def key_press key case key when *@up_actions then go_up when *@down_actions then go_down when 'q', "\u0004" then throw :quit # ctrl-d else footer.message = "Unknown key: #{key.inspect}" end end
print_screen()
click to toggle source
# File lib/fullscreen_tui.rb, line 31 def print_screen clear rows, columns = TermInfo.screen_size header_text = header.output rows -= required_lines header_text, columns footer_text = footer.output width: columns, lines: lines.size, current_line: selection rows -= required_lines footer_text, columns renderer.header header_text unless header_text.empty? print_lines rows, columns renderer.footer footer_text end
react()
click to toggle source
# File lib/fullscreen_tui.rb, line 63 def react key = case char = STDIN.getch when "\u0003" then exit 1 when "\e" case subchar = STDIN.getch when '[' ARROWS.fetch(STDIN.getch) {|k| "\e[#{k}" } else "\e#{subchar}" end else char end key_press key end
read_full_line(message)
click to toggle source
# File lib/fullscreen_tui.rb, line 91 def read_full_line message puts message gets end
run()
click to toggle source
# File lib/fullscreen_tui.rb, line 22 def run catch :quit do loop do print_screen react end end end
selected_line()
click to toggle source
# File lib/fullscreen_tui.rb, line 57 def selected_line lines[selection] end
Private Instance Methods
clear()
click to toggle source
# File lib/fullscreen_tui.rb, line 129 def clear system(Gem.win_platform? ? 'cls' : 'clear') end
fill_remaining_lines(rows, used_lines)
click to toggle source
# File lib/fullscreen_tui.rb, line 116 def fill_remaining_lines rows, used_lines (rows - used_lines).times { renderer.line '~', selected: false } end
height(text, columns)
click to toggle source
# File lib/fullscreen_tui.rb, line 120 def height text, columns (text.size.to_f / columns).ceil end
print_lines(rows, columns)
click to toggle source
# File lib/fullscreen_tui.rb, line 98 def print_lines rows, columns used_lines = 0 offset = [selection - 5, 0].max lines[offset..-1].map(&:to_s).each.with_index do |line, i| selected = i + offset == selection used_lines += height line, columns if used_lines > rows renderer.line '...', selected: selected break end renderer.line line, selected: selected break if used_lines == rows end fill_remaining_lines rows, used_lines end
required_lines(text, columns)
click to toggle source
# File lib/fullscreen_tui.rb, line 124 def required_lines text, columns text.split($INPUT_RECORD_SEPARATOR) .inject(0) {|sum, line| sum + height(line, columns) } end