36
36
use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
37
37
use Symfony \Component \Console \Exception \CommandNotFoundException ;
38
38
use Symfony \Component \Console \Exception \LogicException ;
39
+ use Symfony \Component \Console \Terminal \TerminalDimensionsProvider ;
39
40
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
40
41
41
42
/**
@@ -65,20 +66,24 @@ class Application
65
66
private $ definition ;
66
67
private $ helperSet ;
67
68
private $ dispatcher ;
68
- private $ terminalDimensions ;
69
69
private $ defaultCommand ;
70
70
private $ singleCommand ;
71
71
72
72
/**
73
- * Constructor.
74
- *
75
- * @param string $name The name of the application
76
- * @param string $version The version of the application
73
+ * @var TerminalDimensionsProvider
77
74
*/
78
- public function __construct ($ name = 'UNKNOWN ' , $ version = 'UNKNOWN ' )
75
+ private $ terminalDimensionsProvider ;
76
+
77
+ /**
78
+ * @param string $name The name of the application
79
+ * @param string $version The version of the application
80
+ * @param TerminalDimensionsProvider $terminalDimensionsProvider
81
+ */
82
+ public function __construct ($ name = 'UNKNOWN ' , $ version = 'UNKNOWN ' , TerminalDimensionsProvider $ terminalDimensionsProvider = null )
79
83
{
80
84
$ this ->name = $ name ;
81
85
$ this ->version = $ version ;
86
+ $ this ->terminalDimensionsProvider = $ terminalDimensionsProvider ?: new TerminalDimensionsProvider ();
82
87
$ this ->defaultCommand = 'list ' ;
83
88
$ this ->helperSet = $ this ->getDefaultHelperSet ();
84
89
$ this ->definition = $ this ->getDefaultInputDefinition ();
@@ -692,9 +697,7 @@ public function renderException(\Exception $e, OutputInterface $output)
692
697
*/
693
698
protected function getTerminalWidth ()
694
699
{
695
- $ dimensions = $ this ->getTerminalDimensions ();
696
-
697
- return $ dimensions [0 ];
700
+ return $ this ->terminalDimensionsProvider ->getTerminalWidth ();
698
701
}
699
702
700
703
/**
@@ -704,9 +707,7 @@ protected function getTerminalWidth()
704
707
*/
705
708
protected function getTerminalHeight ()
706
709
{
707
- $ dimensions = $ this ->getTerminalDimensions ();
708
-
709
- return $ dimensions [1 ];
710
+ return $ this ->terminalDimensionsProvider ->getTerminalWidth ();
710
711
}
711
712
712
713
/**
@@ -716,33 +717,7 @@ protected function getTerminalHeight()
716
717
*/
717
718
public function getTerminalDimensions ()
718
719
{
719
- if ($ this ->terminalDimensions ) {
720
- return $ this ->terminalDimensions ;
721
- }
722
-
723
- if ('\\' === DIRECTORY_SEPARATOR ) {
724
- // extract [w, H] from "wxh (WxH)"
725
- if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
726
- return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
727
- }
728
- // extract [w, h] from "wxh"
729
- if (preg_match ('/^(\d+)x(\d+)$/ ' , $ this ->getConsoleMode (), $ matches )) {
730
- return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
731
- }
732
- }
733
-
734
- if ($ sttyString = $ this ->getSttyColumns ()) {
735
- // extract [w, h] from "rows h; columns w;"
736
- if (preg_match ('/rows.(\d+);.columns.(\d+);/i ' , $ sttyString , $ matches )) {
737
- return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
738
- }
739
- // extract [w, h] from "; h rows; w columns"
740
- if (preg_match ('/;.(\d+).rows;.(\d+).columns/i ' , $ sttyString , $ matches )) {
741
- return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
742
- }
743
- }
744
-
745
- return array (null , null );
720
+ return $ this ->terminalDimensionsProvider ->getTerminalDimensions ();
746
721
}
747
722
748
723
/**
@@ -757,7 +732,7 @@ public function getTerminalDimensions()
757
732
*/
758
733
public function setTerminalDimensions ($ width , $ height )
759
734
{
760
- $ this ->terminalDimensions = array ($ width , $ height );
735
+ $ this ->terminalDimensionsProvider -> setTerminalDimensions ($ width , $ height );
761
736
762
737
return $ this ;
763
738
}
@@ -927,54 +902,6 @@ protected function getDefaultHelperSet()
927
902
));
928
903
}
929
904
930
- /**
931
- * Runs and parses stty -a if it's available, suppressing any error output.
932
- *
933
- * @return string
934
- */
935
- private function getSttyColumns ()
936
- {
937
- if (!function_exists ('proc_open ' )) {
938
- return ;
939
- }
940
-
941
- $ descriptorspec = array (1 => array ('pipe ' , 'w ' ), 2 => array ('pipe ' , 'w ' ));
942
- $ process = proc_open ('stty -a | grep columns ' , $ descriptorspec , $ pipes , null , null , array ('suppress_errors ' => true ));
943
- if (is_resource ($ process )) {
944
- $ info = stream_get_contents ($ pipes [1 ]);
945
- fclose ($ pipes [1 ]);
946
- fclose ($ pipes [2 ]);
947
- proc_close ($ process );
948
-
949
- return $ info ;
950
- }
951
- }
952
-
953
- /**
954
- * Runs and parses mode CON if it's available, suppressing any error output.
955
- *
956
- * @return string <width>x<height> or null if it could not be parsed
957
- */
958
- private function getConsoleMode ()
959
- {
960
- if (!function_exists ('proc_open ' )) {
961
- return ;
962
- }
963
-
964
- $ descriptorspec = array (1 => array ('pipe ' , 'w ' ), 2 => array ('pipe ' , 'w ' ));
965
- $ process = proc_open ('mode CON ' , $ descriptorspec , $ pipes , null , null , array ('suppress_errors ' => true ));
966
- if (is_resource ($ process )) {
967
- $ info = stream_get_contents ($ pipes [1 ]);
968
- fclose ($ pipes [1 ]);
969
- fclose ($ pipes [2 ]);
970
- proc_close ($ process );
971
-
972
- if (preg_match ('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/ ' , $ info , $ matches )) {
973
- return $ matches [2 ].'x ' .$ matches [1 ];
974
- }
975
- }
976
- }
977
-
978
905
/**
979
906
* Returns abbreviated suggestions in string format.
980
907
*
0 commit comments