Passed
Push — master ( db1ea6...f2ea2d )
by Théo
03:06
created

Terminal::getHeight()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the box project.
5
 *
6
 * (c) Kevin Herrera <[email protected]>
7
 *     Théo Fidry <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
namespace _HumbugBox5addf3ce683e7\KevinGH\RequirementChecker;
13
14
/**
15
 * This file is copy/pasted from the Symfony project to avoid a dependency on `symfony/console` which would be too big for just using this
16
 * class.
17
 *
18
 * @license MIT (c) Fabien Potencier <[email protected]>
19
 */
20
class Terminal
21
{
22
    private static $width;
23
    private static $height;
24
    /**
25
     * Gets the terminal width.
26
     *
27
     * @return int
28
     */
29
    public function getWidth()
30
    {
31
        $width = \getenv('COLUMNS');
32
        if (\false !== $width) {
33
            return (int) \trim($width);
34
        }
35
        if (null === self::$width) {
36
            self::initDimensions();
37
        }
38
        return self::$width ?: 80;
39
    }
40
    /**
41
     * Gets the terminal height.
42
     *
43
     * @return int
44
     */
45
    public function getHeight()
46
    {
47
        $height = \getenv('LINES');
48
        if (\false !== $height) {
49
            return (int) \trim($height);
50
        }
51
        if (null === self::$height) {
52
            self::initDimensions();
53
        }
54
        return self::$height ?: 50;
55
    }
56
    private static function initDimensions()
57
    {
58
        if ('\\' === \DIRECTORY_SEPARATOR) {
59
            if (\preg_match('/^(\\d+)x(\\d+)(?: \\((\\d+)x(\\d+)\\))?$/', \trim(\getenv('ANSICON')), $matches)) {
60
                // extract [w, H] from "wxh (WxH)"
61
                // or [w, h] from "wxh"
62
                self::$width = (int) $matches[1];
63
                self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
64
            } elseif (null !== ($dimensions = self::getConsoleMode())) {
65
                // extract [w, h] from "wxh"
66
                self::$width = (int) $dimensions[0];
67
                self::$height = (int) $dimensions[1];
68
            }
69
        } elseif ($sttyString = self::getSttyColumns()) {
70
            if (\preg_match('/rows.(\\d+);.columns.(\\d+);/i', $sttyString, $matches)) {
71
                // extract [w, h] from "rows h; columns w;"
72
                self::$width = (int) $matches[2];
73
                self::$height = (int) $matches[1];
74
            } elseif (\preg_match('/;.(\\d+).rows;.(\\d+).columns/i', $sttyString, $matches)) {
75
                // extract [w, h] from "; h rows; w columns"
76
                self::$width = (int) $matches[2];
77
                self::$height = (int) $matches[1];
78
            }
79
        }
80
    }
81
    /**
82
     * Runs and parses mode CON if it's available, suppressing any error output.
83
     *
84
     * @return null|int[] An array composed of the width and the height or null if it could not be parsed
85
     */
86
    private static function getConsoleMode()
87
    {
88
        if (!\function_exists('proc_open')) {
89
            return;
90
        }
91
        $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
92
        $process = \proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => \true));
93
        if (\is_resource($process)) {
94
            $info = \stream_get_contents($pipes[1]);
95
            \fclose($pipes[1]);
96
            \fclose($pipes[2]);
97
            \proc_close($process);
98
            if (\preg_match('/--------+\\r?\\n.+?(\\d+)\\r?\\n.+?(\\d+)\\r?\\n/', $info, $matches)) {
99
                return array((int) $matches[2], (int) $matches[1]);
100
            }
101
        }
102
    }
103
    /**
104
     * Runs and parses stty -a if it's available, suppressing any error output.
105
     *
106
     * @return null|string
107
     */
108
    private static function getSttyColumns()
109
    {
110
        if (!\function_exists('proc_open')) {
111
            return;
112
        }
113
        $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
114
        $process = \proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => \true));
115
        if (\is_resource($process)) {
116
            $info = \stream_get_contents($pipes[1]);
117
            \fclose($pipes[1]);
118
            \fclose($pipes[2]);
119
            \proc_close($process);
120
            return $info;
121
        }
122
        return null;
123
    }
124
}
125