Passed
Pull Request — master (#116)
by Théo
02:03
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 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
13
namespace KevinGH\RequirementChecker;
14
15
/**
16
 * 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
17
 * class.
18
 */
19
class Terminal
20
{
21
    private static $width;
22
    private static $height;
23
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
36
        if (null === self::$width) {
37
            self::initDimensions();
38
        }
39
40
        return self::$width ?: 80;
41
    }
42
43
    /**
44
     * Gets the terminal height.
45
     *
46
     * @return int
47
     */
48
    public function getHeight()
49
    {
50
        $height = getenv('LINES');
51
        if (false !== $height) {
52
            return (int) trim($height);
53
        }
54
55
        if (null === self::$height) {
56
            self::initDimensions();
57
        }
58
59
        return self::$height ?: 50;
60
    }
61
62
    private static function initDimensions()
63
    {
64
        if ('\\' === DIRECTORY_SEPARATOR) {
65
            if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
66
                // extract [w, H] from "wxh (WxH)"
67
                // or [w, h] from "wxh"
68
                self::$width = (int) $matches[1];
69
                self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
70
            } elseif (null !== $dimensions = self::getConsoleMode()) {
71
                // extract [w, h] from "wxh"
72
                self::$width = (int) $dimensions[0];
73
                self::$height = (int) $dimensions[1];
74
            }
75
        } elseif ($sttyString = self::getSttyColumns()) {
76
            if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
77
                // extract [w, h] from "rows h; columns w;"
78
                self::$width = (int) $matches[2];
79
                self::$height = (int) $matches[1];
80
            } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
81
                // extract [w, h] from "; h rows; w columns"
82
                self::$width = (int) $matches[2];
83
                self::$height = (int) $matches[1];
84
            }
85
        }
86
    }
87
88
    /**
89
     * Runs and parses mode CON if it's available, suppressing any error output.
90
     *
91
     * @return null|int[] An array composed of the width and the height or null if it could not be parsed
92
     */
93
    private static function getConsoleMode()
94
    {
95
        if (!function_exists('proc_open')) {
96
            return;
97
        }
98
99
        $descriptorspec = array(
100
            1 => array('pipe', 'w'),
101
            2 => array('pipe', 'w'),
102
        );
103
        $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
104
        if (is_resource($process)) {
105
            $info = stream_get_contents($pipes[1]);
106
            fclose($pipes[1]);
107
            fclose($pipes[2]);
108
            proc_close($process);
109
110
            if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
111
                return array((int) $matches[2], (int) $matches[1]);
112
            }
113
        }
114
    }
115
116
    /**
117
     * Runs and parses stty -a if it's available, suppressing any error output.
118
     *
119
     * @return null|string
120
     */
121
    private static function getSttyColumns()
122
    {
123
        if (!function_exists('proc_open')) {
124
            return;
125
        }
126
127
        $descriptorspec = array(
128
            1 => array('pipe', 'w'),
129
            2 => array('pipe', 'w'),
130
        );
131
132
        $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
133
        if (is_resource($process)) {
134
            $info = stream_get_contents($pipes[1]);
135
            fclose($pipes[1]);
136
            fclose($pipes[2]);
137
            proc_close($process);
138
139
            return $info;
140
        }
141
142
        return null;
143
    }
144
}
145