-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSttyWrapper.php
executable file
·136 lines (123 loc) · 2.66 KB
/
SttyWrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace MaplePHP\Prompts;
/**
* Class SttyWrapper
* @package MaplePHP\Prompts
*/
class SttyWrapper
{
protected array $command = [];
public function __construct()
{
}
/**
* Get command as string
*
* @return string
*/
public function __toString(): string
{
return $this->get();
}
/**
* Get command as string
*
* @return string
*/
public function get(): string
{
return implode(";", $this->command);
}
/**
* Masking input
*
* @return self
*/
public function maskInput(): self
{
return $this->toggleEcho(false)->readInput()->toggleEcho(true)->raw('echo $input');
}
/**
* Turn on/off output stream
*
* @param bool $bool
* @return self
*/
public function toggleEcho(bool $bool): self
{
return $this->toggleEnable($bool, "echo");
}
/**
* Toggle character break mode
*
* @param bool $bool
* @return self
*/
public function toggleCharBreakMode(bool $bool): self
{
return $this->toggleEnable($bool, "cbreak");
}
/**
* Will listen to the input
*
* @return self
*/
public function readInput(): self
{
return $this->raw('read input');
}
/**
* Toggle a custom command on/off
*
* @param bool $bool
* @param string $command
* @return self
*/
public function toggleEnable(bool $bool, string $command): self
{
return $this->raw('stty ' . (!$bool ? '-' : '') . $command);
}
/**
* Execute a raw command
*
* @param string $input
* @return self
*/
public function raw(string $input): self
{
$inst = clone $this;
$inst->command[] = $input;
return $inst;
}
/**
* Check if the OS is Unix-based
*
* @return bool
*/
public function isUnix(): bool
{
$os = php_uname('s');
$supportedOSes = ['Linux', 'Unix', 'Darwin'];
foreach ($supportedOSes as $supportedOS) {
if (stripos($os, $supportedOS) !== false) {
return true;
}
}
return false;
}
/**
* Check if stty is supported
*
* @return bool
*/
public function hasSttySupport(): bool
{
// Make sure it really is installed
// Can be absent on specialized UNIX environments (e.g. minimalistic or embedded system)
if (function_exists("exec") && $this->isUnix()) {
exec('stty -a 2>&1', $output, $returnStatus);
return ($returnStatus === 0);
}
return false;
}
}