@@ -784,7 +784,7 @@ public function renderException($e, $output)
784
784
$ len = $ strlen ($ title );
785
785
$ width = $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 1 : PHP_INT_MAX ;
786
786
$ lines = array ();
787
- foreach (preg_split (" { \r? \n} " , $ e ->getMessage ()) as $ line ) {
787
+ foreach (preg_split (' / \r?\n/ ' , $ e ->getMessage ()) as $ line ) {
788
788
foreach (str_split ($ line , $ width - 4 ) as $ line ) {
789
789
$ lines [] = sprintf (' %s ' , $ line );
790
790
$ len = max ($ strlen ($ line ) + 4 , $ len );
@@ -848,19 +848,9 @@ public function renderException($e, $output)
848
848
*/
849
849
public function getTerminalWidth ()
850
850
{
851
- if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
852
- if ($ ansicon = getenv ('ANSICON ' )) {
853
- return preg_replace ('{^(\d+)x.*$} ' , '$1 ' , $ ansicon );
854
- }
855
-
856
- if (preg_match ('{^(\d+)x\d+$}i ' , $ this ->getConsoleMode (), $ matches )) {
857
- return $ matches [1 ];
858
- }
859
- }
851
+ $ dimensions = $ this ->getTerminalDimensions ();
860
852
861
- if (preg_match ("{rows.(\d+);.columns.(\d+);}i " , $ this ->getSttyColumns (), $ match )) {
862
- return $ match [2 ];
863
- }
853
+ return $ dimensions [0 ];
864
854
}
865
855
866
856
/**
@@ -869,20 +859,42 @@ public function getTerminalWidth()
869
859
* @return int|null
870
860
*/
871
861
public function getTerminalHeight ()
862
+ {
863
+ $ dimensions = $ this ->getTerminalDimensions ();
864
+
865
+ return $ dimensions [1 ];
866
+ }
867
+
868
+ /**
869
+ * Tries to figure out the terminal dimensions based on the current environment
870
+ *
871
+ * @return array Array containing width and height
872
+ */
873
+ protected function getTerminalDimensions ()
872
874
{
873
875
if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
874
- if ($ ansicon = getenv ('ANSICON ' )) {
875
- return preg_replace ('{^\d+x\d+ \(\d+x(\d+)\)$} ' , '$1 ' , trim ($ ansicon ));
876
+ // extract [w, H] from "wxh (WxH)"
877
+ if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
878
+ return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
876
879
}
877
-
878
- if (preg_match ('{^ \d+x(\d+)$}i ' , $ this ->getConsoleMode (), $ matches )) {
879
- return $ matches [1 ];
880
+ // extract [w, h] from "wxh"
881
+ if (preg_match ('/^( \d+) x(\d+)$/ ' , $ this ->getConsoleMode (), $ matches )) {
882
+ return array (( int ) $ matches [1 ], ( int ) $ matches [ 2 ]) ;
880
883
}
881
884
}
882
885
883
- if (preg_match ("{rows.(\d+);.columns.(\d+);}i " , $ this ->getSttyColumns (), $ match )) {
884
- return $ match [1 ];
886
+ if ($ sttyString = $ this ->getSttyColumns ()) {
887
+ // extract [w, h] from "rows h; columns w;"
888
+ if (preg_match ('/rows.(\d+);.columns.(\d+);/i ' , $ sttyString , $ matches )) {
889
+ return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
890
+ }
891
+ // extract [w, h] from "; h rows; w columns"
892
+ if (preg_match ('/;.(\d+).rows;.(\d+).columns/i ' , $ sttyString , $ matches )) {
893
+ return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
894
+ }
885
895
}
896
+
897
+ return array (null , null );
886
898
}
887
899
888
900
/**
@@ -982,7 +994,7 @@ private function getConsoleMode()
982
994
fclose ($ pipes [2 ]);
983
995
proc_close ($ process );
984
996
985
- if (preg_match ('{ --------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n} ' , $ info , $ matches )) {
997
+ if (preg_match ('/ --------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/ ' , $ info , $ matches )) {
986
998
return $ matches [2 ].'x ' .$ matches [1 ];
987
999
}
988
1000
}
0 commit comments