@@ -27,21 +27,35 @@ class Shell {
27
27
static public function columns () {
28
28
static $ columns ;
29
29
30
+ if ( getenv ( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET ' ) ) {
31
+ $ columns = null ;
32
+ }
30
33
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 ' );
38
54
}
39
55
}
40
- } else if (!preg_match ('/(^|,)(\s*)?exec(\s*)?(,|$)/ ' , ini_get ('disable_functions ' ))) {
41
- $ columns = (int ) exec ('/usr/bin/env tput cols ' );
42
56
}
43
57
44
- if ( !$ columns ) {
58
+ if ( ! $ columns ) {
45
59
$ columns = 80 ; // default width of cmd window on Windows OS
46
60
}
47
61
}
0 commit comments