PHP: Ncurses terminal screen control functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<syslogncurses_addch>
view the version of this page
Last updated: Wed, 29 Jan 2003

LXVII. Ncurses terminal screen control functions

Introduction

ncurses (new curses) is a free software emulation of curses in System V Rel 4.0 (and above). It uses terminfo format, supports pads, colors, multiple highlights, form characters and function key mapping. Because of the interactive nature of this library, it will be of little use for writing Web applications, but may be useful when writing scripts meant using PHP from the command line.

주의

This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.

Ncurses is available for the following platforms:

  • AIX

  • BeOS

  • Cygwin

  • Digital Unix (aka OSF1)

  • FreeBSD

  • GNU/Linux

  • HPUX

  • IRIX

  • OS/2

  • SCO OpenServer

  • Solaris

  • SunOS

Requirements

You need the ncurses libraries and headerfiles. Download the latest version from the or from an other GNU-Mirror.

Installation

To get these functions to work, you have to compile the CGI or CLI version of PHP with --with-ncurses[=DIR].

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

표 1. Ncurses configuration options

NameDefaultChangeable
ncurses.value"42"PHP_INI_ALL
ncurses.string"foobar"PHP_INI_ALL
For further details and definition of the PHP_INI_* constants see ini_set().

Resource Types

This extension has no resource types defined.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

Error codes

On error ncurses functions return NCURSES_ERR.

Colors

표 2. ncurses color constants

constantmeaning
NCURSES_COLOR_BLACKno color (black)
NCURSES_COLOR_WHITEwhite
NCURSES_COLOR_REDred - supported when terminal is in color mode
NCURSES_COLOR_GREENgreen - supported when terminal is in color mod
NCURSES_COLOR_YELLOWyellow - supported when terminal is in color mod
NCURSES_COLOR_BLUEblue - supported when terminal is in color mod
NCURSES_COLOR_CYANcyan - supported when terminal is in color mod
NCURSES_COLOR_MAGENTAmagenta - supported when terminal is in color mod

Keys

표 3. ncurses key constants

constantmeaning
NCURSES_KEY_F0 - NCURSES_KEY_F64function keys F1 - F64
NCURSES_KEY_DOWNdown arrow
NCURSES_KEY_UPup arrow
NCURSES_KEY_LEFTleft arrow
NCURSES_KEY_RIGHTright arrow
NCURSES_KEY_HOMEhome key (upward+left arrow)
NCURSES_KEY_BACKSPACEbackspace
NCURSES_KEY_DLdelete line
NCURSES_KEY_ILinsert line
NCURSES_KEY_DCdelete character
NCURSES_KEY_ICinsert char or enter insert mode
NCURSES_KEY_EICexit insert char mode
NCURSES_KEY_CLEARclear screen
NCURSES_KEY_EOSclear to end of screen
NCURSES_KEY_EOLclear to end of line
NCURSES_KEY_SFscroll one line forward
NCURSES_KEY_SRscroll one line backward
NCURSES_KEY_NPAGEnext page
NCURSES_KEY_PPAGEprevious page
NCURSES_KEY_STABset tab
NCURSES_KEY_CTABclear tab
NCURSES_KEY_CATABclear all tabs
NCURSES_KEY_SRESETsoft (partial) reset
NCURSES_KEY_RESETreset or hard reset
NCURSES_KEY_PRINTprint
NCURSES_KEY_LLlower left
NCURSES_KEY_A1upper left of keypad
NCURSES_KEY_A3upper right of keypad
NCURSES_KEY_B2center of keypad
NCURSES_KEY_C1lower left of keypad
NCURSES_KEY_C3lower right of keypad
NCURSES_KEY_BTABback tab
NCURSES_KEY_BEGbeginning
NCURSES_KEY_CANCELcancel
NCURSES_KEY_CLOSEclose
NCURSES_KEY_COMMANDcmd (command)
NCURSES_KEY_COPYcopy
NCURSES_KEY_CREATEcreate
NCURSES_KEY_ENDend
NCURSES_KEY_EXITexit
NCURSES_KEY_FINDfind
NCURSES_KEY_HELPhelp
NCURSES_KEY_MARKmark
NCURSES_KEY_MESSAGEmessage
NCURSES_KEY_MOVEmove
NCURSES_KEY_NEXTnext
NCURSES_KEY_OPENopen
NCURSES_KEY_OPTIONSoptions
NCURSES_KEY_PREVIOUSprevious
NCURSES_KEY_REDOredo
NCURSES_KEY_REFERENCEref (reference)
NCURSES_KEY_REFRESHrefresh
NCURSES_KEY_REPLACEreplace
NCURSES_KEY_RESTARTrestart
NCURSES_KEY_RESUMEresume
NCURSES_KEY_SAVEsave
NCURSES_KEY_SBEGshiftet beg (beginning)
NCURSES_KEY_SCANCELshifted cancel
NCURSES_KEY_SCOMMANDshifted command
NCURSES_KEY_SCOPYshifted copy
NCURSES_KEY_SCREATEshifted create
NCURSES_KEY_SDCshifted delete char
NCURSES_KEY_SDLshifted delete line
NCURSES_KEY_SELECTselect
NCURSES_KEY_SENDshifted end
NCURSES_KEY_SEOLshifted end of line
NCURSES_KEY_SEXITshifted exit
NCURSES_KEY_SFINDshifted find
NCURSES_KEY_SHELPshifted help
NCURSES_KEY_SHOMEshifted home
NCURSES_KEY_SICshifted input
NCURSES_KEY_SLEFTshifted left arrow
NCURSES_KEY_SMESSAGEshifted message
NCURSES_KEY_SMOVEshifted move
NCURSES_KEY_SNEXTshifted next
NCURSES_KEY_SOPTIONSshifted options
NCURSES_KEY_SPREVIOUSshifted previous
NCURSES_KEY_SPRINTshifted print
NCURSES_KEY_SREDOshifted redo
NCURSES_KEY_SREPLACEshifted replace
NCURSES_KEY_SRIGHTshifted right arrow
NCURSES_KEY_SRSUMEshifted resume
NCURSES_KEY_SSAVEshifted save
NCURSES_KEY_SSUSPENDshifted suspend
NCURSES_KEY_UNDOundo
NCURSES_KEY_MOUSEmouse event has occured
NCURSES_KEY_MAXmaximum key value

Mouse

표 4. mouse constants

Constantmeaning
NCURSES_BUTTON1_RELEASED - NCURSES_BUTTON4_RELEASEDbutton (1-4) released
NCURSES_BUTTON1_PRESSED - NCURSES_BUTTON4_PRESSEDbutton (1-4) pressed
NCURSES_BUTTON1_CLICKED - NCURSES_BUTTON4_CLICKEDbutton (1-4) clicked
NCURSES_BUTTON1_DOUBLE_CLICKED - NCURSES_BUTTON4_DOUBLE_CLICKEDbutton (1-4) double clicked
NCURSES_BUTTON1_TRIPLE_CLICKED - NCURSES_BUTTON4_TRIPLE_CLICKEDbutton (1-4) triple clicked
NCURSES_BUTTON_CTRLctrl pressed during click
NCURSES_BUTTON_SHIFTshift pressed during click
NCURSES_BUTTON_ALTalt pressed during click
NCURSES_ALL_MOUSE_EVENTSreport all mouse events
NCURSES_REPORT_MOUSE_POSITIONreport mouse position
차례
ncurses_addch -- Add character at current position and advance cursor
ncurses_addchnstr -- Add attributed string with specified length at current position
ncurses_addchstr -- Add attributed string at current position
ncurses_addnstr -- Add string with specified length at current position
ncurses_addstr -- Output text at current position
ncurses_assume_default_colors -- Define default colors for color 0
ncurses_attroff -- Turn off the given attributes
ncurses_attron -- Turn on the given attributes
ncurses_attrset -- Set given attributes
ncurses_baudrate -- Returns baudrate of terminal
ncurses_beep -- Let the terminal beep
ncurses_bkgd -- Set background property for terminal screen
ncurses_bkgdset -- Control screen background
ncurses_border -- Draw a border around the screen using attributed characters
ncurses_can_change_color -- Check if we can change terminals colors
ncurses_cbreak -- Switch of input buffering
ncurses_clear -- Clear screen
ncurses_clrtobot -- Clear screen from current position to bottom
ncurses_clrtoeol -- Clear screen from current position to end of line
ncurses_color_set -- Set fore- and background color
ncurses_curs_set -- Set cursor state
ncurses_def_prog_mode -- Saves terminals (program) mode
ncurses_def_shell_mode -- Saves terminals (shell) mode
ncurses_define_key -- Define a keycode
ncurses_delay_output -- Delay output on terminal using padding characters
ncurses_delch -- Delete character at current position, move rest of line left
ncurses_deleteln -- Delete line at current position, move rest of screen up
ncurses_delwin -- Delete a ncurses window
ncurses_doupdate -- Write all prepared refreshes to terminal
ncurses_echo -- Activate keyboard input echo
ncurses_echochar -- Single character output including refresh
ncurses_end -- Stop using ncurses, clean up the screen
ncurses_erase -- Erase terminal screen
ncurses_erasechar -- Returns current erase character
ncurses_filter -- 
ncurses_flash -- Flash terminal screen (visual bell)
ncurses_flushinp -- Flush keyboard input buffer
ncurses_getch -- Read a character from keyboard
ncurses_getmouse -- Reads mouse event
ncurses_halfdelay -- Put terminal into halfdelay mode
ncurses_has_colors -- Check if terminal has colors
ncurses_has_ic -- Check for insert- and delete-capabilities
ncurses_has_il -- Check for line insert- and delete-capabilities
ncurses_has_key -- Check for presence of a function key on terminal keyboard
ncurses_hline -- Draw a horizontal line at current position using an attributed character and max. n characters long
ncurses_inch -- Get character and attribute at current position
ncurses_init_color -- Set new RGB value for color
ncurses_init_pair -- Allocate a color pair
ncurses_init -- Initialize ncurses
ncurses_insch -- Insert character moving rest of line including character at current position
ncurses_insdelln -- Insert lines before current line scrolling down (negative numbers delete and scroll up)
ncurses_insertln -- Insert a line, move rest of screen down
ncurses_insstr -- Insert string at current position, moving rest of line right
ncurses_instr -- Reads string from terminal screen
ncurses_isendwin -- Ncurses is in endwin mode, normal screen output may be performed
ncurses_keyok -- Enable or disable a keycode
ncurses_killchar -- Returns current line kill character
ncurses_longname -- Returns terminals description
ncurses_mouseinterval -- Set timeout for mouse button clicks
ncurses_mousemask -- Sets mouse options
ncurses_move -- Move output position
ncurses_mvaddch -- Move current position and add character
ncurses_mvaddchnstr -- Move position and add attrributed string with specified length
ncurses_mvaddchstr -- Move position and add attributed string
ncurses_mvaddnstr -- Move position and add string with specified length
ncurses_mvaddstr -- Move position and add string
ncurses_mvcur -- Move cursor immediately
ncurses_mvdelch -- Move position and delete character, shift rest of line left
ncurses_mvgetch -- Move position and get character at new position
ncurses_mvhline -- Set new position and draw a horizontal line using an attributed character and max. n characters long
ncurses_mvinch -- Move position and get attributed character at new position
ncurses_mvvline -- Set new position and draw a vertical line using an attributed character and max. n characters long
ncurses_mvwaddstr -- Add string at new position in window
ncurses_napms -- Sleep
ncurses_newwin -- Create a new window
ncurses_nl -- Translate newline and carriage return / line feed
ncurses_nocbreak -- Switch terminal to cooked mode
ncurses_noecho -- Switch off keyboard input echo
ncurses_nonl -- Do not translate newline and carriage return / line feed
ncurses_noqiflush -- Do not flush on signal characters
ncurses_noraw -- Switch terminal out of raw mode
ncurses_putp -- 
ncurses_qiflush -- Flush on signal characters
ncurses_raw -- Switch terminal into raw mode
ncurses_refresh -- Refresh screen
ncurses_resetty -- Restores saved terminal state
ncurses_savetty -- Saves terminal state
ncurses_scr_dump -- Dump screen content to file
ncurses_scr_init -- Initialize screen from file dump
ncurses_scr_restore -- Restore screen from file dump
ncurses_scr_set -- Inherit screen from file dump
ncurses_scrl -- Scroll window content up or down without changing current position
ncurses_slk_attr -- Returns current soft label key attribute
ncurses_slk_attroff -- 
ncurses_slk_attron -- 
ncurses_slk_attrset -- 
ncurses_slk_clear -- Clears soft labels from screen
ncurses_slk_color -- Sets color for soft label keys
ncurses_slk_init -- Initializes soft label key functions
ncurses_slk_noutrefresh -- Copies soft label keys to virtual screen
ncurses_slk_refresh -- Copies soft label keys to screen
ncurses_slk_restore -- Restores soft label keys
ncurses_slk_touch -- Fources output when ncurses_slk_noutrefresh is performed
ncurses_standend -- Stop using 'standout' attribute
ncurses_standout -- Start using 'standout' attribute
ncurses_start_color -- Start using colors
ncurses_termattrs -- Returns a logical OR of all attribute flags supported by terminal
ncurses_termname -- Returns terminals (short)-name
ncurses_timeout -- Set timeout for special key sequences
ncurses_typeahead -- Specify different filedescriptor for typeahead checking
ncurses_ungetch -- Put a character back into the input stream
ncurses_ungetmouse -- Pushes mouse event to queue
ncurses_use_default_colors -- Assign terminal default colors to color id -1
ncurses_use_env -- Control use of environment information about terminal size
ncurses_use_extended_names -- Control use of extended names in terminfo descriptions
ncurses_vidattr -- 
ncurses_vline -- Draw a vertical line at current position using an attributed character and max. n characters long
ncurses_wrefresh -- Refresh window on terminal screen


User Contributed Notes
Ncurses terminal screen control functions
add a note add a note
zak at php dot net
30-May-2002 11:34

There is a simple ncurses application in the PHP source tree. See
john at weider dot cc
18-Oct-2002 05:28

In addition to the above constants the following are also defined:

 A_NORMAL
   A_STANDOUT
   A_UNDERLINE
   A_REVERSE
  A_BLINK
   A_DIM
   A_BOLD
   A_PROTECT
   A_INVIS
  A_ALTCHARSET
   A_CHARTEXT

nweeks at utas dot edu dot au
07-Nov-2002 11:43

With your CGI version of PHP compiled with ncurses support, console apps are amazingly easy!

For example:
whack the following into a file, chmod +x it, and run it.

#!/usr/local/bin/php -q
&lt;?php
ncurses_init();
ncurses_border(25,25,18,18,24,24,23,23); // Do a lovely border
ncurses_move(1,1); // Start top left(inside border)
ncurses_addstr("Welcome to NCurses");
ncurses_addstr(" with PHP!");
ncurses_refresh(); // Send buffer to screen
ncurses_end(); // Clean up, and quit
?&gt;

You'll notice the second addstr simply tacks onto the first.

joeldegan AT yahoo.com
14-Dec-2002 05:56

actually *that* example does not work...
here is one that I took and trimmed down from the ncurses examples.
I will do some more and post them here..

<?
ncurses_init();
##############################################
ncurses_noecho();

  $large = ncurses_newwin(20, 60, 2, 10);
   $small = ncurses_newwin(10, 30, 7, 25);
   
   ncurses_refresh();
  ncurses_wrefresh($large);
   ncurses_wrefresh($small);

  ncurses_mvwaddstr($small, 5, 5, "   Test  String   ");
  ncurses_wrefresh($small);
  ncurses_getch();

##############################################
ncurses_end(); // Clean up, and quit
?>

This is from PHP 4.3.0RC0 compiled with the following flags..
./configure --prefix=/wwwroot/php --with-config-file-path=/wwwroot/php --with-mysql --enable-pcntl --with-tsrm-pthreads --enable-sysvsem --enable-sysvshm --with-curl --enable-bcmath --enable-sigchild --enable-sockets --with-ncurses

joeldegan AT yahoo.com
14-Dec-2002 09:37

This is from the examples that come with the latest release.

From:
php-4.3.0RC3/ext/ncurses/example1.php

I found this useful...

<?php
$n=0;
ncurses_init();
if(ncurses_has_colors()){
ncurses_start_color();
      ncurses_init_pair(1,NCURSES_COLOR_RED,NCURSES_COLOR_BLACK);
      ncurses_init_pair(2,NCURSES_COLOR_GREEN,NCURSES_COLOR_BLACK);
      ncurses_init_pair(3,NCURSES_COLOR_YELLOW,NCURSES_COLOR_BLACK);
      ncurses_init_pair(4,NCURSES_COLOR_BLUE,NCURSES_COLOR_BLACK);
      ncurses_init_pair(5,NCURSES_COLOR_MAGENTA,NCURSES_COLOR_BLACK);
      ncurses_init_pair(6,NCURSES_COLOR_CYAN,NCURSES_COLOR_BLACK);
      ncurses_init_pair(7,NCURSES_COLOR_WHITE,NCURSES_COLOR_BLACK);
}
while(1){
for ($x=0; $x<80; $x++) {
for ($y=0; $y<24; $y++) {
 $n++;
ncurses_move($y,$x);
 ncurses_addch($n+64);
ncurses_color_set($n%8);
 ncurses_refresh();
if($n>26)$n=0;
}
}
ncurses_getch();
}
?>

joeldegan AT yahoo.com
14-Dec-2002 10:10

I noticed a lack of a getxy() function so I wrote one.
You may need to change the path for your resize cmd.

<?
function getxy(){
$rez = `/usr/X11R6/bin/resize`;
$rez = explode("\n",$rez);
while(list($key,$val)=each($rez)){
$a=explode("=",$val);
if(trim($a[0])=="COLUMNS"){ $COLUMNS = $a[1]; }
if(trim($a[0])=="LINES"){ $LINES = $a[1]; }
}//
$retval[0]=$COLUMNS;
$retval[1]=$LINES;
  return $retval;
}

print_r(getxy());
?>

joeldegan AT yahoo.com
15-Dec-2002 07:26

See the documentation for ncurses_border and ncurses_wrefresh for some more examples of doing windowing and dynamic sizing.. I also posted some information to the zend.com code-gallery for doing ncurses under php.
25-Mar-2003 02:18
In the example above, if you run resize from a C shell it will output the sizes in C shell syntax, run resize with -u to force Bourne syntax:
The $win parameter is just for future compatibility.

function ncurses_getmaxyx($win, &$y, &$x)
{
      exec("/usr/X11R6/bin/resize -u", $output);
       $cols = explode("=", $output[0]);
       $rows = explode("=", $output[1]);
       $x = intval($cols[1]);
     $y = intval($rows[1]);
}

Habib Valanejad
28-Mar-2003 01:13

For those of you who want to check if <ENTER> key is passed,
you have to check the key agains both NL and CR keys:

function get_str()
{
  for ($str = "";;){
     $key = ncurses_getch();
     switch ($key){
       case 10:   // newline
       case 13:   // Carrige Return
          return($str);
       default:
          $str .= chr($key);
          ncurses_refresh();
     } // switch
  } // for
} // get_str()

Hope that would help.

add a note add a note

<syslogncurses_addch>
 Last updated: Wed, 29 Jan 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Fri May 23 21:10:19 2003 CEST