@@ -27,21 +27,35 @@ class Shell {
2727 static public function columns () {
2828 static $ columns ;
2929
30+ if ( getenv ( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET ' ) ) {
31+ $ columns = null ;
32+ }
3033 if ( null === $ columns ) {
31- if (self ::is_windows () ) {
32- $ output = array ();
33- exec ('mode CON ' , $ output );
34- foreach ($ output as $ line ) {
35- if (preg_match ('/Columns:( )*([0-9]+)/ ' , $ line , $ matches )) {
36- $ columns = (int )$ matches [2 ];
37- break ;
34+ if ( function_exists ( 'exec ' ) ) {
35+ if ( self ::is_windows () ) {
36+ // Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns.
37+ if ( ( $ shell = getenv ( 'SHELL ' ) ) && preg_match ( '/(?:bash|zsh)(?:\.exe)?$/ ' , $ shell ) && getenv ( 'TERM ' ) ) {
38+ $ columns = (int ) exec ( 'tput cols ' );
39+ }
40+ if ( ! $ columns ) {
41+ $ return_var = -1 ;
42+ $ output = array ();
43+ exec ( 'mode CON ' , $ output , $ return_var );
44+ if ( 0 === $ return_var && $ output ) {
45+ // Look for second line ending in ": <number>" (searching for "Columns:" will fail on non-English locales).
46+ if ( preg_match ( '/:\s*[0-9]+\n[^:]+:\s*([0-9]+)\n/ ' , implode ( "\n" , $ output ), $ matches ) ) {
47+ $ columns = (int ) $ matches [1 ];
48+ }
49+ }
50+ }
51+ } else {
52+ if ( getenv ( 'TERM ' ) ) {
53+ $ columns = (int ) exec ( '/usr/bin/env tput cols ' );
3854 }
3955 }
40- } else if (!preg_match ('/(^|,)(\s*)?exec(\s*)?(,|$)/ ' , ini_get ('disable_functions ' ))) {
41- $ columns = (int ) exec ('/usr/bin/env tput cols ' );
4256 }
4357
44- if ( !$ columns ) {
58+ if ( ! $ columns ) {
4559 $ columns = 80 ; // default width of cmd window on Windows OS
4660 }
4761 }
0 commit comments