1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\CLImate\Util\System; |
4
|
|
|
|
5
|
|
|
use function getenv; |
6
|
|
|
|
7
|
|
|
class Linux extends System |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Get the width of the terminal |
11
|
|
|
* |
12
|
8 |
|
* @return integer|null |
13
|
|
|
*/ |
14
|
8 |
|
public function width() |
15
|
|
|
{ |
16
|
|
|
return $this->getDimension($this->tput("cols")); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the height of the terminal |
21
|
|
|
* |
22
|
8 |
|
* @return integer|null |
23
|
|
|
*/ |
24
|
8 |
|
public function height() |
25
|
|
|
{ |
26
|
|
|
return $this->getDimension($this->tput("lines")); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get a value from the tput command. |
31
|
|
|
* |
32
|
|
|
* @param string $type |
33
|
|
|
* |
34
|
|
|
* @return array|null|string |
35
|
|
|
*/ |
36
|
|
|
private function tput($type) |
37
|
|
|
{ |
38
|
|
|
return $this->exec("tput {$type} 2>/dev/null"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determine if system has access to bash commands |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function canAccessBash() |
47
|
|
|
{ |
48
|
|
|
return (rtrim($this->exec("/usr/bin/env bash -c 'echo OK'")) === 'OK'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Display a hidden response prompt and return the response |
53
|
|
|
* |
54
|
|
|
* @param string $prompt |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
16 |
|
public function hiddenResponsePrompt($prompt) |
59
|
|
|
{ |
60
|
16 |
|
$bash_command = 'read -s -p "' . $prompt . '" response && echo $response'; |
61
|
|
|
|
62
|
|
|
return rtrim($this->exec("/usr/bin/env bash -c '{$bash_command}'")); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Determine if dimension is numeric and return it |
67
|
|
|
* |
68
|
|
|
* @param integer|string|null $dimension |
69
|
|
|
* |
70
|
|
|
* @return integer|null |
71
|
|
|
*/ |
72
|
|
|
protected function getDimension($dimension) |
73
|
|
|
{ |
74
|
|
|
return (is_numeric($dimension)) ? $dimension : null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check if the stream supports ansi escape characters. |
79
|
|
|
* |
80
|
|
|
* Based on https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/StreamOutput.php |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function systemHasAnsiSupport() |
85
|
|
|
{ |
86
|
|
|
if ('Hyper' === getenv('TERM_PROGRAM')) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$stream = STDOUT; |
91
|
|
|
|
92
|
|
|
if (function_exists('stream_isatty')) { |
93
|
|
|
return @stream_isatty($stream); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (function_exists('posix_isatty')) { |
97
|
|
|
return @posix_isatty($stream); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$stat = @fstat($stream); |
101
|
|
|
// Check if formatted mode is S_IFCHR |
102
|
|
|
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.