1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace terminal; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* class for terminal/cli/bash output |
7
|
|
|
*/ |
8
|
|
|
final class Terminal { |
9
|
|
|
private $buffer = []; |
10
|
|
|
|
11
|
|
|
public function addBuffer(string $buffer) |
12
|
|
|
{ |
13
|
|
|
$this->buffer[] = $buffer; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getBuffer(): string |
17
|
|
|
{ |
18
|
|
|
return implode(PHP_EOL, $this->buffer); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function echo(string $string, bool $to_buffer = false, bool $return = false) |
22
|
|
|
{ |
23
|
|
|
if ($return) { |
24
|
|
|
return $string; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ($to_buffer) { |
28
|
|
|
$this->addBuffer($string); |
29
|
|
|
} else { |
30
|
|
|
echo $string . PHP_EOL; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function diff(string $string, bool $to_buffer = false, bool $return = false) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$result_string = ''; |
37
|
|
|
$separator = "\r\n"; |
38
|
|
|
$line = strtok($string, $separator); |
39
|
|
|
|
40
|
|
|
while ($line !== false) { |
41
|
|
|
if (strpos($line, '-') === 0) { |
42
|
|
|
$result_string .= $this->error($line, false, true); |
43
|
|
|
} else if(strpos($line, '+') === 0) { |
44
|
|
|
$result_string .= $this->success($line, false, true); |
45
|
|
|
} else { |
46
|
|
|
$result_string .= $line; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$result_string .= PHP_EOL; |
50
|
|
|
|
51
|
|
|
$line = strtok($separator); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($to_buffer) { |
55
|
|
|
$this->setBuffer($result_string); |
|
|
|
|
56
|
|
|
} else { |
57
|
|
|
echo $result_string; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function info(string $string, bool $to_buffer = false, bool $return = false) |
62
|
|
|
{ |
63
|
|
|
$output = "\e[46m" . $string . "\e[0m"; |
64
|
|
|
|
65
|
|
|
if ($return) { |
66
|
|
|
return $output; |
67
|
|
|
} else { |
68
|
|
|
$this->echo($output, $to_buffer); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function success(string $string, bool $to_buffer = false, bool $return = false) |
73
|
|
|
{ |
74
|
|
|
$output = "\e[32m" . $string . "\e[0m"; |
75
|
|
|
|
76
|
|
|
if ($return) { |
77
|
|
|
return $output; |
78
|
|
|
} else { |
79
|
|
|
$this->echo($output, $to_buffer); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function warning(string $string, bool $to_buffer = false, bool $return = false) |
84
|
|
|
{ |
85
|
|
|
$output = "\e[43m" . $string . "\e[0m"; |
86
|
|
|
|
87
|
|
|
if ($return) { |
88
|
|
|
return $output; |
89
|
|
|
} else { |
90
|
|
|
$this->echo($output, $to_buffer); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function error(string $string, bool $to_buffer = false, bool $return = false) |
95
|
|
|
{ |
96
|
|
|
$output = "\e[1;31m" . $string . "\e[0m"; |
97
|
|
|
|
98
|
|
|
if ($return) { |
99
|
|
|
return $output; |
100
|
|
|
} else { |
101
|
|
|
$this->echo($output, $to_buffer); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function forceOutput() |
106
|
|
|
{ |
107
|
|
|
$this->echo( |
108
|
|
|
$this->getBuffer() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.