Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 22 changed files with 899 additions and 174 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "2.5-dev"
"dev-master": "2.1-dev",
"dev-develop": "2.2-dev"
}
},
"autoload-dev": {
Expand Down
49 changes: 5 additions & 44 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Zend\Console\Charset;
use Zend\Console\Exception;

/**
* Common console adapter codebase
*/
abstract class AbstractAdapter implements AdapterInterface
{
/**
Expand Down Expand Up @@ -85,7 +88,7 @@ public function writeLine($text = "", $color = null, $bgColor = null)
$text = trim($text, "\r\n");

// Replace newline characters with spaces
$test = str_replace("\n", " ", $text);
$text = str_replace("\n", " ", $text);

// Trim the line if it's too long and output text
$consoleWidth = $this->getWidth();
Expand Down Expand Up @@ -356,32 +359,6 @@ public function isUtf8()
return true;
}

/**
* Return current cursor position - array($x, $y)
*
*
* @return array array($x, $y);
*/
public function getPos()
{
}

// /**
// * Return current cursor X coordinate (column)
// *
// *
// * @return false|int Integer or false if failed to determine.
// */
// public function getX();
//
// /**
// * Return current cursor Y coordinate (row)
// *
// *
// * @return false|int Integer or false if failed to determine.
// */
// public function getY();

/**
* Set cursor position
*
Expand Down Expand Up @@ -416,22 +393,6 @@ public function getTitle()
return '';
}

/**
* Set console window title
*
* @param $title
*/
public function setTitle($title)
{
}

/**
* Reset console window title to previous value.
*/
public function resetTitle()
{
}

/**
* Prepare a string that will be rendered in color.
*
Expand Down Expand Up @@ -601,7 +562,7 @@ public function readChar($mask = null)
$f = fopen('php://stdin','r');
do {
$char = fread($f,1);
} while ($mask === null || stristr($mask, $char));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
fclose($f);
return $char;
}
Expand Down
35 changes: 0 additions & 35 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,6 @@ public function getSize();
*/
public function isUtf8();


// /**
// * Return current cursor position - array($x, $y)
// *
// * @return array array($x, $y);
// */
// public function getPos();
//
// /**
// * Return current cursor X coordinate (column)
// *
// * @return false|int Integer or false if failed to determine.
// */
// public function getX();
//
// /**
// * Return current cursor Y coordinate (row)
// *
// * @return false|int Integer or false if failed to determine.
// */
// public function getY();

/**
* Set cursor position
*
Expand All @@ -190,19 +168,6 @@ public function showCursor();
*/
public function getTitle();

/**
* Set console window title
*
* @param $title
*/
public function setTitle($title);

/**
* Reset console window title to previous value.
*/
public function resetTitle();


/**
* Prepare a string that will be rendered in color.
*
Expand Down
11 changes: 1 addition & 10 deletions src/Adapter/Posix.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,6 @@ public function resetColor()
echo "\x1b[24;39m"; // reset fg underline
}

/**
* Return current console window title.
*
* @return string
*/
public function getTitle()
{
}

/**
* Set Console charset to use.
*
Expand Down Expand Up @@ -315,7 +306,7 @@ public function readChar($mask = null)
$stream = fopen('php://stdin', 'rb');
do {
$char = fgetc($stream);
} while (strlen($char) !== 1 || ($mask !== null && stristr($mask, $char) === false));
} while (strlen($char) !== 1 || ($mask !== null && false === strstr($mask, $char)));
fclose($stream);

$this->restoreTTYMode();
Expand Down
10 changes: 0 additions & 10 deletions src/Adapter/Virtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ public function isUtf8()
return false;
}

/**
* Set cursor position
*
* @param int $x
* @param int $y
*/
public function setPos($x, $y)
{
}

/**
* Return current console window title.
*
Expand Down
11 changes: 1 addition & 10 deletions src/Adapter/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,6 @@ public function isUtf8()
return false;
}

/**
* Set cursor position
* @param int $x
* @param int $y
*/
public function setPos($x, $y)
{
}

/**
* Return current console window title.
*
Expand Down Expand Up @@ -257,7 +248,7 @@ public function readChar($mask = null)

// Fetch the char from mask
$char = substr($mask, $return - 1, 1);
} while (!$char || ($mask !== null && !stristr($mask, $char)));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));

return $char;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/WindowsAnsicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function readChar($mask = null)

// Fetch the char from mask
$char = substr($mask, $return - 1, 1);
} while (!$char || ($mask !== null && !stristr($mask, $char)));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));

return $char;
}
Expand Down
17 changes: 14 additions & 3 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public static function getInstance($forceAdapter = null, $forceCharset = null)
return static::$instance;
}

/**
* Reset the console instance
*/
public static function resetInstance()
{
static::$instance = null;
}

/**
* Check if currently running under MS Windows
*
Expand Down Expand Up @@ -135,10 +143,10 @@ public static function isAnsicon()
*/
public static function isConsole()
{
if (null !== static::$isConsole && is_bool(static::$isConsole)) {
return static::$isConsole;
if (null === static::$isConsole) {
static::$isConsole = (PHP_SAPI == 'cli');
}
return PHP_SAPI == 'cli';
return static::$isConsole;
}

/**
Expand All @@ -148,6 +156,9 @@ public static function isConsole()
*/
public static function overrideIsConsole($flag)
{
if(null != $flag) {
$flag = (bool)$flag;
}
static::$isConsole = $flag;
}

Expand Down
79 changes: 27 additions & 52 deletions src/Prompt/Char.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,33 @@ class Char extends AbstractPrompt
/**
* Ask the user for a single key stroke
*
* @param string $promptText The prompt text to display in console
* @param string $allowedChars A list of allowed chars (i.e. "abc12345")
* @param bool $ignoreCase If true, case will be ignored and prompt will always return lower-cased response
* @param bool $allowEmpty Is empty response allowed?
* @param bool $echo Display the selection after user presses key
* @param string $promptText The prompt text to display in console
* @param string $allowedChars A list of allowed chars (i.e. "abc12345")
* @param bool $ignoreCase If true, case will be ignored and prompt will always return lower-cased response
* @param bool $allowEmpty Is empty response allowed?
* @param bool $echo Display the selection after user presses key
*/
public function __construct(
$promptText = 'Please hit a key',
$allowedChars = 'abc',
$allowedChars = '0123456789abcdefghijklmnopqrstuvwxyz',
$ignoreCase = true,
$allowEmpty = false,
$echo = true
) {

if ($promptText !== null) {
$this->setPromptText($promptText);
}

if ($allowEmpty !== null) {
$this->setAllowEmpty($allowEmpty);
}
$this->setPromptText($promptText);
$this->setAllowEmpty($allowEmpty);
$this->setIgnoreCase($ignoreCase);

if ($ignoreCase !== null) {
$this->setIgnoreCase($ignoreCase);
}

if ($allowedChars !== null) {
if (null != $allowedChars) {
if ($this->ignoreCase) {
$this->setAllowedChars(strtolower($allowedChars));
} else {
$this->setAllowedChars($allowedChars);
}
}

if ($echo !== null) {
$this->setEcho($echo);
}
$this->setEcho($echo);
}

/**
Expand All @@ -99,43 +89,28 @@ public function show()
$mask = implode("", $mask); // convert back to string
}

do {
/**
* Read char from console
*/
$char = $this->getConsole()->readChar($mask);

/**
* Lowercase the response if case is irrelevant
*/
if ($this->ignoreCase) {
$char = strtolower($char);
}
/**
* Read char from console
*/
$char = $this->getConsole()->readChar($mask);

/**
* Check if it is an allowed char
*/
if (stristr($this->allowedChars, $char) !== false) {
if ($this->echo) {
echo trim($char)."\n";
} else {
if ($this->promptText) {
echo "\n"; // skip to next line but only if we had any prompt text
}
}
break;
if ($this->echo) {
echo trim($char)."\n";
} else {
if ($this->promptText) {
echo "\n"; // skip to next line but only if we had any prompt text
}
} while (true);
}

return $this->lastResponse = $char;
}

/**
* @param bool $allowEmpty
* @param bool $allowEmpty
*/
public function setAllowEmpty($allowEmpty)
{
$this->allowEmpty = $allowEmpty;
$this->allowEmpty = (bool) $allowEmpty;
}

/**
Expand Down Expand Up @@ -179,11 +154,11 @@ public function getAllowedChars()
}

/**
* @param bool $ignoreCase
* @param bool $ignoreCase
*/
public function setIgnoreCase($ignoreCase)
{
$this->ignoreCase = $ignoreCase;
$this->ignoreCase = (bool) $ignoreCase;
}

/**
Expand All @@ -195,11 +170,11 @@ public function getIgnoreCase()
}

/**
* @param bool $echo
* @param bool $echo
*/
public function setEcho($echo)
{
$this->echo = $echo;
$this->echo = (bool) $echo;
}

/**
Expand Down
Loading

0 comments on commit 5abf474

Please sign in to comment.