Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 24 additions & 10 deletions lib/cli/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,35 @@ class Shell {
static public function columns() {
static $columns;

if ( getenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' ) ) {
$columns = null;
}
if ( null === $columns ) {
if (self::is_windows() ) {
$output = array();
exec('mode CON', $output);
foreach ($output as $line) {
if (preg_match('/Columns:( )*([0-9]+)/', $line, $matches)) {
$columns = (int)$matches[2];
break;
if ( function_exists( 'exec' ) ) {
if ( self::is_windows() ) {
// Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns.
if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:\.exe)?$/', $shell ) && getenv( 'TERM' ) ) {
$columns = (int) exec( 'tput cols' );
}
if ( ! $columns ) {
$return_var = -1;
$output = array();
exec( 'mode CON', $output, $return_var );
if ( 0 === $return_var && $output ) {
// Look for second line ending in ": <number>" (searching for "Columns:" will fail on non-English locales).
if ( preg_match( '/:\s*[0-9]+\n[^:]+:\s*([0-9]+)\n/', implode( "\n", $output ), $matches ) ) {
$columns = (int) $matches[1];
}
}
}
} else {
if ( getenv( 'TERM' ) ) {
$columns = (int) exec( '/usr/bin/env tput cols' );
}
}
} else if (!preg_match('/(^|,)(\s*)?exec(\s*)?(,|$)/', ini_get('disable_functions'))) {
$columns = (int) exec('/usr/bin/env tput cols');
}

if ( !$columns ) {
if ( ! $columns ) {
$columns = 80; // default width of cmd window on Windows OS
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/test-shell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use cli\Shell;

/**
* Class TestShell
*/
class TestShell extends PHPUnit_Framework_TestCase {

/**
* Test getting TERM columns.
*/
function testColumns() {
// Save.
$env_term = getenv( 'TERM' );
$env_columns = getenv( 'COLUMNS' );
$env_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' );
$env_shell_columns_reset = getenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' );

putenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=1' );

// No TERM should result in default 80.

putenv( 'TERM' );

putenv( 'WP_CLI_TEST_IS_WINDOWS=0' );
$columns = cli\Shell::columns();
$this->assertSame( 80, $columns );

putenv( 'WP_CLI_TEST_IS_WINDOWS=1' );
$columns = cli\Shell::columns();
$this->assertSame( 80, $columns );

// TERM and COLUMNS should result in whatever COLUMNS is.

putenv( 'TERM=vt100' );
putenv( 'COLUMNS=100' );

putenv( 'WP_CLI_TEST_IS_WINDOWS=0' );
$columns = cli\Shell::columns();
$this->assertSame( 100, $columns );

putenv( 'WP_CLI_TEST_IS_WINDOWS=1' );
$columns = cli\Shell::columns();
$this->assertSame( 100, $columns );

// Restore.
putenv( false === $env_term ? 'TERM' : "TERM=$env_term" );
putenv( false === $env_columns ? 'COLUMNS' : "COLUMNS=$env_columns" );
putenv( false === $env_is_windows ? 'WP_CLI_TEST_IS_WINDOWS' : "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" );
putenv( false === $env_shell_columns_reset ? 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' : "PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" );
}
}