Skip to content

Commit

Permalink
Merge pull request #1594 from mreishus/feature-readline-support
Browse files Browse the repository at this point in the history
Use readline in console when supported
  • Loading branch information
lorenzo committed Sep 1, 2013
2 parents 64cf405 + bb98ac7 commit 5f4feb0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/Cake/Console/ConsoleInput.php
Expand Up @@ -32,12 +32,24 @@ class ConsoleInput {
*/
protected $_input;

/**
* Can this instance use readline?
* Two conditions must be met:
* 1. Readline support must be enabled.
* 2. Handle we are attached to must be stdin.
* Allows rich editing with arrow keys and history when inputting a string.
*
* @var bool
*/
protected $_canReadline;

/**
* Constructor
*
* @param string $handle The location of the stream to use as input.
*/
public function __construct($handle = 'php://stdin') {
$this->_canReadline = extension_loaded('readline') && $handle == 'php://stdin' ? true : false;
$this->_input = fopen($handle, 'r');
}

Expand All @@ -47,6 +59,13 @@ public function __construct($handle = 'php://stdin') {
* @return mixed The value of the stream
*/
public function read() {
if ($this->_canReadline) {
$line = readline('');
if (!empty($line)) {
readline_add_history($line);
}
return $line;
}
return fgets($this->_input);
}

Expand Down

0 comments on commit 5f4feb0

Please sign in to comment.