scanner/ncurses/ncurses.php

87 lines
1.9 KiB
PHP
Raw Normal View History

<?php
/**
* 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
*/
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');
/**
* PHP ncurses application
*
* @package Nc
*/
abstract class NcApp extends Signaler {
protected $nc_screen;
protected $screen;
protected $log = array();
protected $done = false;
/**
* 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 ) {
ncurses_init();
$echo ? ncurses_echo() : ncurses_noecho();
ncurses_curs_set( (bool)$cursor );
$this->nc_screen = ncurses_newwin( 0, 0, 0, 0 );
ncurses_refresh();
$this->log($this, 'nCurses Initialized');
$this->screen = new NcWindow($this, 0, 0, 0, 0);
$this->init();
}
/**
* Destructor
*
* Clears the screen and ends the ncurses session
*/
public function __destruct() {
ncurses_clear();
ncurses_refresh();
ncurses_end();
}
public function log(&$object, $message) {
if( DEBUG ) {
$this->log[] = array(
'class' => get_class($object),
'message' => $message
);
$this->emit('log', $this->log);
}
}
public function exec() {
if( $this->screen instanceof NcWindow ) {
while(!$this->done) {
$key = ncurses_getch();
$this->screen->on_key_press($key);
}
}
}
public function quit() {
$this->done = true;
}
/**
* This is a placeholder function for application code
*/
abstract function init();
}
?>