#!/usr/bin/env ruby # encoding: UTF-8 require 'curses' require 'optparse' $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) def parse_options options = {} parser = OptionParser.new do |opts| opts.banner = < we can use timeouts Curses.init_screen begin yield ensure Curses.close_screen end end def display(lines, offset, color) @screen ||= [] # current screen is used as cache Curses.attrset color lines.each_with_index do |content, line| line += offset clearer = Curses.stdscr.maxx - content.size clearer = " " * clearer content += clearer next if @screen[line] == content @screen[line] = content write(line, 0, content) write(line, 0, (rand(899)+100).to_s) if @options[:debug_cache] end end def show_app(app) lines = app.view.naive_split("\n") # TODO move this logic into application display([lines.first], 0, Curses::A_REVERSE) display(lines[1..-2], 1, Curses::A_NORMAL) display([lines.last], Curses.stdscr.maxy - 1, Curses::A_REVERSE) Curses.setpos(app.cursor.line, app.cursor.column) end def debug_key(key) @key_line ||= -1 @key_line = (@key_line + 1) % Curses.stdscr.maxy write(@key_line, 0, "#{key}---") end def log(stuff) File.open('ruco.log','a'){|f| f.puts stuff } end @options = parse_options require 'ruco' require 'ruco/keyboard' # needs curses <-> not loaded for specs app = Ruco::Application.new(ARGV[0], :lines => Curses.stdscr.maxy, :columns => Curses.stdscr.maxx) init_screen do show_app app Keyboard.listen do |key| debug_key(key) if @options[:debug_keys] result = app.key key break if result == :quit show_app app end end