|
1
|
|
|
<?php namespace nyx\console\terminals; |
|
2
|
|
|
|
|
3
|
|
|
// Internal dependencies |
|
4
|
|
|
use nyx\console; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Windows Terminal |
|
8
|
|
|
* |
|
9
|
|
|
* @package Nyx\Console |
|
10
|
|
|
* @version 0.1.0 |
|
11
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
12
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
13
|
|
|
* @link https://github.com/unyx/nyx |
|
14
|
|
|
*/ |
|
15
|
|
|
class Windows extends console\Terminal |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritDoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function getDimensions() : ?array |
|
21
|
|
|
{ |
|
22
|
|
|
// Ansicon environmental variable is our first fallback. Result of 'mode CON' is the next one. |
|
23
|
|
|
if (null === $dimensions = $this->getDimensionsFromAnsicon()) { |
|
24
|
|
|
$dimensions = $this->getDimensionsFromMode(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// If we got valid dimensions from any of our fallbacks, let's store the results. |
|
28
|
|
|
if (null !== $dimensions) { |
|
29
|
|
|
$this->dimensions = $dimensions; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this->dimensions; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Attempts to determine the Terminal's dimensions based on the 'ANSICON' environmental variables. |
|
37
|
|
|
* |
|
38
|
|
|
* @return array Either an array containing two keys - 'width' and 'height' or null if the data couldn't |
|
|
|
|
|
|
39
|
|
|
* be parsed to retrieve anything useful. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getDimensionsFromAnsicon() : ?array |
|
42
|
|
|
{ |
|
43
|
|
|
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) { |
|
44
|
|
|
return [ |
|
45
|
|
|
'width' => (int) $matches[1], |
|
46
|
|
|
'height' => (int) ($matches[4] ?? $matches[2]) |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Attempts to determine the Terminal's dimensions based on the result of a 'mode CON' system call. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array Either an array containing two keys - 'width' and 'height' or null if the data couldn't |
|
|
|
|
|
|
57
|
|
|
* be parsed to retrieve anything useful. |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getDimensionsFromMode() : ?array |
|
60
|
|
|
{ |
|
61
|
|
|
if (empty($output = $this->execute('mode CON'))) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $output, $matches)) { |
|
66
|
|
|
return [ |
|
67
|
|
|
'width' => (int) $matches[2], |
|
68
|
|
|
'height'=> (int) $matches[1] |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.