Last active
December 18, 2015 22:49
-
-
Save Azenet/5857488 to your computer and use it in GitHub Desktop.
Interface à Ncurses en PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Ncurses | |
* | |
* Singleton pour créer des interfaces avec Ncurses. | |
*/ | |
class Ncurses { | |
private static $_instance; | |
private $screen; | |
private $windows = array(); | |
private $debug = array(); | |
/** | |
* Termine Ncurses. | |
*/ | |
public function end() { | |
foreach($this->windows as $wn => $w) { | |
$this->closeWindow($wn); | |
} | |
$this->redraw(); | |
ncurses_end(); | |
} | |
private function __clone() {} | |
private function __construct() {} | |
/** | |
* Retourne le singleton. | |
* @return Ncurses | |
*/ | |
public static function getInstance() | |
{ | |
if (!(self::$_instance instanceof self)) | |
self::$_instance = new self(); | |
return self::$_instance; | |
} | |
/** | |
* Ajoute du debug en bas à droite. | |
* @param string $buffer Texte à ajouter | |
*/ | |
public static function addDebug($buffer) { | |
$ncurses = Ncurses::getInstance(); | |
ncurses_getmaxyx($ncurses->getScreen(), $my, $mx); | |
$ncurses->newWindow("Debug", 12, 90, $my - 15, $mx - 93); | |
$ncurses->debug[] = $buffer; | |
$ncurses->clearWindow("Debug"); | |
$i = 2; | |
foreach(array_slice($ncurses->debug, -8, 8) as $debugline) { | |
if (!empty($debugline)) ncurses_mvwaddstr($ncurses->getWindow("Debug"), $i, 3, $debugline); | |
$i++; | |
} | |
$ncurses->redraw(); | |
} | |
/** | |
* Retourne l'écran principal | |
* @return resource | |
*/ | |
public function getScreen() { | |
return $this->screen; | |
} | |
/** | |
* Initialise Ncurses | |
* @param string $title Titre de la fenêtre | |
*/ | |
public function init($title = null) { | |
ncurses_init(); | |
$this->screen = ncurses_newwin(0, 0, 0, 0); | |
ncurses_border(0, 0, 0, 0, 0, 0, 0, 0); | |
if ($title != null) ncurses_mvaddstr(0, 1, " ".$title." "); | |
$this->redraw(); | |
} | |
/** | |
* Redessine les fenêtres | |
*/ | |
public function redraw() { | |
foreach($this->windows as $window) { | |
ncurses_wrefresh($window); | |
} | |
ncurses_refresh(0); | |
} | |
/** | |
* Créé une fenêtre | |
* @param string $title Titre de la fenêtre | |
* @param int $rows Lignes | |
* @param int $cols Colonnes | |
* @param int $y Position verticale | |
* @param int $x Position horizontale | |
* @return bool | |
*/ | |
public function newWindow($title, $rows, $cols, $y, $x) { | |
if (isset($this->windows[$title])) return false; | |
$this->windows[$title] = ncurses_newwin($rows, $cols, $y, $x); | |
ncurses_wborder($this->windows[$title], 0, 0, 0, 0, 0, 0, 0, 0); | |
ncurses_mvwaddstr($this->windows[$title], 0, 1, " ".$title." "); | |
$this->redraw(); | |
return true; | |
} | |
/** | |
* Vide une fenêtre (buggé ?) | |
* @param string $title Titre de la fenêtre | |
* @return bool | |
*/ | |
public function clearWindow($title) { | |
if (isset($this->windows[$title])) return false; | |
ncurses_wrefresh($this->windows[$title]); | |
ncurses_wclear($this->windows[$title]); | |
ncurses_werase($this->windows[$title]); | |
ncurses_wrefresh($this->windows[$title]); | |
ncurses_wnoutrefresh($this->windows[$title]); | |
return true; | |
} | |
/** | |
* Retourne une fenêtre | |
* @param string $title Titre de la fenêtre | |
* @return resource | |
*/ | |
public function getWindow($title) { | |
return $this->windows[$title]; | |
} | |
/** | |
* Écrit vers une fenêtre | |
* @param string $title Titre de la fenêtre | |
* @param string $y Position verticale | |
* @param string $x Position horizontale | |
* @param int $attr Attribut Ncurses | |
* @param string $text Texte | |
* @return bool | |
*/ | |
public function writeToWindow($title, $y, $x, $attr = NCURSES_A_NORMAL, $text) { | |
if (!isset($this->windows[$title])) return false; | |
ncurses_attron($attr); | |
ncurses_mvwaddstr($this->windows[$title], $y, $x, $text); | |
ncurses_attroff($attr); | |
$this->redraw(); | |
return true; | |
} | |
/** | |
* Ferme une fenêtre | |
* @param string $title Titre de la fenêtre | |
* @return bool | |
*/ | |
public function closeWindow($title) { | |
if (!isset($this->windows[$title])) return false; | |
ncurses_werase($this->windows[$title]); | |
ncurses_wclear($this->windows[$title]); | |
ncurses_wnoutrefresh($this->windows[$title]); | |
$this->redraw(); | |
ncurses_delwin($this->windows[$title]); | |
unset($this->windows[$title]); | |
$this->redraw(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xD toi sur github