-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleOutput.php
More file actions
158 lines (134 loc) · 3.65 KB
/
ConsoleOutput.php
File metadata and controls
158 lines (134 loc) · 3.65 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
namespace PhpOpcua\Cli\Output;
/**
* Human-readable console output with ANSI colors and tree rendering.
*/
class ConsoleOutput implements OutputInterface
{
private bool $colorEnabled;
/**
* @param resource $stdout
* @param resource $stderr
* @param ?bool $forceColor
*/
public function __construct(
private $stdout = STDOUT,
private $stderr = STDERR,
?bool $forceColor = null,
) {
$this->colorEnabled = $forceColor ?? $this->detectColor();
}
/**
* {@inheritDoc}
*/
public function writeln(string $message): void
{
fwrite($this->stdout, $message . "\n");
}
/**
* {@inheritDoc}
*/
public function write(string $message): void
{
fwrite($this->stdout, $message);
}
/**
* {@inheritDoc}
*/
public function error(string $message): void
{
fwrite($this->stderr, $this->color($message, '31') . "\n");
}
/**
* {@inheritDoc}
*/
public function data(array $data): void
{
$maxKeyLen = 0;
foreach ($data as $key => $value) {
$maxKeyLen = max($maxKeyLen, strlen($key));
}
foreach ($data as $key => $value) {
$paddedKey = str_pad($key . ':', $maxKeyLen + 1);
$this->writeln($this->color($paddedKey, '36') . ' ' . $this->formatValue($value));
}
}
/**
* {@inheritDoc}
*/
public function table(array $rows): void
{
if (empty($rows)) {
return;
}
foreach ($rows as $row) {
$this->writeln('');
foreach ($row as $key => $value) {
$this->writeln($this->color($key . ':', '36') . ' ' . $this->formatValue($value));
}
}
}
/**
* {@inheritDoc}
*/
public function tree(array $nodes, string $prefix = ''): void
{
$count = count($nodes);
foreach ($nodes as $i => $node) {
$isLast = ($i === $count - 1);
$connector = $isLast ? '└── ' : '├── ';
$childPrefix = $isLast ? ' ' : '│ ';
$line = $prefix . $connector
. $this->color($node['name'], '37')
. ' ' . $this->color('(' . $node['nodeId'] . ')', '90')
. ' ' . $this->color('[' . $node['class'] . ']', '33');
$this->writeln($line);
if (! empty($node['children'])) {
$this->tree($node['children'], $prefix . $childPrefix);
}
}
}
/**
* @param mixed $value
* @return string
*/
private function formatValue(mixed $value): string
{
if ($value === null) {
return $this->color('null', '90');
}
if (is_bool($value)) {
return $value ? $this->color('true', '32') : $this->color('false', '31');
}
if (is_array($value)) {
return implode(', ', $value);
}
return (string) $value;
}
/**
* @param string $text
* @param string $code
* @return string
*/
private function color(string $text, string $code): string
{
if (! $this->colorEnabled) {
return $text;
}
return "\033[{$code}m{$text}\033[0m";
}
/**
* @return bool
*/
private function detectColor(): bool
{
if (getenv('NO_COLOR') !== false) {
return false;
}
if (function_exists('posix_isatty') && is_resource($this->stdout)) {
return posix_isatty($this->stdout);
}
return getenv('TERM') !== false && getenv('TERM') !== 'dumb';
}
}