Skip to content

Commit 44815c8

Browse files
authored
Improve the formatting of disabled menu items in different terminals (#236)
* Improve the formatting of disabled menu items Disabled items are now modified to use the ANSI "bright" equivalent of the same colour as their foreground text, as well as the dim escape sequence modifier. * Added return types * Corrected typo * Fixed broken test * Corrected mismatched types found in static analysis
1 parent c48a4d8 commit 44815c8

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/MenuStyle.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,34 +277,74 @@ public function hasChangedFromDefaults() : bool
277277
return $currentValues !== array_values($defaultStyleValues);
278278
}
279279

280+
/**
281+
* Get text for a disabled menu item.
282+
*
283+
* This sets the foreground colour to the ansi bright equivalent,
284+
* and on supported terminals, adds additional dim formatting.
285+
*
286+
* @return string
287+
*/
280288
public function getDisabledItemText(string $text) : string
281289
{
282290
return sprintf(
283-
"\033[%sm%s\033[%sm",
291+
"\033[%sm\033[%sm%s\033[%sm\033[%sm",
284292
self::$availableOptions['dim']['set'],
293+
$this->getForegroundColourCode(true),
285294
$text,
295+
$this->getForegroundColourCode(),
286296
self::$availableOptions['dim']['unset']
287297
);
288298
}
289299

290300
/**
291-
* Generates the ansi escape sequence to set the colours
301+
* Get the ansi escape sequence for the foreground colour.
302+
*
303+
* @param bool $bright Whether to modify to the ansi bright variation
304+
*
305+
* @return string
292306
*/
293-
private function generateColoursSetCode() : void
307+
private function getForegroundColourCode(bool $bright = false) : string
294308
{
295309
if (!ctype_digit($this->fg)) {
296-
$fgCode = self::$availableForegroundColors[$this->fg];
310+
$fgCode = (int)self::$availableForegroundColors[$this->fg];
311+
$fgCode += ($bright ? 60 : 0);
297312
} else {
298-
$fgCode = sprintf("38;5;%s", $this->fg);
313+
$fgCode = sprintf("38;5;%s", ((int)$this->fg + ($bright ? 60 : 0)));
299314
}
300315

316+
return (string)$fgCode;
317+
}
318+
319+
/**
320+
* Get the ansi escape sequence for the background colour.
321+
*
322+
* @param bool $bright Whether to modify to the ansi bright variation
323+
*
324+
* @return string
325+
*/
326+
private function getBackgroundColourCode(bool $bright = false) : string
327+
{
301328
if (!ctype_digit($this->bg)) {
302-
$bgCode = self::$availableBackgroundColors[$this->bg];
329+
$bgCode = (int)self::$availableBackgroundColors[$this->bg];
330+
$bgCode += ($bright ? 60 : 0);
303331
} else {
304-
$bgCode = sprintf("48;5;%s", $this->bg);
332+
$bgCode = sprintf("48;5;%s", ((int)$this->bg + ($bright ? 60 : 0)));
305333
}
306334

307-
$this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode);
335+
return (string)$bgCode;
336+
}
337+
338+
/**
339+
* Generates the ansi escape sequence to set the colours
340+
*/
341+
private function generateColoursSetCode() : void
342+
{
343+
$this->coloursSetCode = sprintf(
344+
"\033[%s;%sm",
345+
$this->getForegroundColourCode(),
346+
$this->getBackgroundColourCode()
347+
);
308348
}
309349

310350
/**

test/MenuItem/SelectableItemRendererTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,14 @@ public function testRenderDisabled() : void
8484
$menuStyle->setWidth(35);
8585
$style = (new SelectableStyle())->setItemExtra('[DONE]');
8686

87-
$item = new SelectableItem('SOME TEXT', function () {
87+
$item = new SelectableItem($testString = 'SOME TEXT', function () {
8888
});
8989
$item->setStyle($style);
9090
$item->showItemExtra();
9191

9292
self::assertEquals(
93-
[
94-
"\033[2m● SOME TEXT\033[22m [DONE]",
95-
],
96-
$renderer->render($menuStyle, $item, true, true)
93+
escapeshellcmd("\033[2m\033[97m● " . $testString . "\033[37m\033[22m [DONE]"),
94+
escapeshellcmd($renderer->render($menuStyle, $item, true, true)[0])
9795
);
9896
}
9997
public function testWrapAndIndentText() : void

0 commit comments

Comments
 (0)