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 974 additions and 245 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
122 changes: 7 additions & 115 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

use Zend\Console\Charset;
use Zend\Console\Exception;
use Zend\Stdlib\StringUtils;

/**
* Common console adapter codebase
*/
abstract class AbstractAdapter implements AdapterInterface
{
/**
Expand Down Expand Up @@ -70,34 +74,14 @@ public function writeText($text, $color = null, $bgColor = null)

/**
* Write a single line of text to console and advance cursor to the next line.
* If the text is longer than console width it will be truncated.
*
*
* @param string $text
* @param null|int $color
* @param null|int $bgColor
*/
public function writeLine($text = "", $color = null, $bgColor = null)
{
$width = $this->getStringWidth($text);

// Remove newline characters from the end of string
$text = trim($text, "\r\n");

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

// Trim the line if it's too long and output text
$consoleWidth = $this->getWidth();
if ($width > $consoleWidth) {
$text = $this->stringTrim($text, $consoleWidth);
$this->write($text, $color, $bgColor);
} elseif ($width == $consoleWidth) {
$this->write($text, $color, $bgColor);
} else {
$this->write($text, $color, $bgColor);
$this->write("\n");
}
$this->write($text . PHP_EOL, $color, $bgColor);
}

/**
Expand Down Expand Up @@ -225,7 +209,7 @@ public function writeBox(
}

} elseif ($fillStyle) {
$fillChar = $this->stringTrim($fillStyle, 1);
$fillChar = StringUtils::getWrapper()->substr($fillStyle, 0, 1);
} else {
$fillChar = ' ';
}
Expand Down Expand Up @@ -356,32 +340,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 +374,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 @@ -526,56 +468,6 @@ public function clearScreen()
return $this->clear();
}

/**
* Helper function that return string length as rendered in console.
*
* @static
* @param $string
* @return int
*/
protected function getStringWidth($string)
{
$width = strlen($string);

if (!$this->isUtf8()) {
return $width;
}

if (static::$hasMBString === null) {
static::$hasMBString = extension_loaded( 'mbstring' );
}

$width = (static::$hasMBString)
? mb_strlen($string, 'UTF-8' )
: strlen(utf8_decode($string));

return $width;
}

/**
* Trim a string in an encoding-safe way
*
* @param mixed $string
* @param mixed $length
* @return int
*/
protected function stringTrim($string, $length)
{
if ($this->isUtf8()) {
if (static::$hasMBString === null) {
static::$hasMBString = extension_loaded('mbstring');
}

if (static::$hasMBString) {
return mb_strlen($string, 'UTF-8');
}

return strlen(utf8_decode($string));
}

return strlen($string);
}

/**
* Read a single line from the console input
*
Expand All @@ -601,7 +493,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
29 changes: 19 additions & 10 deletions src/Adapter/Posix.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ class Posix extends AbstractAdapter
*/
protected $lastTTYMode = null;

/**
* Write a single line of text to console and advance cursor to the next line.
*
* This override works around a bug in some terminals that cause the background color
* to fill the next line after EOL. To remedy this, we are sending the colored string with
* appropriate color reset sequences before sending EOL character.
*
* @link https://github.com/zendframework/zf2/issues/4167
* @param string $text
* @param null|int $color
* @param null|int $bgColor
*/
public function writeLine($text = "", $color = null, $bgColor = null)
{
$this->write($text, $color, $bgColor);
$this->write(PHP_EOL);
}

/**
* Determine and return current console width.
*
Expand Down Expand Up @@ -258,15 +276,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 +324,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
Loading

0 comments on commit a53deb5

Please sign in to comment.