Helper/ConsoleHelper.php
public static function getScreenSize( $refresh = false ) {
		static $size;
		if ( $size !== null && ! $refresh ) {
			return $size;
		}
		if ( static::isRunningOnWindows() ) {
			$output = [];
			exec( 'mode con', $output );
			if ( isset( $output, $output[1] ) && strpos( $output[1], 'CON' ) !== false ) {
				return $size = [
					(int) preg_replace( '~\D~', '', $output[4] ),
					(int) preg_replace( '~\D~', '', $output[3] )
				];
			}
		} else {
			// try stty if available
			$stty = [];
			if ( exec( 'stty -a 2>&1', $stty ) ) {
				$stty = implode( ' ', $stty );
				// Linux stty output
				if ( preg_match( '/rows\s+(\d+);\s*columns\s+(\d+);/mi', $stty, $matches ) ) {
					return $size = [ (int) $matches[2], (int) $matches[1] ];
				}
				// MacOS stty output
				if ( preg_match( '/(\d+)\s+rows;\s*(\d+)\s+columns;/mi', $stty, $matches ) ) {
					return $size = [ (int) $matches[2], (int) $matches[1] ];
				}
			}
			// fallback to tput, which may not be updated on terminal resize
			if ( ( $width = (int) exec( 'tput cols 2>&1' ) ) > 0 && ( $height = (int) exec( 'tput lines 2>&1' ) ) > 0 ) {
				return $size = [ $width, $height ];
			}
			// fallback to ENV variables, which may not be updated on terminal resize
			if ( ( $width = (int) getenv( 'COLUMNS' ) ) > 0 && ( $height = (int) getenv( 'LINES' ) ) > 0 ) {
				return $size = [ $width, $height ];
			}
		}

		return $size = false;
	}
public static function getScreenSize( $refresh = false ) {
		static $size;
		if ( $size !== null && ! $refresh ) {
			return $size;
		}
		if ( static::isRunningOnWindows() ) {
			$output = [];
			exec( 'mode con', $output );
			if ( isset( $output, $output[1] ) && strpos( $output[1], 'CON' ) !== false ) {
				return $size = [
					(int) preg_replace( '~\D~', '', $output[4] ),
					(int) preg_replace( '~\D~', '', $output[3] )
				];
			}
		} else {
			// try stty if available
			$stty = [];
			if ( exec( 'stty -a 2>&1', $stty ) ) {
				$stty = implode( ' ', $stty );
				// Linux stty output
				if ( preg_match( '/rows\s+(\d+);\s*columns\s+(\d+);/mi', $stty, $matches ) ) {
					return $size = [ (int) $matches[2], (int) $matches[1] ];
				}
				// MacOS stty output
				if ( preg_match( '/(\d+)\s+rows;\s*(\d+)\s+columns;/mi', $stty, $matches ) ) {
					return $size = [ (int) $matches[2], (int) $matches[1] ];
				}
			}
			// fallback to tput, which may not be updated on terminal resize
			if ( ( $width = (int) exec( 'tput cols 2>&1' ) ) > 0 && ( $height = (int) exec( 'tput lines 2>&1' ) ) > 0 ) {
				return $size = [ $width, $height ];
			}
			// fallback to ENV variables, which may not be updated on terminal resize
			if ( ( $width = (int) getenv( 'COLUMNS' ) ) > 0 && ( $height = (int) getenv( 'LINES' ) ) > 0 ) {
				return $size = [ $width, $height ];
			}
		}

		return $size = false;
	}
public static function getScreenSize ( $ refresh = false ) { static $ size ; if ( $ size !== null && ! $ refresh ) { return $ size ; } if ( static :: isRunningOnWindows ( ) ) { $ output = [ ] ; exec ( 'mode con' , $ output ) ; if ( isset ( $ output , $ output [ 1 ] ) && strpos ( $ output [ 1 ] , 'CON' ) !== false ) { return $ size = [ ( int ) preg_replace ( '~\D~' , '' , $ output [ 4 ] ) , ( int ) preg_replace ( '~\D~' , '' , $ output [ 3 ] ) ] ; } } else { // try stty if available $ stty = [ ] ; if ( exec ( 'stty -a 2>&1' , $ stty ) ) { $ stty = implode ( ' ' , $ stty ) ; // Linux stty output if ( preg_match ( '/rows\s+(\d+);\s*columns\s+(\d+);/mi' , $ stty , $ matches ) ) { return $ size = [ ( int ) $ matches [ 2 ] , ( int ) $ matches [ 1 ] ] ; } // MacOS stty output if ( preg_match ( '/(\d+)\s+rows;\s*(\d+)\s+columns;/mi' , $ stty , $ matches ) ) { return $ size = [ ( int ) $ matches [ 2 ] , ( int ) $ matches [ 1 ] ] ; } } // fallback to tput, which may not be updated on terminal resize if ( ( $ width = ( int ) exec ( 'tput cols 2>&1' ) ) > 0 && ( $ height = ( int ) exec ( 'tput lines 2>&1' ) ) > 0 ) { return $ size = [ $ width , $ height ] ; } // fallback to ENV variables, which may not be updated on terminal resize if ( ( $ width = ( int ) getenv ( 'COLUMNS' ) ) > 0 && ( $ height = ( int ) getenv ( 'LINES' ) ) > 0 ) { return $ size = [ $ width , $ height ] ; } } return $ size = false ; }
Returns terminal screen size.

Usage:

```php
list($width, $height) = ConsoleHelper::getScreenSize();
```

@param bool $refresh whether to force checking and not re-use cached size value.
This is useful to detect changing window size while the application is running but may
not get up to date values on every terminal.

@return array|bool An array of ($width, $height) or false when it was not able to determine size.
Returns terminal screen size .