2008-02-28 16:39:33 +00:00
|
|
|
<?php
|
2008-02-29 15:39:44 +00:00
|
|
|
/**
|
|
|
|
* PHP ncurses library
|
|
|
|
*
|
|
|
|
* Provides a clean, object-oriented interface to the PHP ncurses functions to ease
|
|
|
|
* the creation of elaborate, functional CLI applications.
|
|
|
|
*
|
|
|
|
* @author Correl J. Roush <correl@gmail.com>
|
|
|
|
* @copyright Copyright (c) 2008, Correl J. Roush
|
|
|
|
* @version 0.1
|
|
|
|
*
|
|
|
|
* @package Nc
|
|
|
|
*/
|
|
|
|
|
2008-04-24 18:22:08 +00:00
|
|
|
require_once('signals.php');
|
|
|
|
include_once('ncurses.window.php');
|
|
|
|
include_once('ncurses.button.php');
|
|
|
|
include_once('ncurses.progressbar.php');
|
|
|
|
include_once('ncurses.textinput.php');
|
|
|
|
include_once('ncurses.tableview.php');
|
|
|
|
|
2008-02-29 15:39:44 +00:00
|
|
|
/**
|
|
|
|
* PHP ncurses application
|
|
|
|
*
|
|
|
|
* @package Nc
|
|
|
|
*/
|
2008-04-24 18:22:08 +00:00
|
|
|
abstract class NcApp extends Signaler {
|
|
|
|
protected $nc_screen;
|
|
|
|
protected $screen;
|
|
|
|
protected $log = array();
|
|
|
|
protected $done = false;
|
2008-02-28 16:39:33 +00:00
|
|
|
|
2008-02-29 15:39:44 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* Initializes the ncurses environment, as well as the fullscreen window {@link $screen}
|
|
|
|
*
|
|
|
|
* @param boolean $echo Echo keyboard input to the screen
|
|
|
|
* @param boolean $cursor Display the cursor on the screen
|
|
|
|
*/
|
|
|
|
public function __construct( $echo = false, $cursor = false ) {
|
2008-02-28 16:39:33 +00:00
|
|
|
ncurses_init();
|
|
|
|
$echo ? ncurses_echo() : ncurses_noecho();
|
|
|
|
ncurses_curs_set( (bool)$cursor );
|
2008-04-24 18:22:08 +00:00
|
|
|
$this->nc_screen = ncurses_newwin( 0, 0, 0, 0 );
|
2008-02-28 16:39:33 +00:00
|
|
|
ncurses_refresh();
|
2008-04-24 18:22:08 +00:00
|
|
|
$this->log($this, 'nCurses Initialized');
|
|
|
|
$this->screen = new NcWindow($this, 0, 0, 0, 0);
|
|
|
|
$this->init();
|
2008-02-28 16:39:33 +00:00
|
|
|
}
|
2008-02-29 15:39:44 +00:00
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*
|
|
|
|
* Clears the screen and ends the ncurses session
|
|
|
|
*/
|
|
|
|
public function __destruct() {
|
2008-02-28 16:39:33 +00:00
|
|
|
ncurses_clear();
|
|
|
|
ncurses_refresh();
|
|
|
|
ncurses_end();
|
|
|
|
}
|
2008-04-24 18:22:08 +00:00
|
|
|
public function log(&$object, $message) {
|
|
|
|
if( DEBUG ) {
|
|
|
|
$this->log[] = array(
|
|
|
|
'class' => get_class($object),
|
|
|
|
'message' => $message
|
|
|
|
);
|
|
|
|
$this->emit('log', $this->log);
|
2008-02-28 16:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-24 18:22:08 +00:00
|
|
|
public function exec() {
|
|
|
|
if( $this->screen instanceof NcWindow ) {
|
|
|
|
while(!$this->done) {
|
|
|
|
$key = ncurses_getch();
|
|
|
|
$this->screen->on_key_press($key);
|
2008-02-28 16:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 18:22:08 +00:00
|
|
|
public function quit() {
|
|
|
|
$this->done = true;
|
2008-02-28 16:39:33 +00:00
|
|
|
}
|
2008-02-29 15:39:44 +00:00
|
|
|
/**
|
2008-04-24 18:22:08 +00:00
|
|
|
* This is a placeholder function for application code
|
2008-02-29 15:39:44 +00:00
|
|
|
*/
|
2008-04-24 18:22:08 +00:00
|
|
|
abstract function init();
|
2008-02-28 16:39:33 +00:00
|
|
|
}
|
|
|
|
?>
|