@@ -797,7 +797,7 @@ public function renderException($e, $output)
797
797
$ len = $ strlen ($ title );
798
798
$ width = $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 1 : PHP_INT_MAX ;
799
799
$ lines = array ();
800
- foreach (preg_split (" { \r? \n} " , $ e ->getMessage ()) as $ line ) {
800
+ foreach (preg_split (' / \r?\n/ ' , $ e ->getMessage ()) as $ line ) {
801
801
foreach (str_split ($ line , $ width - 4 ) as $ line ) {
802
802
$ lines [] = sprintf (' %s ' , $ line );
803
803
$ len = max ($ strlen ($ line ) + 4 , $ len );
@@ -859,43 +859,55 @@ public function renderException($e, $output)
859
859
*
860
860
* @return int|null
861
861
*/
862
- public function getTerminalWidth ()
862
+ protected function getTerminalWidth ()
863
863
{
864
- if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
865
- if ($ ansicon = getenv ('ANSICON ' )) {
866
- return preg_replace ('{^(\d+)x.*$} ' , '$1 ' , $ ansicon );
867
- }
868
-
869
- if (preg_match ('{^(\d+)x\d+$}i ' , $ this ->getConsoleMode (), $ matches )) {
870
- return $ matches [1 ];
871
- }
872
- }
864
+ $ dimensions = $ this ->getTerminalDimensions ();
873
865
874
- if (preg_match ("{rows.(\d+);.columns.(\d+);}i " , $ this ->getSttyColumns (), $ match )) {
875
- return $ match [2 ];
876
- }
866
+ return $ dimensions [0 ];
877
867
}
878
868
879
869
/**
880
870
* Tries to figure out the terminal height in which this application runs
881
871
*
882
872
* @return int|null
883
873
*/
884
- public function getTerminalHeight ()
874
+ protected function getTerminalHeight ()
875
+ {
876
+ $ dimensions = $ this ->getTerminalDimensions ();
877
+
878
+ return $ dimensions [1 ];
879
+ }
880
+
881
+ /**
882
+ * Tries to figure out the terminal dimensions based on the current environment
883
+ *
884
+ * @return array Array containing width and height
885
+ */
886
+ public function getTerminalDimensions ()
885
887
{
886
888
if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
887
- if ($ ansicon = getenv ('ANSICON ' )) {
888
- return preg_replace ('{^\d+x\d+ \(\d+x(\d+)\)$} ' , '$1 ' , trim ($ ansicon ));
889
+ // extract [w, H] from "wxh (WxH)"
890
+ if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
891
+ return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
889
892
}
890
-
891
- if (preg_match ('{^ \d+x(\d+)$}i ' , $ this ->getConsoleMode (), $ matches )) {
892
- return $ matches [1 ];
893
+ // extract [w, h] from "wxh"
894
+ if (preg_match ('/^( \d+) x(\d+)$/ ' , $ this ->getConsoleMode (), $ matches )) {
895
+ return array (( int ) $ matches [1 ], ( int ) $ matches [ 2 ]) ;
893
896
}
894
897
}
895
898
896
- if (preg_match ("{rows.(\d+);.columns.(\d+);}i " , $ this ->getSttyColumns (), $ match )) {
897
- return $ match [1 ];
899
+ if ($ sttyString = $ this ->getSttyColumns ()) {
900
+ // extract [w, h] from "rows h; columns w;"
901
+ if (preg_match ('/rows.(\d+);.columns.(\d+);/i ' , $ sttyString , $ matches )) {
902
+ return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
903
+ }
904
+ // extract [w, h] from "; h rows; w columns"
905
+ if (preg_match ('/;.(\d+).rows;.(\d+).columns/i ' , $ sttyString , $ matches )) {
906
+ return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
907
+ }
898
908
}
909
+
910
+ return array (null , null );
899
911
}
900
912
901
913
/**
@@ -996,7 +1008,7 @@ private function getConsoleMode()
996
1008
fclose ($ pipes [2 ]);
997
1009
proc_close ($ process );
998
1010
999
- if (preg_match ('{ --------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n} ' , $ info , $ matches )) {
1011
+ if (preg_match ('/ --------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/ ' , $ info , $ matches )) {
1000
1012
return $ matches [2 ].'x ' .$ matches [1 ];
1001
1013
}
1002
1014
}
0 commit comments