Code Coverage
 
Lines
Covered
26.56% covered (danger)
26.56%
17 / 64
1
require 'curses'
#require 'io/console'
1
module TheFox
1
module TermKit
# Default Curses read input timeout.
1
CURSES_TIMEOUT = 200
1
class CursesApp < UIApp
1
def initialize
0
@curses_timeout = CURSES_TIMEOUT
0
@ui_inited = false
0
@ui_closed = false
0
super()
#puts 'CursesApp->initialize'
end
1
def curses_timeout=(curses_timeout)
0
@curses_timeout = curses_timeout
0
Curses.timeout = @curses_timeout
end
##
# See UIApp `run_cycle()` method.
1
def run_cycle
0
super()
#puts 'CursesApp->run_cycle'
0
handle_user_input
end
##
# See UIApp `draw_point()` method.
1
def draw_point(point, content)
# @logger.debug("draw_point #{point} #{content.inspect}")
0
content_s = content
0
foreground_color = nil
0
background_color = nil
0
if content.is_a?(ViewContent)
# @logger.debug("draw_point #{point} content is ViewContent")
0
content_s = content.char
# @logger.debug("draw_point #{point} #{content.foreground_color} #{content.background_color}")
0
foreground_color = CursesColor::COLORS[content.foreground_color]
0
background_color = CursesColor::COLORS[content.background_color]
end
0
c_attr = Curses::A_NORMAL
0
if !foreground_color.nil? && !background_color.nil?
0
Curses.init_pair(1, foreground_color, background_color)
0
c_attr = Curses.color_pair(1)
end
# @logger.debug("draw_point #{point} '#{content_s}' #{foreground_color.inspect} #{background_color.inspect}")
0
begin
0
Curses.setpos(point.y, point.x)
0
Curses.attron(c_attr) do
0
Curses.addstr(content_s)
end
0
rescue Exception => e
0
@logger.error("draw_point: #{e}")
end
# @logger.debug("draw_point #{point} #{content.inspect} DONE")
end
##
# See UIApp `ui_refresh()` method.
1
def ui_refresh
0
Curses.refresh
end
##
# See UIApp `ui_max_x()` method.
1
def ui_max_x
0
Curses.cols
end
##
# See UIApp `ui_max_y()` method.
1
def ui_max_y
0
Curses.rows
end
1
protected
##
# See UIApp `ui_init()` method.
1
def ui_init
#puts "CursesApp->ui_init '#{@curses_timeout}'"
0
raise 'ui already initialized' if @ui_inited
0
@ui_inited = true
0
super()
# @logger.debug("init Curses")
0
Curses.noecho
0
Curses.timeout = @curses_timeout
0
Curses.curs_set(0)
0
Curses.init_screen
0
Curses.start_color
0
Curses.use_default_colors
0
Curses.crmode
0
Curses.stdscr.keypad(true)
# @logger.debug("color_pairs: #{Curses.color_pairs}")
# Curses.init_pair(1, Curses::COLOR_BLACK, Curses::COLOR_GREEN)
# Curses.setpos(0, 0)
# Curses.addstr('INIT OK')
# Curses.refresh
end
##
# See UIApp `ui_close()` method.
1
def ui_close
#puts "CursesApp->ui_close"
0
raise 'ui already closed' if @ui_closed
0
@ui_closed = true
# Curses.setpos(10, 0)
# Curses.addstr('CLOSE ')
# Curses.refresh
# sleep(2)
0
Curses.refresh
0
Curses.stdscr.clear
0
Curses.stdscr.refresh
0
Curses.stdscr.close
0
Curses.close_screen
end
1
private
1
def handle_user_input
0
key_down(Curses.getch)
#key_down(IO.console.getch)
end
end
end
end