|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/console-diff-renderer. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/console-diff-renderer/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/console-diff-renderer |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DiffRenderer\Wrap; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DiffRenderer\Terminal\TerminalInterface; |
|
17
|
|
|
|
|
18
|
|
|
class Wrapper |
|
19
|
|
|
{ |
|
20
|
|
|
const REPLACEMENT_CHAR = "\6"; |
|
21
|
|
|
|
|
22
|
|
|
/** @var TerminalInterface */ |
|
23
|
|
|
private $terminal; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Wrapper constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param TerminalInterface $terminal |
|
29
|
|
|
*/ |
|
30
|
25 |
|
public function __construct(TerminalInterface $terminal) |
|
31
|
|
|
{ |
|
32
|
25 |
|
$this->terminal = $terminal; |
|
33
|
25 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return int |
|
37
|
|
|
*/ |
|
38
|
14 |
|
public function getWidth() |
|
39
|
|
|
{ |
|
40
|
14 |
|
return $this->terminal->getWidth(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string[] $input |
|
45
|
|
|
* |
|
46
|
|
|
* @return string[] |
|
47
|
|
|
*/ |
|
48
|
15 |
|
public function wrap(array $input) |
|
49
|
|
|
{ |
|
50
|
15 |
|
$out = []; |
|
51
|
15 |
|
foreach ($input as $line) { |
|
52
|
15 |
|
foreach ($this->chunk($line) as $new) { |
|
53
|
15 |
|
$out[] = $new; |
|
54
|
15 |
|
} |
|
55
|
15 |
|
} |
|
56
|
15 |
|
return $out; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string[] $input |
|
61
|
|
|
* |
|
62
|
|
|
* @return string[] |
|
63
|
|
|
*/ |
|
64
|
7 |
|
public function trim(array $input) |
|
65
|
|
|
{ |
|
66
|
7 |
|
$out = []; |
|
67
|
7 |
|
foreach ($input as $line) { |
|
68
|
7 |
|
$chunk = $this->chunk($line); |
|
69
|
7 |
|
$out[] = reset($chunk); |
|
70
|
7 |
|
} |
|
71
|
7 |
|
return $out; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $line |
|
76
|
|
|
* |
|
77
|
|
|
* @return string[] |
|
78
|
|
|
*/ |
|
79
|
22 |
|
private function chunk($line) |
|
80
|
|
|
{ |
|
81
|
22 |
|
$width = $this->getWidth(); |
|
82
|
22 |
|
if (mb_strlen($line) <= $width) { |
|
83
|
9 |
|
return [$line]; |
|
84
|
|
|
} |
|
85
|
13 |
|
$stripped = $this->terminal->filter($line, static::REPLACEMENT_CHAR); |
|
86
|
13 |
|
$offset = 0; |
|
87
|
13 |
|
$out = []; |
|
88
|
|
|
|
|
89
|
13 |
|
for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $i++) { |
|
90
|
13 |
|
if (mb_substr($stripped, $i, 1) != static::REPLACEMENT_CHAR) { |
|
91
|
13 |
|
if ($j > 0 && $j % $width == 0) { |
|
92
|
13 |
|
$out[] = mb_substr($line, $offset, $i - $offset); |
|
93
|
13 |
|
$offset = $i; |
|
94
|
13 |
|
} |
|
95
|
13 |
|
$j++; |
|
96
|
13 |
|
} |
|
97
|
13 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
// return any remaining entries |
|
100
|
13 |
|
if ($offset != $i - 1) { |
|
101
|
11 |
|
$out[] = mb_substr($line, $offset); |
|
102
|
11 |
|
} |
|
103
|
13 |
|
return $out; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|