module Curses
Description¶ ↑
An implementation of the CRT screen handling and optimization library.
Structures and such¶ ↑
Classes¶ ↑
-
Curses::Window- class with the means to draw a window or box -
Curses::MouseEvent- class for collecting mouse events
Modules¶ ↑
Curses-
The curses implementation
Curses::Key-
Collection of constants for keypress events
Examples¶ ↑
-
hello.rb
require "curses" def show_message(message) height = 5 width = message.length + 6 top = (Curses.lines - height) / 2 left = (Curses.cols - width) / 2 win = Curses::Window.new(height, width, top, left) win.box("|", "-") win.setpos(2, 3) win.addstr(message) win.refresh win.getch win.close end Curses.init_screen begin Curses.crmode Curses.setpos((Curses.lines - 1) / 2, (Curses.cols - 11) / 2) Curses.addstr("Hit any key") Curses.refresh Curses.getch show_message("Hello, World!") ensure Curses.close_screen end
-
rain.rb
require "curses" def onsig(signal) Curses.close_screen exit signal end def place_string(y, x, string) Curses.setpos(y, x) Curses.addstr(string) end def cycle_index(index) (index + 1) % 5 end %w[HUP INT QUIT TERM].each do |sig| unless trap(sig, "IGNORE") == "IGNORE" # previous handler trap(sig) {|s| onsig(s) } end end Curses.init_screen Curses.nl Curses.noecho Curses.curs_set 0 srand xpos, ypos = {}, {} x_range = 2..(Curses.cols - 3) y_range = 2..(Curses.lines - 3) (0..4).each do |i| xpos[i], ypos[i] = rand(x_range), rand(y_range) end i = 0 loop do x, y = rand(x_range), rand(y_range) place_string(y, x, ".") place_string(ypos[i], xpos[i], "o") i = cycle_index(i) place_string(ypos[i], xpos[i], "O") i = cycle_index(i) place_string(ypos[i] - 1, xpos[i], "-") place_string(ypos[i], xpos[i] - 1, "|.|") place_string(ypos[i] + 1, xpos[i], "-") i = cycle_index(i) place_string(ypos[i] - 2, xpos[i], "-") place_string(ypos[i] - 1, xpos[i] - 1, "/ \\") place_string(ypos[i], xpos[i] - 2, "| O |") place_string(ypos[i] + 1, xpos[i] - 1, "\\ /") place_string(ypos[i] + 2, xpos[i], "-") i = cycle_index(i) place_string(ypos[i] - 2, xpos[i], " ") place_string(ypos[i] - 1, xpos[i] - 1, " ") place_string(ypos[i], xpos[i] - 2, " ") place_string(ypos[i] + 1, xpos[i] - 1, " ") place_string(ypos[i] + 2, xpos[i], " ") xpos[i], ypos[i] = x, y Curses.refresh sleep(0.5) end
Constants
- ALL_MOUSE_EVENTS
-
Mouse event mask: report all button state changes
See
Curses.getmouse - ALT_0
-
PDCurses-specific keys
- ALT_1
- ALT_2
- ALT_3
- ALT_4
- ALT_5
- ALT_6
- ALT_7
- ALT_8
- ALT_9
- ALT_A
- ALT_B
- ALT_C
- ALT_D
- ALT_E
- ALT_F
- ALT_G
- ALT_H
- ALT_I
- ALT_J
- ALT_K
- ALT_L
- ALT_M
- ALT_N
- ALT_O
- ALT_P
- ALT_Q
- ALT_R
- ALT_S
- ALT_T
- ALT_U
- ALT_V
- ALT_W
- ALT_X
- ALT_Y
- ALT_Z
- A_ALTCHARSET
-
Attribute mask: Alternate character set
See
Curses.attrset - A_ATTRIBUTES
-
Character attribute mask: Bit-mask to extract attributes
See
Curses.inchorCurses::Window.inch - A_BLINK
-
Attribute mask: Blinking
See
Curses.attrset - A_BOLD
-
Attribute mask: Extra bright or bold
See
Curses.attrset - A_CHARTEXT
-
Attribute mask: Bit-mask to extract a character
See
Curses.attrset - A_COLOR
-
Character attribute mask: Bit-mask to extract color-pair field information
See
Curses.inchorCurses::Window.inch - A_DIM
-
Attribute mask: Half bright
See
Curses.attrset - A_HORIZONTAL
-
Attribute mask: horizontal highlight
Check system curs_attr(3x) for support
- A_INVIS
-
Attribute mask: Invisible or blank mode
See
Curses.attrset - A_LEFT
-
Attribute mask: left highlight
Check system curs_attr(3x) for support
- A_LOW
-
Attribute mask: low highlight
Check system curs_attr(3x) for support
- A_NORMAL
-
Attribute mask: Normal display (no highlight)
See
Curses.attrset - A_PROTECT
-
Attribute mask: Protected mode
See
Curses.attrset - A_REVERSE
-
Attribute mask: Reverse video
See
Curses.attrset - A_RIGHT
-
Attribute mask: right highlight
Check system curs_attr(3x) for support
- A_STANDOUT
-
Attribute mask: Best highlighting mode of the terminal.
See
Curses.attrset - A_TOP
-
Attribute mask: top highlight
Check system curs_attr(3x) for support
- A_UNDERLINE
-
Attribute mask: Underlining
See
Curses.attrset - A_VERTICAL
-
Attribute mask: vertical highlight
Check system curs_attr(3x) for support
- BUTTON1_CLICKED
-
Mouse event mask: mouse button 1 clicked
See
Curses.getmouse - BUTTON1_DOUBLE_CLICKED
-
Mouse event mask: mouse button 1 double clicked
See
Curses.getmouse - BUTTON1_PRESSED
-
Mouse event mask: mouse button 1 down
See
Curses.getmouse - BUTTON1_RELEASED
-
Mouse event mask: mouse button 1 up
See
Curses.getmouse - BUTTON1_TRIPLE_CLICKED
-
Mouse event mask: mouse button 1 triple clicked
See
Curses.getmouse - BUTTON2_CLICKED
-
Mouse event mask: mouse button 2 clicked
See
Curses.getmouse - BUTTON2_DOUBLE_CLICKED
-
Mouse event mask: mouse button 2 double clicked
See
Curses.getmouse - BUTTON2_PRESSED
-
Mouse event mask: mouse button 2 down
See
Curses.getmouse - BUTTON2_RELEASED
-
Mouse event mask: mouse button 2 up
See
Curses.getmouse - BUTTON2_TRIPLE_CLICKED
-
Mouse event mask: mouse button 2 triple clicked
See
Curses.getmouse - BUTTON3_CLICKED
-
Mouse event mask: mouse button 3 clicked
See
Curses.getmouse - BUTTON3_DOUBLE_CLICKED
-
Mouse event mask: mouse button 3 double clicked
See
Curses.getmouse - BUTTON3_PRESSED
-
Mouse event mask: mouse button 3 down
See
Curses.getmouse - BUTTON3_RELEASED
-
Mouse event mask: mouse button 3 up
See
Curses.getmouse - BUTTON3_TRIPLE_CLICKED
-
Mouse event mask: mouse button 3 triple clicked
See
Curses.getmouse - BUTTON4_CLICKED
-
Mouse event mask: mouse button 4 clicked
See
Curses.getmouse - BUTTON4_DOUBLE_CLICKED
-
Mouse event mask: mouse button 4 double clicked
See
Curses.getmouse - BUTTON4_PRESSED
-
Mouse event mask: mouse button 4 down
See
Curses.getmouse - BUTTON4_RELEASED
-
Mouse event mask: mouse button 4 up
See
Curses.getmouse - BUTTON4_TRIPLE_CLICKED
-
Mouse event mask: mouse button 4 triple clicked
See
Curses.getmouse - BUTTON_ALT
-
Mouse event mask: alt was down during button state change
See
Curses.getmouse - BUTTON_CTRL
-
Mouse event mask: control was down during button state change
See
Curses.getmouse - BUTTON_SHIFT
-
Mouse event mask: shift was down during button state change
See
Curses.getmouse - COLORS
-
Number of the colors available
- COLOR_BLACK
-
Value of the color black
- COLOR_BLUE
-
Value of the color blue
- COLOR_CYAN
-
Value of the color cyan
- COLOR_GREEN
-
Value of the color green
- COLOR_MAGENTA
-
Value of the color magenta
- COLOR_RED
-
Value of the color red
- COLOR_WHITE
-
Value of the color white
- COLOR_YELLOW
-
Value of the color yellow
- KEY_A1
-
A1 Upper left of keypad
- KEY_A3
-
A3 Upper right of keypad
- KEY_B2
-
B2 Center of keypad
- KEY_BACKSPACE
-
BACKSPACE Backspace
- KEY_BEG
-
BEG Beginning key
- KEY_BREAK
-
BREAK Break key
- KEY_BTAB
-
KEY_BTABBack tab key - KEY_C1
-
C1 Lower left of keypad
- KEY_C3
-
C3 Lower right of keypad
- KEY_CANCEL
-
CANCEL Cancel key
- KEY_CATAB
-
CATAB Clear all tabs
- KEY_CLEAR
-
CLEAR Clear
Screen - KEY_CLOSE
-
CLOSE Close key
- KEY_COMMAND
-
COMMAND Cmd (command) key
- KEY_COPY
-
COPY Copy key
- KEY_CREATE
-
CREATE Create key
- KEY_CTAB
-
CTAB Clear tab
- KEY_DC
-
DC Delete character
- KEY_DL
-
DL Delete line
- KEY_DOWN
-
DOWN the down arrow key
- KEY_EIC
-
EIC Enter insert char mode
- KEY_END
-
END End key
- KEY_ENTER
-
ENTER Enter or send
- KEY_EOL
-
EOL Clear to end of line
- KEY_EOS
-
EOS Clear to end of screen
- KEY_EXIT
-
EXIT Exit key
- KEY_FIND
-
FIND Find key
- KEY_HELP
-
HELP Help key
- KEY_HOME
-
HOME Home key (upward+left arrow)
- KEY_IC
-
IC Insert char or enter insert mode
- KEY_IL
-
IL Insert line
- KEY_LEFT
-
LEFT the left arrow key
- KEY_LL
-
LL Home down or bottom (lower left)
- KEY_MARK
-
MARK Mark key
- KEY_MAX
-
MAX The maximum allowed curses key value.
- KEY_MESSAGE
-
MESSAGE Message key
- KEY_MIN
-
MIN The minimum allowed curses key value.
- KEY_MOUSE
-
MOUSE Mouse event read
- KEY_MOVE
-
MOVE Move key
- KEY_NEXT
-
NEXT Next object key
- KEY_NPAGE
-
NPAGE Next page
- KEY_OPEN
-
OPEN Open key
- KEY_OPTIONS
-
OPTIONS Options key
- KEY_PPAGE
-
PPAGE Previous page
- KEY_PREVIOUS
-
PREVIOUS Previous object key
- KEY_PRINT
-
PRINT Print or copy
- KEY_REDO
-
REDO Redo key
- KEY_REFERENCE
-
REFERENCE Reference key
- KEY_REFRESH
-
REFRESH Refresh key
- KEY_REPLACE
-
REPLACE Replace key
- KEY_RESET
-
RESET Reset or hard reset
- KEY_RESIZE
-
RESIZE
ScreenResized - KEY_RESTART
-
RESTART Restart key
- KEY_RESUME
-
RESUME Resume key
- KEY_RIGHT
-
RIGHT the right arrow key
- KEY_SAVE
-
SAVE Save key
- KEY_SBEG
-
SBEG Shifted beginning key
- KEY_SCANCEL
-
SCANCEL Shifted cancel key
- KEY_SCOMMAND
-
SCOMMAND Shifted command key
- KEY_SCOPY
-
SCOPY Shifted copy key
- KEY_SCREATE
-
SCREATE Shifted create key
- KEY_SDC
-
SDC Shifted delete char key
- KEY_SDL
-
SDL Shifted delete line key
- KEY_SELECT
-
SELECT Select key
- KEY_SEND
-
SEND Shifted end key
- KEY_SEOL
-
SEOL Shifted clear line key
- KEY_SEXIT
-
SEXIT Shifted exit key
- KEY_SF
-
SF Scroll 1 line forward
- KEY_SFIND
-
SFIND Shifted find key
- KEY_SHELP
-
SHELP Shifted help key
- KEY_SHOME
-
SHOME Shifted home key
- KEY_SIC
-
SIC Shifted input key
- KEY_SLEFT
-
SLEFT Shifted left arrow key
- KEY_SMESSAGE
-
SMESSAGE Shifted message key
- KEY_SMOVE
-
SMOVE Shifted move key
- KEY_SNEXT
-
SNEXT Shifted next key
- KEY_SOPTIONS
-
SOPTIONS Shifted options key
- KEY_SPREVIOUS
-
SPREVIOUS Shifted previous key
- KEY_SPRINT
-
SPRINT Shifted print key
- KEY_SR
-
SR Scroll 1 line backward (reverse)
- KEY_SREDO
-
SREDO Shifted redo key
- KEY_SREPLACE
-
SREPLACE Shifted replace key
- KEY_SRESET
-
SRESET Soft (partial) reset
- KEY_SRIGHT
-
SRIGHT Shifted right arrow key
- KEY_SRSUME
-
SRSUME Shifted resume key
- KEY_SSAVE
-
SSAVE Shifted save key
- KEY_SSUSPEND
-
SSUSPEND Shifted suspend key
- KEY_STAB
-
STAB Set tab
- KEY_SUNDO
-
SUNDO Shifted undo key
- KEY_SUSPEND
-
SUSPEND Suspend key
- KEY_UNDO
-
UNDO Undo key
- KEY_UP
-
UP the up arrow key
- O_ACTIVE
- O_AUTOSKIP
- O_BLANK
- O_DYNAMIC_JUSTIFY
- O_EDIT
- O_IGNORECASE
- O_MOUSE_MENU
- O_NONCYCLIC
- O_NO_LEFT_STRIP
- O_NULLOK
- O_ONEVALUE
- O_PASSOK
- O_PUBLIC
- O_ROWMAJOR
- O_SELECTABLE
- O_SHOWDESC
- O_SHOWMATCH
- O_STATIC
- O_VISIBLE
- O_WRAP
- PDC_KEY_MODIFIER_ALT
- PDC_KEY_MODIFIER_CONTROL
- PDC_KEY_MODIFIER_NUMLOCK
- PDC_KEY_MODIFIER_SHIFT
- REPORT_MOUSE_POSITION
-
Mouse event mask: report mouse movement
See
Curses.getmouse - REQ_BACK_PATTERN
- REQ_BEG_FIELD
- REQ_BEG_LINE
- REQ_CLEAR_PATTERN
- REQ_CLR_EOF
- REQ_CLR_EOL
- REQ_CLR_FIELD
- REQ_DEL_CHAR
- REQ_DEL_LINE
- REQ_DEL_PREV
- REQ_DEL_WORD
- REQ_DOWN_CHAR
- REQ_DOWN_FIELD
- REQ_DOWN_ITEM
- REQ_END_FIELD
- REQ_END_LINE
- REQ_FIRST_FIELD
- REQ_FIRST_ITEM
- REQ_FIRST_PAGE
- REQ_INS_CHAR
- REQ_INS_LINE
- REQ_INS_MODE
- REQ_LAST_FIELD
- REQ_LAST_ITEM
- REQ_LAST_PAGE
- REQ_LEFT_CHAR
- REQ_LEFT_FIELD
- REQ_LEFT_ITEM
- REQ_NEW_LINE
- REQ_NEXT_CHAR
- REQ_NEXT_CHOICE
- REQ_NEXT_FIELD
- REQ_NEXT_ITEM
- REQ_NEXT_LINE
- REQ_NEXT_MATCH
- REQ_NEXT_PAGE
- REQ_NEXT_WORD
- REQ_OVL_MODE
- REQ_PREV_CHAR
- REQ_PREV_CHOICE
- REQ_PREV_FIELD
- REQ_PREV_ITEM
- REQ_PREV_LINE
- REQ_PREV_MATCH
- REQ_PREV_PAGE
- REQ_PREV_WORD
- REQ_RIGHT_CHAR
- REQ_RIGHT_FIELD
- REQ_RIGHT_ITEM
- REQ_SCR_BCHAR
- REQ_SCR_BHPAGE
- REQ_SCR_BLINE
- REQ_SCR_BPAGE
- REQ_SCR_DLINE
- REQ_SCR_DPAGE
- REQ_SCR_FCHAR
- REQ_SCR_FHPAGE
- REQ_SCR_FLINE
- REQ_SCR_FPAGE
- REQ_SCR_HBHALF
- REQ_SCR_HBLINE
- REQ_SCR_HFHALF
- REQ_SCR_HFLINE
- REQ_SCR_ULINE
- REQ_SCR_UPAGE
- REQ_SFIRST_FIELD
- REQ_SLAST_FIELD
- REQ_SNEXT_FIELD
- REQ_SPREV_FIELD
- REQ_TOGGLE_ITEM
- REQ_UP_CHAR
- REQ_UP_FIELD
- REQ_UP_ITEM
- REQ_VALIDATION
- TYPE_ALNUM
- TYPE_ALPHA
- TYPE_ENUM
- TYPE_INTEGER
- TYPE_NUMERIC
- TYPE_REGEXP
- VERSION
-
Identifies curses library version.
-
“ncurses 5.9.20110404”
-
“PDCurses 3.4 - Public Domain 2008”
-
“curses (SVR4)” (System V curses)
-
“curses (unknown)” (The original BSD curses? NetBSD maybe.)
-
Public Class Methods
Source
static VALUE
curses_escdelay_get(VALUE obj)
{
return INT2NUM(ESCDELAY);
}
Returns the total time, in milliseconds, for which curses will await a character sequence, e.g., a function key
Source
static VALUE
curses_escdelay_set(VALUE obj, VALUE val)
{
ESCDELAY = NUM2INT(val);
return INT2NUM(ESCDELAY);
}
Sets the ESCDELAY to Integer value
Source
static VALUE
curses_tabsize_get(VALUE ojb)
{
return INT2NUM(TABSIZE);
}
Returns the number of positions in a tab.
Source
static VALUE
curses_tabsize_set(VALUE obj, VALUE val)
{
TABSIZE = NUM2INT(val);
return INT2NUM(TABSIZE);
}
Sets the TABSIZE to Integer value
Source
static VALUE
curses_addch(VALUE obj, VALUE ch)
{
curses_stdscr();
addch(OBJ2CHTYPE(ch));
return Qnil;
}
Add a character ch, with attributes, then advance the cursor.
see also the system manual for curs_addch(3)
Source
static VALUE
curses_addstr(VALUE obj, VALUE str)
{
StringValue(str);
#if defined(HAVE_ADDNWSTR) && defined(_WIN32)
str = rb_str_export_to_enc(str, get_wide_encoding());
curses_stdscr();
if (!NIL_P(str)) {
addnwstr((wchar_t *)RSTRING_PTR(str), RSTRING_LEN(str) / sizeof(wchar_t));
}
#else
str = rb_str_export_to_enc(str, terminal_encoding);
curses_stdscr();
if (!NIL_P(str)) {
addstr(StringValueCStr(str));
}
#endif
return Qnil;
}
add a string of characters str, to the window and advance cursor
Source
static VALUE
curses_assume_default_colors(VALUE obj, VALUE fg, VALUE bg)
{
curses_stdscr();
assume_default_colors(NUM2INT(fg), NUM2INT(bg));
return Qnil;
}
tells which colors to paint for color pair 0.
see also the system manual for default_colors(3)
Source
static VALUE
curses_attroff(VALUE obj, VALUE attrs)
{
curses_stdscr();
return window_attroff(rb_stdscr,attrs);
/* return INT2FIX(attroff(NUM2INT(attrs))); */
}
Turns off the named attributes attrs without affecting any others.
See also Curses::Window.attrset for additional information.
Source
static VALUE
curses_attron(VALUE obj, VALUE attrs)
{
curses_stdscr();
return window_attron(rb_stdscr,attrs);
/* return INT2FIX(attroff(NUM2INT(attrs))); */
}
Turns on the named attributes attrs without turning any other attributes on or off.
See also Curses::Window.attrset for additional information.
Source
static VALUE
curses_attrset(VALUE obj, VALUE attrs)
{
curses_stdscr();
return window_attrset(rb_stdscr,attrs);
/* return INT2FIX(attroff(NUM2INT(attrs))); */
}
Sets the current attributes of the given window to attrs.
see also Curses::Window.attrset
Source
static VALUE
curses_beep(VALUE obj)
{
#ifdef HAVE_BEEP
curses_stdscr();
beep();
#endif
return Qnil;
}
Sounds an audible alarm on the terminal, if possible; otherwise it flashes the screen (visual bell).
see also Curses.flash
Source
static VALUE
curses_bkgd(VALUE obj, VALUE ch)
{
#ifdef HAVE_BKGD
curses_stdscr();
return (bkgd(OBJ2CHTYPE(ch)) == OK) ? Qtrue : Qfalse;
#else
return Qfalse;
#endif
}
Window background manipulation routines.
Set the background property of the current and then apply the character Integer ch setting to every character position in that window.
see also the system manual for curs_bkgd(3)
Source
static VALUE
curses_bkgdset(VALUE obj, VALUE ch)
{
#ifdef HAVE_BKGDSET
curses_stdscr();
bkgdset(OBJ2CHTYPE(ch));
#endif
return Qnil;
}
Manipulate the background of the named window with character Integer ch
The background becomes a property of the character and moves with the character through any scrolling and insert/delete line/character operations.
see also the system manual for curs_bkgd(3)
Source
static VALUE
curses_can_change_color(VALUE obj)
{
curses_stdscr();
return can_change_color() ? Qtrue : Qfalse;
}
Returns true or false depending on whether the terminal can change color attributes
Source
static VALUE
curses_cbreak(VALUE obj)
{
curses_stdscr();
cbreak();
return Qnil;
}
Put the terminal into cbreak mode.
Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The Curses.cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program.
The Curses.nocbreak routine returns the terminal to normal (cooked) mode.
Initially the terminal may or may not be in cbreak mode, as the mode is inherited; therefore, a program should call Curses.cbreak or Curses.nocbreak explicitly. Most interactive programs using curses set the cbreak mode. Note that Curses.cbreak overrides Curses.raw.
see also Curses.raw
Source
static VALUE
curses_clear(VALUE obj)
{
curses_stdscr();
wclear(stdscr);
return Qnil;
}
Clears every position on the screen completely, so that a subsequent call by Curses.refresh for the screen/window will be repainted from scratch.
Source
static VALUE
curses_close_screen(VALUE self)
{
curses_stdscr();
#ifdef HAVE_ISENDWIN
if (!isendwin())
#endif
endwin();
rb_stdscr = 0;
return Qnil;
}
A program should always call Curses.close_screen before exiting or escaping from curses mode temporarily. This routine restores tty modes, moves the cursor to the lower left-hand corner of the screen and resets the terminal into the proper non-visual mode.
Calling Curses.refresh or Curses.doupdate after a temporary escape causes the program to resume visual mode.
Source
static VALUE
curses_closed(VALUE self)
{
curses_stdscr();
if (isendwin()) {
return Qtrue;
}
return Qfalse;
}
Returns true if the window/screen has been closed, without any subsequent Curses.refresh calls, returns false otherwise.
Source
static VALUE
curses_clrtoeol(VALUE self)
{
curses_stdscr();
clrtoeol();
return Qnil;
}
Clears to the end of line, that the cursor is currently on.
Source
static VALUE
curses_color_content(VALUE obj, VALUE color)
{
short r,g,b;
curses_stdscr();
color_content(NUM2INT(color),&r,&g,&b);
return rb_ary_new3(3,INT2FIX(r),INT2FIX(g),INT2FIX(b));
}
Returns an 3 item Array of the RGB values in color
Source
static VALUE
curses_color_pair(VALUE obj, VALUE attrs)
{
return CHTYPE2NUM(COLOR_PAIR(NUM2CHTYPE(attrs)));
}
Sets the color pair attributes to attrs.
This should be equivalent to Curses.attrset(COLOR_PAIR(attrs))
TODO: validate that equivalency
Source
static VALUE
curses_color_pairs(VALUE obj)
{
return INT2FIX(COLOR_PAIRS);
}
Returns the COLOR_PAIRS available, if the curses library supports it.
Source
static VALUE
curses_cols(VALUE self)
{
return INT2FIX(COLS);
}
Returns the number of columns on the screen
Source
static VALUE
curses_cbreak(VALUE obj)
{
curses_stdscr();
cbreak();
return Qnil;
}
Put the terminal into cbreak mode.
Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The Curses.cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program.
The Curses.nocbreak routine returns the terminal to normal (cooked) mode.
Initially the terminal may or may not be in cbreak mode, as the mode is inherited; therefore, a program should call Curses.cbreak or Curses.nocbreak explicitly. Most interactive programs using curses set the cbreak mode. Note that Curses.cbreak overrides Curses.raw.
see also Curses.raw
Source
static VALUE
curses_curs_set(VALUE obj, VALUE visibility)
{
#ifdef HAVE_CURS_SET
int n;
curses_stdscr();
return (n = curs_set(NUM2INT(visibility)) != ERR) ? INT2FIX(n) : Qnil;
#else
return Qnil;
#endif
}
Sets Cursor Visibility. 0: invisible 1: visible 2: very visible
Source
static VALUE
curses_def_prog_mode(VALUE obj)
{
curses_stdscr();
return def_prog_mode() == OK ? Qtrue : Qfalse;
}
Save the current terminal modes as the “program” state for use by the Curses.reset_prog_mode
This is done automatically by Curses.init_screen
Source
static VALUE
curses_delch(VALUE obj)
{
curses_stdscr();
delch();
return Qnil;
}
Delete the character under the cursor
Source
static VALUE
curses_deleteln(VALUE obj)
{
curses_stdscr();
#if defined(HAVE_DELETELN) || defined(deleteln)
deleteln();
#endif
return Qnil;
}
Delete the line under the cursor.
Source
static VALUE
curses_doupdate(VALUE obj)
{
curses_stdscr();
#ifdef HAVE_DOUPDATE
doupdate();
#else
refresh();
#endif
return Qnil;
}
Refreshes the windows and lines.
Curses.doupdate allows multiple updates with more efficiency than Curses.refresh alone.
Source
static VALUE
curses_echo(VALUE obj)
{
curses_stdscr();
echo();
return Qnil;
}
Enables characters typed by the user to be echoed by Curses.getch as they are typed.
Source
static VALUE
curses_erase(VALUE obj)
{
curses_stdscr();
werase(stdscr);
return Qnil;
}
Erase the screen.
Source
static VALUE
curses_flash(VALUE obj)
{
#ifdef HAVE_FLASH
curses_stdscr();
flash();
#endif
return Qnil;
}
Flashes the screen, for visual alarm on the terminal, if possible; otherwise it sounds the alert.
see also Curses.beep
Source
static VALUE
curses_flushinp(VALUE obj)
{
curses_stdscr();
flushinp();
return Qnil;
}
The flushinp routine throws away any typeahead that has been typed by the user and has not yet been read by the program.
Source
static VALUE
curses_get_char(VALUE obj)
{
#ifdef HAVE_GET_WCH
struct get_wch_arg arg;
curses_stdscr();
rb_thread_call_without_gvl(get_wch_func, &arg, RUBY_UBF_IO, 0);
switch (arg.retval) {
case OK:
return keyboard_uint_chr(arg.ch);
case KEY_CODE_YES:
return key_code_value(arg.ch);
}
return Qnil;
#else
int c;
curses_stdscr();
rb_thread_call_without_gvl(getch_func, &c, RUBY_UBF_IO, 0);
if (c > 0xff) {
return INT2NUM(c);
}
else if (c >= 0) {
return keyboard_uint_chr(c);
}
else {
return Qnil;
}
#endif
}
Read and returns a character or function key from the window. A single or multibyte character is represented by a String, and a function key is represented by an Integer. Returns nil if no input is ready.
See Curses::Key to all the function KEY_* available
Source
HAVE_PDC_GET_KEY_MODIFIERS
static VALUE
curses_get_key_modifiers(VALUE obj)
{
return ULONG2NUM(PDC_get_key_modifiers());
}
Source
static VALUE
curses_getch(VALUE obj)
{
int c;
curses_stdscr();
rb_thread_call_without_gvl(getch_func, &c, RUBY_UBF_IO, 0);
if (c == EOF) return Qnil;
if (rb_isprint(c)) {
char ch = (char)c;
return rb_external_str_new_with_enc(&ch, 1, keyboard_encoding);
}
return UINT2NUM(c);
}
Read and returns a character from the window.
See Curses::Key to all the function KEY_* available
Source
static VALUE
curses_getmouse(VALUE obj)
{
struct mousedata *mdata;
VALUE val;
curses_stdscr();
val = TypedData_Make_Struct(cMouseEvent,struct mousedata,
&mousedata_type,mdata);
mdata->mevent = (MEVENT*)xmalloc(sizeof(MEVENT));
return (getmouse(mdata->mevent) == OK) ? val : Qnil;
}
Returns coordinates of the mouse.
This will read and pop the mouse event data off the queue
See the BUTTON*, ALL_MOUSE_EVENTS and REPORT_MOUSE_POSITION constants, to examine the mask of the event
Source
static VALUE
curses_getstr(VALUE obj)
{
char rtn[GETSTR_BUF_SIZE];
curses_stdscr();
rb_thread_call_without_gvl(getstr_func, rtn, RUBY_UBF_IO, 0);
return rb_external_str_new_with_enc(rtn, strlen(rtn), keyboard_encoding);
}
This is equivalent to a series of Curses::Window.getch calls
Source
static VALUE
curses_has_colors(VALUE obj)
{
curses_stdscr();
return has_colors() ? Qtrue : Qfalse;
}
Returns true or false depending on whether the terminal has color capabilities.
Source
static VALUE
curses_inch(VALUE obj)
{
curses_stdscr();
return CHTYPE2NUM(inch());
}
Returns the character at the current position.
Source
static VALUE
curses_init_color(VALUE obj, VALUE color, VALUE r, VALUE g, VALUE b)
{
/* may have to raise exception on ERR */
curses_stdscr();
return (init_color(NUM2INT(color),NUM2INT(r),
NUM2INT(g),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
}
Changes the definition of a color. It takes four arguments:
-
the number of the color to be changed,
color -
the amount of red,
r -
the amount of green,
g -
the amount of blue,
b
The value of the first argument must be between 0 and COLORS. (See the section Colors for the default color index.) Each of the last three arguments must be a value between 0 and 1000. When Curses.init_color is used, all occurrences of that color on the screen immediately change to the new definition.
Source
static VALUE
curses_init_pair(VALUE obj, VALUE pair, VALUE f, VALUE b)
{
/* may have to raise exception on ERR */
curses_stdscr();
return (init_pair(NUM2INT(pair),NUM2INT(f),NUM2INT(b)) == OK) ? Qtrue : Qfalse;
}
Changes the definition of a color-pair.
It takes three arguments: the number of the color-pair to be changed pair, the foreground color number f, and the background color number b.
If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition.
Source
static VALUE
curses_init_screen(VALUE self)
{
if (rb_stdscr) return rb_stdscr;
initscr();
if (stdscr == 0) {
rb_raise(rb_eRuntimeError, "can't initialize curses");
}
rb_set_end_proc(curses_finalize, 0);
clear();
rb_stdscr = prep_window(cWindow, stdscr, 1);
return rb_stdscr;
}
Initialize a standard screen
see also Curses.stdscr
Source
static VALUE
curses_insch(VALUE obj, VALUE ch)
{
curses_stdscr();
insch(OBJ2CHTYPE(ch));
return Qnil;
}
Insert a character ch, before the cursor.
Source
static VALUE
curses_insertln(VALUE obj)
{
curses_stdscr();
#if defined(HAVE_INSERTLN) || defined(insertln)
insertln();
#endif
return Qnil;
}
Inserts a line above the cursor, and the bottom line is lost
Source
static VALUE
curses_get_keyboard_encoding(VALUE obj)
{
return rb_enc_from_encoding(keyboard_encoding);
}
Returns the encoding for keyboard input.
Source
static VALUE
curses_set_keyboard_encoding(VALUE obj, VALUE enc)
{
keyboard_encoding = rb_to_encoding(enc);
return enc;
}
Sets the encoding for keyboard input.
Source
static VALUE
curses_keyname(VALUE obj, VALUE c)
{
#ifdef HAVE_KEYNAME
int cc = curses_char(c);
const char *name;
curses_stdscr();
name = keyname(cc);
if (name) {
return rb_str_new_cstr(name);
}
else {
return Qnil;
}
#else
return Qnil;
#endif
}
Returns the character string corresponding to key c
Source
static VALUE
curses_lines(VALUE self)
{
return INT2FIX(LINES);
}
Returns the number of lines on the screen
Source
static VALUE
curses_mouseinterval(VALUE obj, VALUE interval)
{
curses_stdscr();
return mouseinterval(NUM2INT(interval)) ? Qtrue : Qfalse;
}
The Curses.mouseinterval function sets the maximum time (in thousands of a second) that can elapse between press and release events for them to be recognized as a click.
Use Curses.mouseinterval(0) to disable click resolution. This function returns the previous interval value.
Use Curses.mouseinterval(-1) to obtain the interval without altering it.
The default is one sixth of a second.
Source
static VALUE
curses_mousemask(VALUE obj, VALUE mask)
{
curses_stdscr();
return ULONG2NUM(mousemask(NUM2UINT(mask),NULL));
}
Returns the mask of the reportable events
Source
static VALUE
curses_nl(VALUE obj)
{
curses_stdscr();
nl();
return Qnil;
}
Enable the underlying display device to translate the return key into newline on input, and whether it translates newline into return and line-feed on output (in either case, the call Curses.addch(‘n’) does the equivalent of return and line feed on the virtual screen).
Initially, these translations do occur. If you disable them using Curses.nonl, curses will be able to make better use of the line-feed capability, resulting in faster cursor motion. Also, curses will then be able to detect the return key.
Source
static VALUE
curses_nocbreak(VALUE obj)
{
curses_stdscr();
nocbreak();
return Qnil;
}
Put the terminal into normal mode (out of cbreak mode).
See Curses.cbreak for more detail.
Source
static VALUE
curses_nocbreak(VALUE obj)
{
curses_stdscr();
nocbreak();
return Qnil;
}
Put the terminal into normal mode (out of cbreak mode).
See Curses.cbreak for more detail.
Source
static VALUE
curses_noecho(VALUE obj)
{
curses_stdscr();
noecho();
return Qnil;
}
Disables characters typed by the user to be echoed by Curses.getch as they are typed.
Source
static VALUE
curses_nonl(VALUE obj)
{
curses_stdscr();
nonl();
return Qnil;
}
Disable the underlying display device to translate the return key into newline on input
See Curses.nl for more detail
Source
static VALUE
curses_noraw(VALUE obj)
{
curses_stdscr();
noraw();
return Qnil;
}
Put the terminal out of raw mode.
see Curses.raw for more detail
Source
static VALUE
curses_pair_content(VALUE obj, VALUE pair)
{
short f,b;
curses_stdscr();
pair_content(NUM2INT(pair),&f,&b);
return rb_ary_new3(2,INT2FIX(f),INT2FIX(b));
}
Returns a 2 item Array, with the foreground and background color, in pair
Source
static VALUE
curses_pair_number(VALUE obj, VALUE attrs)
{
curses_stdscr();
return INT2FIX(PAIR_NUMBER(NUM2CHTYPE(attrs)));
}
Returns the Fixnum color pair number of attributes attrs.
Source
static VALUE
curses_raw(VALUE obj)
{
curses_stdscr();
raw();
return Qnil;
}
Put the terminal into raw mode.
Raw mode is similar to Curses.cbreak mode, in that characters typed are immediately passed through to the user program.
The differences are that in raw mode, the interrupt, quit, suspend, and flow control characters are all passed through uninterpreted, instead of generating a signal. The behavior of the BREAK key depends on other bits in the tty driver that are not set by curses.
Source
static VALUE
curses_refresh(VALUE obj)
{
curses_stdscr();
refresh();
return Qnil;
}
Refreshes the windows and lines.
Source
static VALUE
curses_reset_prog_mode(VALUE obj)
{
curses_stdscr();
return reset_prog_mode() == OK ? Qtrue : Qfalse;
}
Reset the current terminal modes to the saved state by the Curses.def_prog_mode
This is done automatically by Curses.close_screen
Source
static VALUE
curses_resizeterm(VALUE obj, VALUE lin, VALUE col)
{
#if defined(HAVE_RESIZETERM)
curses_stdscr();
return (resizeterm(NUM2INT(lin),NUM2INT(col)) == OK) ? Qtrue : Qfalse;
#else
return Qnil;
#endif
}
Resize the current term to Fixnum lines and Fixnum cols
Source
static VALUE
curses_resizeterm(VALUE obj, VALUE lin, VALUE col)
{
#if defined(HAVE_RESIZETERM)
curses_stdscr();
return (resizeterm(NUM2INT(lin),NUM2INT(col)) == OK) ? Qtrue : Qfalse;
#else
return Qnil;
#endif
}
Resize the current term to Fixnum lines and Fixnum cols
Source
static VALUE
curses_return_key_modifiers(VALUE obj, VALUE flag)
{
return INT2NUM(PDC_return_key_modifiers(RTEST(flag)));
}
Source
static VALUE
curses_save_key_modifiers(VALUE obj, VALUE flag)
{
return INT2NUM(PDC_save_key_modifiers(RTEST(flag)));
}
Source
static VALUE
curses_scrl(VALUE obj, VALUE n)
{
/* may have to raise exception on ERR */
#ifdef HAVE_SCRL
curses_stdscr();
return (scrl(NUM2INT(n)) == OK) ? Qtrue : Qfalse;
#else
return Qfalse;
#endif
}
Scrolls the current window Fixnum num lines. The current cursor position is not changed.
For positive num, it scrolls up.
For negative num, it scrolls down.
Source
static VALUE
curses_setpos(VALUE obj, VALUE y, VALUE x)
{
curses_stdscr();
move(NUM2INT(y), NUM2INT(x));
return Qnil;
}
A setter for the position of the cursor, using coordinates x and y
Source
static VALUE
curses_setscrreg(VALUE obj, VALUE top, VALUE bottom)
{
/* may have to raise exception on ERR */
#ifdef HAVE_SETSCRREG
curses_stdscr();
return (setscrreg(NUM2INT(top), NUM2INT(bottom)) == OK) ? Qtrue : Qfalse;
#else
return Qfalse;
#endif
}
Set a software scrolling region in a window. top and bottom are lines numbers of the margin.
If this option and Curses.scrollok are enabled, an attempt to move off the bottom margin line causes all lines in the scrolling region to scroll one line in the direction of the first line. Only the text of the window is scrolled.
Source
static VALUE
curses_standend(VALUE obj)
{
curses_stdscr();
standend();
return Qnil;
}
Enables the Normal display (no highlight)
This is equivalent to Curses.attron(A_NORMAL)
see also Curses::Window.attrset for additional information.
Source
static VALUE
curses_standout(VALUE obj)
{
curses_stdscr();
standout();
return Qnil;
}
Enables the best highlighting mode of the terminal.
This is equivalent to Curses:Window.attron(A_STANDOUT)
see also Curses::Window.attrset additional information
Source
static VALUE
curses_start_color(VALUE obj)
{
/* may have to raise exception on ERR */
curses_stdscr();
return (start_color() == OK) ? Qtrue : Qfalse;
}
Initializes the color attributes, for terminals that support it.
This must be called, in order to use color attributes. It is good practice to call it just after Curses.init_screen
Source
static VALUE
curses_init_screen(VALUE self)
{
if (rb_stdscr) return rb_stdscr;
initscr();
if (stdscr == 0) {
rb_raise(rb_eRuntimeError, "can't initialize curses");
}
rb_set_end_proc(curses_finalize, 0);
clear();
rb_stdscr = prep_window(cWindow, stdscr, 1);
return rb_stdscr;
}
Initialize a standard screen
see also Curses.stdscr
Source
static VALUE
curses_get_terminal_encoding(VALUE obj)
{
return rb_enc_from_encoding(terminal_encoding);
}
Returns the encoding for terminal output.
Source
static VALUE
curses_set_terminal_encoding(VALUE obj, VALUE enc)
{
terminal_encoding = rb_to_encoding(enc);
return enc;
}
Sets the encoding for terminal output.
Source
static VALUE
curses_timeout(VALUE obj, VALUE delay)
{
curses_stdscr();
timeout(NUM2INT(delay));
return Qnil;
}
Sets block and non-blocking reads for the window.
-
If delay is negative, blocking read is used (i.e., waits indefinitely for input).
-
If delay is zero, then non-blocking read is used (i.e., read returns ERR if no input is waiting).
-
If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input.
Source
static VALUE
curses_unget_char(VALUE obj, VALUE ch)
{
ID id_ord;
unsigned int c;
curses_stdscr();
if (FIXNUM_P(ch)) {
ungetch(NUM2UINT(ch));
}
else {
StringValue(ch);
CONST_ID(id_ord, "ord");
c = NUM2UINT(rb_funcall(ch, id_ord, 0));
#ifdef HAVE_UNGET_WCH
unget_wch(c);
#else
if (c > 0xff) {
rb_raise(rb_eRangeError, "Out of range: %u", c);
}
ungetch(c);
#endif
}
return Qnil;
}
Places ch back onto the input queue to be returned by the next call to Curses.get_char etc.
There is just one input queue for all windows.
Source
static VALUE
curses_ungetch(VALUE obj, VALUE ch)
{
int c = curses_char(ch);
curses_stdscr();
ungetch(c);
return Qnil;
}
Places ch back onto the input queue to be returned by the next call to Curses.getch.
There is just one input queue for all windows.
Source
static VALUE
curses_ungetmouse(VALUE obj, VALUE mevent)
{
struct mousedata *mdata;
curses_stdscr();
GetMOUSE(mevent,mdata);
return (ungetmouse(mdata->mevent) == OK) ? Qtrue : Qfalse;
}
It pushes a KEY_MOUSE event onto the input queue, and associates with that event the given state data and screen-relative character-cell coordinates.
The Curses.ungetmouse function behaves analogously to Curses.ungetch.
Source
static VALUE
curses_use_default_colors(VALUE obj)
{
curses_stdscr();
use_default_colors();
return Qnil;
}
tells the curses library to use terminal’s default colors.
see also the system manual for default_colors(3)