Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
82462ec
Various half-worked fixes relating to #106 and PR #109.
gitlost Jul 24, 2017
e92d9bc
Merge remote-tracking branch 'upstream/master' into issue_106
gitlost Jul 24, 2017
7a7438d
Remove bool hint on decolorize().
gitlost Jul 24, 2017
a9e31e0
Sigh. Remove the other ones.
gitlost Jul 24, 2017
4bd13bc
Double sigh. Remove all the other ones.
gitlost Jul 24, 2017
57befc8
Use strwidth() in test_column_value_too_long_with_multibytes.
gitlost Jul 24, 2017
79a75c9
Set length to safe_strlen() if null for cross PHP compat. Explicitly …
gitlost Jul 24, 2017
62fdd75
Refactor the unicode regexs into a function.
gitlost Jul 24, 2017
7466f68
Copy core _mb_substr. Add encoding arg. Add keep arg to decolorize.
gitlost Jul 25, 2017
2d2b582
Merge pull request #111 from gitlost/issue_106
miya0001 Jul 25, 2017
105bc96
Suppress tput stderr. Use COLUMNS env var 1st if available.
gitlost Jul 26, 2017
c5f9c03
Merge pull request #113 from wp-cli/issue_112
danielbachhuber Jul 26, 2017
4cc65fd
Optimize double-width safe_substr when all double-width.
gitlost Jul 29, 2017
e8ac443
Need dummy matches arg for preg_match_all() PHP 5.3 (arghh).
gitlost Jul 29, 2017
cacbdf4
Er, put it in the right place (arghh).
gitlost Jul 29, 2017
7667f5e
Round down instead of up in safe_strlen().
gitlost Jul 29, 2017
168e5bc
add single-witdh char to test
miya0001 Jul 29, 2017
0685ba2
remove print_r().
miya0001 Jul 29, 2017
18b8e3d
add single-width char to second line
miya0001 Jul 29, 2017
2c0896b
Merge pull request #115 from miya0001/double_width_optimize
gitlost Jul 29, 2017
65b5ac2
Fix reverse (inverse) and white (grey).
gitlost Jul 30, 2017
c5fb346
Merge pull request #116 from wp-cli/colors
danielbachhuber Jul 31, 2017
37cf6e1
Merge pull request #114 from wp-cli/double_width_optimize
schlessera Aug 1, 2017
f8bb561
Use grapheme_substr & pcre_match in safe_substr. Ascii::columns fix.
gitlost Aug 3, 2017
5591e23
Put stty inside TERM check. Add some php info to travis.
gitlost Aug 3, 2017
73ef90d
phpunit --debug
gitlost Aug 3, 2017
9305edd
icu info
gitlost Aug 3, 2017
ceadb8a
Disable some table tests.
gitlost Aug 3, 2017
e643eea
Disable test_column_value_too_long_with_multibytes.
gitlost Aug 3, 2017
1263375
Disable test_column_fullwidth_and_combining.
gitlost Aug 3, 2017
bc2eb06
Actually disable test_column_fullwidth_and_combining.
gitlost Aug 3, 2017
bba3559
Add can_use_icu().
gitlost Aug 3, 2017
8c02e4e
Reenable test_column_value_too_long_with_multibytes.
gitlost Aug 3, 2017
768f836
Add phpunit6-compat.php.
gitlost Aug 3, 2017
61ec37f
Re-enable tests.
gitlost Aug 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

script: phpunit
before_script:
- php -m
- php --info | grep -i 'intl\|icu\|pcre'

script: phpunit --debug

notifications:
email:
Expand Down
91 changes: 46 additions & 45 deletions lib/cli/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static public function color($color) {

$colors = array();
foreach (array('color', 'style', 'background') as $type) {
$code = @$color[$type];
$code = $color[$type];
if (isset(self::$_colors[$type][$code])) {
$colors[] = self::$_colors[$type][$code];
}
Expand All @@ -115,26 +115,25 @@ static public function color($color) {
static public function colorize($string, $colored = null) {
$passed = $string;

if (isset(self::$_string_cache[md5($passed)]['colorized'])) {
return self::$_string_cache[md5($passed)]['colorized'];
}

if (!self::shouldColorize($colored)) {
$colors = self::getColors();
$search = array_keys( $colors );
$return = str_replace( $search, '', $string );
self::cacheString($passed, $return, $colored);
$return = self::decolorize( $passed, 2 /*keep_encodings*/ );
self::cacheString($passed, $return);
return $return;
}

$md5 = md5($passed);
if (isset(self::$_string_cache[$md5]['colorized'])) {
return self::$_string_cache[$md5]['colorized'];
}

$string = str_replace('%%', '%¾', $string);

foreach (self::getColors() as $key => $value) {
$string = str_replace($key, self::color($value), $string);
}

$string = str_replace('%¾', '%', $string);
self::cacheString($passed, $string, $colored);
self::cacheString($passed, $string);

return $string;
}
Expand All @@ -143,15 +142,22 @@ static public function colorize($string, $colored = null) {
* Remove color information from a string.
*
* @param string $string A string with color information.
* @param int $keep Optional. If the 1 bit is set, color tokens (eg "%n") won't be stripped. If the 2 bit is set, color encodings (ANSI escapes) won't be stripped. Default 0.
* @return string A string with color information removed.
*/
static public function decolorize($string) {
// Get rid of color tokens if they exist
$string = str_replace(array_keys(self::getColors()), '', $string);
static public function decolorize( $string, $keep = 0 ) {
if ( ! ( $keep & 1 ) ) {
// Get rid of color tokens if they exist
$string = str_replace('%%', '%¾', $string);
$string = str_replace(array_keys(self::getColors()), '', $string);
$string = str_replace('%¾', '%', $string);
}

// Remove color encoding if it exists
foreach (self::getColors() as $key => $value) {
$string = str_replace(self::color($value), '', $string);
if ( ! ( $keep & 2 ) ) {
// Remove color encoding if it exists
foreach (self::getColors() as $key => $value) {
$string = str_replace(self::color($value), '', $string);
}
}

return $string;
Expand All @@ -162,13 +168,13 @@ static public function decolorize($string) {
*
* @param string $passed The original string before colorization.
* @param string $colorized The string after running through self::colorize.
* @param string $colored The string without any color information.
* @param string $deprecated Optional. Not used. Default null.
*/
static public function cacheString($passed, $colorized, $colored) {
static public function cacheString( $passed, $colorized, $deprecated = null ) {
self::$_string_cache[md5($passed)] = array(
'passed' => $passed,
'colorized' => $colorized,
'decolorized' => self::decolorize($passed)
'decolorized' => self::decolorize($passed), // Not very useful but keep for BC.
);
}

Expand All @@ -179,41 +185,36 @@ static public function cacheString($passed, $colorized, $colored) {
* @return int
*/
static public function length($string) {
if (isset(self::$_string_cache[md5($string)]['decolorized'])) {
$test_string = self::$_string_cache[md5($string)]['decolorized'];
} else {
$test_string = self::decolorize($string);
}

return safe_strlen($test_string);
return safe_strlen( self::decolorize( $string ) );
}

/**
* Return the width (length in characters) of the string without color codes.
* Return the width (length in characters) of the string without color codes if enabled.
*
* @param string $string the string to measure
* @param string $string The string to measure.
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
* @param string|bool $encoding Optional. The encoding of the string. Default false.
* @return int
*/
static public function width($string) {
$md5 = md5($string);
if (isset(self::$_string_cache[$md5]['decolorized'])) {
$test_string = self::$_string_cache[$md5]['decolorized'];
} else {
$test_string = self::decolorize($string);
}

return strwidth($test_string);
static public function width( $string, $pre_colorized = false, $encoding = false ) {
return strwidth( $pre_colorized || self::shouldColorize() ? self::decolorize( $string, $pre_colorized ? 1 /*keep_tokens*/ : 0 ) : $string, $encoding );
}

/**
* Pad the string to a certain display length.
*
* @param string $string the string to pad
* @param integer $length the display length
* @param string $string The string to pad.
* @param int $length The display length.
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
* @param string|bool $encoding Optional. The encoding of the string. Default false.
* @return string
*/
static public function pad($string, $length) {
return safe_str_pad( $string, $length );
static public function pad( $string, $length, $pre_colorized = false, $encoding = false ) {
$real_length = self::width( $string, $pre_colorized, $encoding );
$diff = strlen( $string ) - $real_length;
$length += $diff;

return str_pad( $string, $length );
}

/**
Expand All @@ -230,7 +231,7 @@ static public function getColors() {
'%p' => array('color' => 'magenta'),
'%m' => array('color' => 'magenta'),
'%c' => array('color' => 'cyan'),
'%w' => array('color' => 'grey'),
'%w' => array('color' => 'white'),
'%k' => array('color' => 'black'),
'%n' => array('color' => 'reset'),
'%Y' => array('color' => 'yellow', 'style' => 'bright'),
Expand All @@ -240,7 +241,7 @@ static public function getColors() {
'%P' => array('color' => 'magenta', 'style' => 'bright'),
'%M' => array('color' => 'magenta', 'style' => 'bright'),
'%C' => array('color' => 'cyan', 'style' => 'bright'),
'%W' => array('color' => 'grey', 'style' => 'bright'),
'%W' => array('color' => 'white', 'style' => 'bright'),
'%K' => array('color' => 'black', 'style' => 'bright'),
'%N' => array('color' => 'reset', 'style' => 'bright'),
'%3' => array('background' => 'yellow'),
Expand All @@ -249,11 +250,11 @@ static public function getColors() {
'%1' => array('background' => 'red'),
'%5' => array('background' => 'magenta'),
'%6' => array('background' => 'cyan'),
'%7' => array('background' => 'grey'),
'%7' => array('background' => 'white'),
'%0' => array('background' => 'black'),
'%F' => array('style' => 'blink'),
'%U' => array('style' => 'underline'),
'%8' => array('style' => 'inverse'),
'%8' => array('style' => 'reverse'),
'%9' => array('style' => 'bright'),
'%_' => array('style' => 'bright')
);
Expand Down
12 changes: 10 additions & 2 deletions lib/cli/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ static public function columns() {
}
}
} else {
if ( getenv( 'TERM' ) ) {
$columns = (int) exec( '/usr/bin/env tput cols' );
if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) ) {
if ( getenv( 'TERM' ) ) {
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
$columns = (int) $matches[1];
}
if ( ! $columns ) {
$columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' );
}
}
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion lib/cli/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function setRenderer(Renderer $renderer) {
*/
protected function checkRow(array $row) {
foreach ($row as $column => $str) {
$width = Colors::shouldColorize() ? Colors::width($str) : strwidth($str);
$width = Colors::width( $str, $this->isAsciiPreColorized( $column ) );
if (!isset($this->_width[$column]) || $width > $this->_width[$column]) {
$this->_width[$column] = $width;
}
Expand Down Expand Up @@ -228,4 +228,30 @@ public function setRows(array $rows) {
public function countRows() {
return count($this->_rows);
}

/**
* Set whether items in an Ascii table are pre-colorized.
*
* @param bool|array $precolorized A boolean to set all columns in the table as pre-colorized, or an array of booleans keyed by column index (number) to set individual columns as pre-colorized.
* @see cli\Ascii::setPreColorized()
*/
public function setAsciiPreColorized( $pre_colorized ) {
if ( $this->_renderer instanceof Ascii ) {
$this->_renderer->setPreColorized( $pre_colorized );
}
}

/**
* Is a column in an Ascii table pre-colorized?
*
* @param int $column Column index to check.
* @return bool True if whole Ascii table is marked as pre-colorized, or if the individual column is pre-colorized; else false.
* @see cli\Ascii::isPreColorized()
*/
private function isAsciiPreColorized( $column ) {
if ( $this->_renderer instanceof Ascii ) {
return $this->_renderer->isPreColorized( $column );
}
return false;
}
}
Loading