Skip to content

Commit 3f2ab1e

Browse files
authored
Merge pull request #104 from Lynesth/patch-17
Modification of colours rendering + 256 colours support
2 parents 06534f7 + be09a76 commit 3f2ab1e

File tree

10 files changed

+578
-123
lines changed

10 files changed

+578
-123
lines changed

src/CliMenu.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,22 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
353353
{
354354
$rows = $item->getRows($this->style, $selected);
355355

356-
$setColour = $selected
357-
? $this->style->getSelectedSetCode()
358-
: $this->style->getUnselectedSetCode();
356+
$setColour = $this->style->getColoursSetCode();
357+
$resetColour = $this->style->getColoursResetCode();
358+
$invertedColour = $selected
359+
? $this->style->getInvertedColoursSetCode()
360+
: '';
359361

360-
$unsetColour = $selected
361-
? $this->style->getSelectedUnsetCode()
362-
: $this->style->getUnselectedUnsetCode();
363-
364-
return array_map(function ($row) use ($setColour, $unsetColour) {
362+
return array_map(function ($row) use ($setColour, $invertedColour, $resetColour) {
365363
return sprintf(
366-
"%s%s%s%s%s%s%s\n",
364+
"%s%s%s%s%s%s%s%s\n",
367365
str_repeat(' ', $this->style->getMargin()),
368366
$setColour,
367+
$invertedColour,
369368
str_repeat(' ', $this->style->getPadding()),
370369
$row,
371370
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
372-
$unsetColour,
371+
$resetColour,
373372
str_repeat(' ', $this->style->getMargin())
374373
);
375374
}, $rows);

src/CliMenuBuilder.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpSchool\CliMenu\MenuItem\SelectableItem;
1212
use PhpSchool\CliMenu\MenuItem\StaticItem;
1313
use PhpSchool\CliMenu\Terminal\TerminalFactory;
14+
use PhpSchool\CliMenu\Util\ColourUtil;
1415
use Assert\Assertion;
1516
use PhpSchool\Terminal\Terminal;
1617
use RuntimeException;
@@ -198,20 +199,24 @@ public function setExitButtonText(string $exitButtonText) : self
198199
return $this;
199200
}
200201

201-
public function setBackgroundColour(string $colour) : self
202+
public function setBackgroundColour($colour, string $fallback = null) : self
202203
{
203-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
204-
205-
$this->style['bg'] = $colour;
204+
$this->style['bg'] = ColourUtil::validateColour(
205+
$this->terminal,
206+
$colour,
207+
$fallback
208+
);
206209

207210
return $this;
208211
}
209212

210-
public function setForegroundColour(string $colour) : self
213+
public function setForegroundColour($colour, string $fallback = null) : self
211214
{
212-
Assertion::inArray($colour, MenuStyle::getAvailableColours());
213-
214-
$this->style['fg'] = $colour;
215+
$this->style['fg'] = ColourUtil::validateColour(
216+
$this->terminal,
217+
$colour,
218+
$fallback
219+
);
215220

216221
return $this;
217222
}

src/Dialogue/Confirm.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function display(string $confirmText = 'OK') : void
2626

2727
$this->write(sprintf(
2828
"%s%s%s%s%s\n",
29-
$this->style->getUnselectedSetCode(),
29+
$this->style->getColoursSetCode(),
3030
str_repeat(' ', $this->style->getPadding()),
3131
$this->text,
3232
str_repeat(' ', $this->style->getPadding()),
33-
$this->style->getUnselectedUnsetCode()
33+
$this->style->getColoursResetCode()
3434
));
3535

3636
$this->emptyRow();
@@ -39,25 +39,23 @@ public function display(string $confirmText = 'OK') : void
3939
$leftFill = ($promptWidth / 2) - (mb_strlen($confirmText) / 2);
4040

4141
$this->write(sprintf(
42-
"%s%s%s%s%s%s%s%s%s\n",
43-
$this->style->getUnselectedSetCode(),
42+
"%s%s%s%s%s%s%s\n",
43+
$this->style->getColoursSetCode(),
4444
str_repeat(' ', $leftFill),
45-
$this->style->getUnselectedUnsetCode(),
46-
$this->style->getSelectedSetCode(),
45+
$this->style->getInvertedColoursSetCode(),
4746
$confirmText,
48-
$this->style->getSelectedUnsetCode(),
49-
$this->style->getUnselectedSetCode(),
47+
$this->style->getInvertedColoursUnsetCode(),
5048
str_repeat(' ', ceil($promptWidth - $leftFill - mb_strlen($confirmText))),
51-
$this->style->getUnselectedUnsetCode()
49+
$this->style->getColoursResetCode()
5250
));
5351

5452
$this->write(sprintf(
5553
"%s%s%s%s%s\n",
56-
$this->style->getUnselectedSetCode(),
54+
$this->style->getColoursSetCode(),
5755
str_repeat(' ', $this->style->getPadding()),
5856
str_repeat(' ', mb_strlen($this->text)),
5957
str_repeat(' ', $this->style->getPadding()),
60-
$this->style->getUnselectedUnsetCode()
58+
$this->style->getColoursResetCode()
6159
));
6260

6361
$this->terminal->moveCursorToTop();

src/Dialogue/Dialogue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ protected function emptyRow() : void
8686
$this->write(
8787
sprintf(
8888
"%s%s%s%s%s\n",
89-
$this->style->getUnselectedSetCode(),
89+
$this->style->getColoursSetCode(),
9090
str_repeat(' ', $this->style->getPadding()),
9191
str_repeat(' ', mb_strlen($this->text)),
9292
str_repeat(' ', $this->style->getPadding()),
93-
$this->style->getUnselectedUnsetCode()
93+
$this->style->getColoursResetCode()
9494
)
9595
);
9696
}

src/Dialogue/Flash.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public function display() : void
2323

2424
$this->write(sprintf(
2525
"%s%s%s%s%s\n",
26-
$this->style->getUnselectedSetCode(),
26+
$this->style->getColoursSetCode(),
2727
str_repeat(' ', $this->style->getPadding()),
2828
$this->text,
2929
str_repeat(' ', $this->style->getPadding()),
30-
$this->style->getUnselectedUnsetCode()
30+
$this->style->getColoursResetCode()
3131
));
3232

3333
$this->emptyRow();

src/Input/InputIO.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ private function drawLine(Input $input, string $userInput, string $text) : void
143143

144144
$line = sprintf(
145145
"%s%s%s%s%s\n",
146-
$input->getStyle()->getUnselectedSetCode(),
146+
$input->getStyle()->getColoursSetCode(),
147147
str_repeat(' ', $input->getStyle()->getPadding()),
148148
$text,
149149
str_repeat(' ', $input->getStyle()->getPadding()),
150-
$input->getStyle()->getUnselectedUnsetCode()
150+
$input->getStyle()->getColoursResetCode()
151151
);
152152

153153
$this->terminal->write($line);
@@ -240,12 +240,10 @@ private function drawInputField(Input $input, string $userInput) : void
240240
$input,
241241
$userInput,
242242
sprintf(
243-
'%s%s%s%s%s',
244-
$input->getStyle()->getUnselectedUnsetCode(),
245-
$input->getStyle()->getSelectedSetCode(),
243+
'%s%s%s',
244+
$input->getStyle()->getInvertedColoursSetCode(),
246245
$userInput,
247-
$input->getStyle()->getSelectedUnsetCode(),
248-
$input->getStyle()->getUnselectedSetCode()
246+
$input->getStyle()->getInvertedColoursUnsetCode()
249247
)
250248
);
251249
}

0 commit comments

Comments
 (0)