Passed
Pull Request — master (#116)
by Théo
02:06
created

Terminal::initDimensions()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 14
nc 8
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace 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
class Terminal
19
{
20
    private static $width;
21
    private static $height;
22
23
    /**
24
     * Gets the terminal width.
25
     *
26
     * @return int
27
     */
28
    public function getWidth()
29
    {
30
        $width = getenv('COLUMNS');
31
        if (false !== $width) {
32
            return (int) trim($width);
33
        }
34
35
        if (null === self::$width) {
36
            self::initDimensions();
37
        }
38
39
        return self::$width ?: 80;
40
    }
41
42
    /**
43
     * Gets the terminal height.
44
     *
45
     * @return int
46
     */
47
    public function getHeight()
48
    {
49
        $height = getenv('LINES');
50
        if (false !== $height) {
51
            return (int) trim($height);
52
        }
53
54
        if (null === self::$height) {
55
            self::initDimensions();
56
        }
57
58
        return self::$height ?: 50;
59
    }
60
61
    private static function initDimensions()
62
    {
63
        if ('\\' === DIRECTORY_SEPARATOR) {
64
            if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
65
                // extract [w, H] from "wxh (WxH)"
66
                // or [w, h] from "wxh"
67
                self::$width = (int) $matches[1];
68
                self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
69
            } elseif (null !== $dimensions = self::getConsoleMode()) {
70
                // extract [w, h] from "wxh"
71
                self::$width = (int) $dimensions[0];
72
                self::$height = (int) $dimensions[1];
73
            }
74
        } elseif ($sttyString = self::getSttyColumns()) {
75
            if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
76
                // extract [w, h] from "rows h; columns w;"
77
                self::$width = (int) $matches[2];
78
                self::$height = (int) $matches[1];
79
            } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
80
                // extract [w, h] from "; h rows; w columns"
81
                self::$width = (int) $matches[2];
82
                self::$height = (int) $matches[1];
83
            }
84
        }
85
    }
86
87
    /**
88
     * Runs and parses mode CON if it's available, suppressing any error output.
89
     *
90
     * @return int[]|null An array composed of the width and the height or null if it could not be parsed
91
     */
92
    private static function getConsoleMode()
93
    {
94
        if (!function_exists('proc_open')) {
95
            return;
96
        }
97
98
        $descriptorspec = array(
99
            1 => array('pipe', 'w'),
100
            2 => array('pipe', 'w'),
101
        );
102
        $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
103
        if (is_resource($process)) {
104
            $info = stream_get_contents($pipes[1]);
105
            fclose($pipes[1]);
106
            fclose($pipes[2]);
107
            proc_close($process);
108
109
            if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
110
                return array((int) $matches[2], (int) $matches[1]);
111
            }
112
        }
113
    }
114
115
    /**
116
     * Runs and parses stty -a if it's available, suppressing any error output.
117
     *
118
     * @return string|null
119
     */
120
    private static function getSttyColumns()
121
    {
122
        if (!function_exists('proc_open')) {
123
            return;
124
        }
125
126
        $descriptorspec = array(
127
            1 => array('pipe', 'w'),
128
            2 => array('pipe', 'w'),
129
        );
130
131
        $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
132
        if (is_resource($process)) {
133
            $info = stream_get_contents($pipes[1]);
134
            fclose($pipes[1]);
135
            fclose($pipes[2]);
136
            proc_close($process);
137
138
            return $info;
139
        }
140
141
        return null;
142
    }
143
}
144