Skip to content

Commit 171f075

Browse files
committed
Fix hexeditor example
1 parent 2514075 commit 171f075

File tree

3 files changed

+383
-6
lines changed

3 files changed

+383
-6
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"license": "MIT",
66
"require": {
77
"php": "^8.1",
8-
"revolt/event-loop": "^1.0",
9-
"aspectus/terminal": "^0.1"
8+
"aspectus/terminal": "^0.1.2",
9+
"revolt/event-loop": "^1.0"
1010
},
1111
"autoload": {
1212
"psr-4": {
Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
<?php
2+
3+
class StyleBuilder
4+
{
5+
private const CSI_PLACEHOLDER = '%CSI%';
6+
private const TERMINATOR = 'm';
7+
8+
/** @var string[] */
9+
private array $modifiers = [];
10+
11+
public function build(bool $s8mode = false, bool $reset = true): string
12+
{
13+
$csi = $s8mode ? "\x9b" : "\x1b[";
14+
15+
$sequenceState = false;
16+
17+
$seq = '';
18+
foreach ($this->modifiers as $entry) {
19+
if (array_key_exists('data', $entry)) {
20+
if ($sequenceState) {
21+
$seq .= 'm';
22+
}
23+
$sequenceState = false;
24+
$seq .= $entry['data'];
25+
continue;
26+
}
27+
28+
if (!$sequenceState) {
29+
$sequenceState = true;
30+
$seq .= $csi . $entry['sequence'];
31+
continue;
32+
}
33+
34+
$seq .= ';' . $entry['sequence'];
35+
}
36+
37+
if ($sequenceState) {
38+
$seq .= 'm';
39+
}
40+
41+
if ($reset) {
42+
$this->reset();
43+
}
44+
45+
return $seq;
46+
}
47+
48+
/**
49+
* Build a callable that will accept placeholders as its arguments.
50+
* Useful when you use `printf` placeholders in between sequences;
51+
* To add in between sequences use the `write()` method
52+
*
53+
* @param bool $s8mode
54+
* @param bool $reset
55+
* @return callable
56+
*/
57+
public function buildCallable(bool $s8mode = false, bool $reset = true): callable
58+
{
59+
$sequence = $this->build($s8mode, $reset);
60+
61+
return function () use (&$sequence) {
62+
return sprintf($sequence, ...func_get_args());
63+
};
64+
}
65+
66+
public function reset(): self
67+
{
68+
$this->modifiers = [];
69+
return $this;
70+
}
71+
72+
public function write(string $data): self
73+
{
74+
$this->modifiers[] = ['data' => $data];
75+
return $this;
76+
}
77+
78+
protected function add(string $data): self
79+
{
80+
$this->modifiers[] = ['sequence' => $data];
81+
return $this;
82+
}
83+
84+
public function normal(): self
85+
{
86+
return $this->add("0");
87+
}
88+
89+
public function bold(): self
90+
{
91+
return $this->add("1");
92+
}
93+
94+
public function faint(): self
95+
{
96+
return $this->add("2");
97+
}
98+
99+
public function italic(): self
100+
{
101+
return $this->add("3");
102+
}
103+
104+
public function underline(): self
105+
{
106+
return $this->add("4");
107+
}
108+
109+
public function blink(): self
110+
{
111+
return $this->add("5");
112+
}
113+
114+
public function inverse(): self
115+
{
116+
return $this->add("7");
117+
}
118+
119+
public function invisible(): self
120+
{
121+
return $this->add("8");
122+
}
123+
124+
public function strikethrough(): self
125+
{
126+
return $this->add("9");
127+
}
128+
129+
public function doubleUnderline(): self
130+
{
131+
return $this->add("21");
132+
}
133+
134+
public function notBoldNotFaint(): self
135+
{
136+
return $this->add("22");
137+
}
138+
139+
public function notItalic(): self
140+
{
141+
return $this->add("23");
142+
}
143+
144+
public function notUnderline(): self
145+
{
146+
return $this->add("24");
147+
}
148+
149+
public function notBlink(): self
150+
{
151+
return $this->add("25");
152+
}
153+
154+
public function steady(): self
155+
{
156+
return $this->add("25");
157+
}
158+
159+
public function notInverse(): self
160+
{
161+
return $this->add("27");
162+
}
163+
164+
public function positive(): self
165+
{
166+
return $this->add("27");
167+
}
168+
169+
public function notInvisible(): self
170+
{
171+
return $this->add("28");
172+
}
173+
174+
public function visible(): self
175+
{
176+
return $this->add("28");
177+
}
178+
179+
public function notStrikethrough(): self
180+
{
181+
return $this->add("29");
182+
}
183+
184+
public function black(): self
185+
{
186+
return $this->add("30");
187+
}
188+
189+
public function red(): self
190+
{
191+
return $this->add("31");
192+
}
193+
194+
public function green(): self
195+
{
196+
return $this->add("32");
197+
}
198+
199+
public function yellow(): self
200+
{
201+
return $this->add("33");
202+
}
203+
204+
public function blue(): self
205+
{
206+
return $this->add("34");
207+
}
208+
209+
public function magenta(): self
210+
{
211+
return $this->add("35");
212+
}
213+
214+
public function cyan(): self
215+
{
216+
return $this->add("36");
217+
}
218+
219+
public function white(): self
220+
{
221+
return $this->add("37");
222+
}
223+
224+
public function default(): self
225+
{
226+
return $this->add("39");
227+
}
228+
229+
public function bgBlack(): self
230+
{
231+
return $this->add("40");
232+
}
233+
234+
public function bgRed(): self
235+
{
236+
return $this->add("41");
237+
}
238+
239+
public function bgGreen(): self
240+
{
241+
return $this->add("42");
242+
}
243+
244+
public function bgYellow(): self
245+
{
246+
return $this->add("43");
247+
}
248+
249+
public function bgBlue(): self
250+
{
251+
return $this->add("44");
252+
}
253+
254+
public function bgMagenta(): self
255+
{
256+
return $this->add("45");
257+
}
258+
259+
public function bgCyan(): self
260+
{
261+
return $this->add("46");
262+
}
263+
264+
public function bgWhite(): self
265+
{
266+
return $this->add("47");
267+
}
268+
269+
public function bgDefault(): self
270+
{
271+
return $this->add("49");
272+
}
273+
274+
public function brightBlack(): self
275+
{
276+
return $this->add("90");
277+
}
278+
279+
public function brightRed(): self
280+
{
281+
return $this->add("91");
282+
}
283+
284+
public function brightGreen(): self
285+
{
286+
return $this->add("92");
287+
}
288+
289+
public function brightYellow(): self
290+
{
291+
return $this->add("93");
292+
}
293+
294+
public function brightBlue(): self
295+
{
296+
return $this->add("94");
297+
}
298+
299+
public function brightMagenta(): self
300+
{
301+
return $this->add("95");
302+
}
303+
304+
public function brightCyan(): self
305+
{
306+
return $this->add("96");
307+
}
308+
309+
public function brightWhite(): self
310+
{
311+
return $this->add("97");
312+
}
313+
314+
public function brightBgBlack(): self
315+
{
316+
return $this->add("100");
317+
}
318+
319+
public function brightBgRed(): self
320+
{
321+
return $this->add("101");
322+
}
323+
324+
public function brightBgGreen(): self
325+
{
326+
return $this->add("102");
327+
}
328+
329+
public function brightBgYellow(): self
330+
{
331+
return $this->add("103");
332+
}
333+
334+
public function brightBgBlue(): self
335+
{
336+
return $this->add("104");
337+
}
338+
339+
public function brightBgMagenta(): self
340+
{
341+
return $this->add("105");
342+
}
343+
344+
public function brightBgCyan(): self
345+
{
346+
return $this->add("106");
347+
}
348+
349+
public function brightBgWhite(): self
350+
{
351+
return $this->add("107");
352+
}
353+
354+
public function fgi(int $color): self
355+
{
356+
return $this->add("38;5;$color");
357+
}
358+
359+
public function bgi(int $color): self
360+
{
361+
return $this->add("48;5;$color");
362+
}
363+
364+
public function rgb(int $red, int $green, int $blue): self
365+
{
366+
return $this->add("38;2;$red;$green;$blue");
367+
}
368+
369+
public function bgRgb(int $red, int $green, int $blue): self
370+
{
371+
return $this->add("48;2;$red;$green;$blue");
372+
}
373+
}

0 commit comments

Comments
 (0)