class RST::CursesController

Controll the curses-lib @example

cc = CursesController.new
cc.print_scren( ['Line 1', 'Line 2', ...] )
cc.clear
cc.status 'It works'

Constants

COLORS

Available colors provided by Curses

Public Class Methods

new(*) click to toggle source

Initialize and register a finalizer to close curses whenever this object gets destroyed by the GC.

# File lib/curses/curses_controller.rb, line 24
def initialize(*)
  init_curses
  ObjectSpace.define_finalizer( self, self.class.finalize )
  clear
end

Private Class Methods

finalize() click to toggle source

Finalize get’s called when the CG will free the object. The call to this function was registered in the constructor.

# File lib/curses/curses_controller.rb, line 117
def self.finalize
  proc do
    Curses.close_screen
  end
end

Public Instance Methods

clear() click to toggle source

Clear the screen and rewrite the status-bar

# File lib/curses/curses_controller.rb, line 56
def clear
  Curses.clear
  status "q=Quit c=Calendar <enter>=Clear screen"
end
noop() click to toggle source

No operation - called when an unknown key is pressed to show any response

# File lib/curses/curses_controller.rb, line 51
def noop
  write(0,0,"NOOP #{Time.now.to_s}"+" "*5)
end
print_screen(lines) { |line,lno| ... } click to toggle source

Outputs array of lines from 0,0 to max-lines @param [Array] lines - lines to output

status(message) click to toggle source

update the status-line @param [String] message

# File lib/curses/curses_controller.rb, line 44
def status(message)
  message += '-' * (status_line_length - message.length)
  write(status_line, 0, message, 1 )
end

Private Instance Methods

height() click to toggle source

@return [Integer] - number of lines available at current screen

# File lib/curses/curses_controller.rb, line 78
def height
  Curses.lines
end
init_colors() click to toggle source

Create color-pairs fg/bg for each combination of given colors (see top of file)

# File lib/curses/curses_controller.rb, line 94
def init_colors
  @color_pairs = []
  COLORS.each_with_index do |fg,fgi|
    (COLORS-[fg]).each_with_index do |bg,bgi|
      Curses.init_pair(fgi*10+bgi,fg,bg)
      @color_pairs << fgi*10+bgi
    end
  end
end
init_curses() click to toggle source

Initialize the Curses-environment

# File lib/curses/curses_controller.rb, line 83
def init_curses
  Curses.noecho # do not show typed keys
  Curses.init_screen
  Curses.start_color
  @screen=Curses.stdscr
  @screen.keypad(true) # enable arrow keys
  init_colors
end
status_line() click to toggle source

@return [Integer] - bottom of screen

# File lib/curses/curses_controller.rb, line 110
def status_line
  Curses.lines-1
end
status_line_length() click to toggle source

@return [Integer] - number of columns (width) of the screen

# File lib/curses/curses_controller.rb, line 105
def status_line_length
  Curses.cols
end
write(line, column, text, color_pair=@color_pairs.sample) click to toggle source

Output a line of text and clear to the end of line @param [Integer] line - the line to write to 0=top of screen @param [Integer] column - the column to start at 0=left most col. @param [String] text - the text to write @param [Integer] color_pair - which color to use (or random)

# File lib/curses/curses_controller.rb, line 70
def write(line, column, text, color_pair=@color_pairs.sample)
  Curses.setpos(line, column)
  Curses.attron(color_pair(color_pair)|A_NORMAL) do
    Curses.addstr(text+ ' '*(status_line_length-column-text.length))
  end
end