@@ -204,6 +204,102 @@ public static function getTempDir(): string
204204 return $ tmp ;
205205 }
206206
207+ /**
208+ * get bash is available
209+ * @return bool
210+ */
211+ public static function shIsAvailable (): bool
212+ {
213+ // $checkCmd = "/usr/bin/env bash -c 'echo OK'";
214+ // $shell = 'echo $0';
215+ $ checkCmd = "sh -c 'echo OK' " ;
216+
217+ return self ::runCommand ($ checkCmd , '' , false ) === 'OK ' ;
218+ }
219+
220+ /**
221+ * get bash is available
222+ * @return bool
223+ */
224+ public static function bashIsAvailable (): bool
225+ {
226+ // $checkCmd = "/usr/bin/env bash -c 'echo OK'";
227+ // $shell = 'echo $0';
228+ $ checkCmd = "bash -c 'echo OK' " ;
229+
230+ return self ::runCommand ($ checkCmd , '' , false ) === 'OK ' ;
231+ }
232+
233+ /**
234+ * @return string
235+ */
236+ public static function getOutsideIP (): string
237+ {
238+ list ($ code , $ output ) = self ::run ('ip addr | grep eth0 ' );
239+
240+ if ($ code === 0 && $ output && preg_match ('#inet (.*)\/# ' , $ output , $ ms )) {
241+ return $ ms [1 ];
242+ }
243+
244+ return 'unknown ' ;
245+ }
246+
247+ /**
248+ * get screen size
249+ *
250+ * ```php
251+ * list($width, $height) = Sys::getScreenSize();
252+ * ```
253+ * @from Yii2
254+ * @param boolean $refresh whether to force checking and not re-use cached size value.
255+ * This is useful to detect changing window size while the application is running but may
256+ * not get up to date values on every terminal.
257+ * @return array|boolean An array of ($width, $height) or false when it was not able to determine size.
258+ */
259+ public static function getScreenSize ($ refresh = false )
260+ {
261+ static $ size ;
262+ if ($ size !== null && !$ refresh ) {
263+ return $ size ;
264+ }
265+
266+ if (self ::shIsAvailable ()) {
267+ // try stty if available
268+ $ stty = [];
269+
270+ if (
271+ exec ('stty -a 2>&1 ' , $ stty ) &&
272+ preg_match ('/rows\s+(\d+);\s*columns\s+(\d+);/mi ' , implode (' ' , $ stty ), $ matches )
273+ ) {
274+ return ($ size = [$ matches [2 ], $ matches [1 ]]);
275+ }
276+
277+ // fallback to tput, which may not be updated on terminal resize
278+ if (($ width = (int )exec ('tput cols 2>&1 ' )) > 0 && ($ height = (int )exec ('tput lines 2>&1 ' )) > 0 ) {
279+ return ($ size = [$ width , $ height ]);
280+ }
281+
282+ // fallback to ENV variables, which may not be updated on terminal resize
283+ if (($ width = (int )getenv ('COLUMNS ' )) > 0 && ($ height = (int )getenv ('LINES ' )) > 0 ) {
284+ return ($ size = [$ width , $ height ]);
285+ }
286+ }
287+
288+ if (self ::isWindows ()) {
289+ $ output = [];
290+ exec ('mode con ' , $ output );
291+
292+ if (isset ($ output [1 ]) && strpos ($ output [1 ], 'CON ' ) !== false ) {
293+ return ($ size = [
294+ (int )preg_replace ('~\D~ ' , '' , $ output [3 ]),
295+ (int )preg_replace ('~\D~ ' , '' , $ output [4 ])
296+ ]);
297+ }
298+ }
299+
300+ return ($ size = false );
301+ }
302+
207303 /**
208304 * @param string $program
209305 * @return int|string
0 commit comments