-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_backbuffer.php
140 lines (112 loc) · 4.01 KB
/
cli_backbuffer.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
137
138
139
140
<?php
declare(strict_types=1);
final class IndexOutOfRangeError extends Exception {}
class CLIBackbuffer
{
private array $char_map = [];
private int $x_pos = 0;
private int $y_pos = 0;
public function __construct(
public readonly int $x_cols = 80,
public readonly int $y_rows = 25,
private string $prefill_char = ''
) {
for ($y = 0; $y < $y_rows; $y++) {
for ($x = 0; $x < $x_cols; $x++) {
$this->char_map[self::makeInternalIdx(x: $x, y: $y)] = $prefill_char;
}
}
}
private static function makeInternalIdx(int $x, int $y): string
{
return $y . ',' . $x;
}
private function getInternalIdx(): string
{
return self::makeInternalIdx(x: $this->x_pos, y: $this->y_pos);
}
public function getPos(): array
{
return ['x' => $this->x_pos, 'y' => $this->y_pos];
}
public function setPos(int $x_pos, int $y_pos): void
{
if ($x_pos >= 0 && $x_pos < $this->x_cols) {
$this->x_pos = $x_pos;
} else {
throw new IndexOutOfRangeError('x-pos is out of range: ' . $x_pos);
}
if ($y_pos >= 0 && $y_pos < $this->y_rows) {
$this->y_pos = $y_pos;
} else {
throw new IndexOutOfRangeError('y-pos is out of range: ' . $y_pos);
}
}
public function placeChar(int $x_pos, int $y_pos, string $char): void
{
if (mb_strlen($char) > 1) throw new InvalidArgumentException('Char is only allowed for placeChar');
$this->char_map[self::makeInternalIdx(x: $x_pos, y: $y_pos)] = $char;
}
public function writeChar(string $char = ''): void
{
if (in_array(mb_strlen($char), [0, 1], true)) {
$this->char_map[$this->getInternalIdx()] = $char;
} else {
throw new InvalidArgumentException('char must have a length of 0 or 1');
}
}
public function getRemCharsRow(): int
{
return $this->x_cols - ($this->x_pos + 1);
}
public function writeString(string $string = ''): void
{
if (mb_strlen($string) > 0) {
$str_idx = 0;
while (true) {
$rem_chars_row = $this->getRemCharsRow();
$rem_chars_str = mb_strlen($string) - $str_idx;
if ($rem_chars_str === 0) break;
if ($rem_chars_row === 0) {
if ($this->y_pos + 1 === $this->y_rows) break;
$this->breakline();
$rem_chars_row = $this->getRemCharsRow();
}
$shortest_collision = min($rem_chars_row, $rem_chars_str);
if ($this->x_pos === $this->x_cols - 1 && $this->y_pos < $this->y_rows - 1) {
$this->breakline();
}
for ($i = 0; $i < $shortest_collision; $i++) {
$this->char_map[$this->getInternalIdx()] = mb_substr($string, $str_idx++, 1);
$this->setPos($this->x_pos + 1, $this->y_pos);
}
if ($shortest_collision === $rem_chars_row) {
if ($this->y_pos + 1 === $this->y_rows) break;
$this->breakline();
}
}
} else {
throw new InvalidArgumentException("Empty string provided");
}
}
public function breakline(): void
{
$this->setPos(0, $this->y_pos + 1);
}
public function writeLine(string $string = ''): void
{
$this->writeString($string);
$this->breakline();
}
public function writeout(): string
{
$out_string = '';
for ($y = 0; $y < $this->y_rows - 1; $y++) {
for ($x = 0; $x < $this->x_cols - 1; $x++) {
$out_string .= $this->char_map[self::makeInternalIdx(x: $x, y: $y)];
}
$out_string .= PHP_EOL;
}
return $out_string;
}
}