Skip to content

Improve the formatting of disabled menu items in different terminals #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,74 @@ public function hasChangedFromDefaults() : bool
return $currentValues !== array_values($defaultStyleValues);
}

/**
* Get text for a disabled menu item.
*
* This sets the foreground colour to the ansi bright equivalent,
* and on supported terminals, adds additional dim formatting.
*
* @return string
*/
public function getDisabledItemText(string $text) : string
{
return sprintf(
"\033[%sm%s\033[%sm",
"\033[%sm\033[%sm%s\033[%sm\033[%sm",
self::$availableOptions['dim']['set'],
$this->getForegroundColourCode(true),
$text,
$this->getForegroundColourCode(),
self::$availableOptions['dim']['unset']
);
}

/**
* Generates the ansi escape sequence to set the colours
* Get the ansi escape sequence for the foreground colour.
*
* @param bool $bright Whether to modify to the ansi bright variation
*
* @return string
*/
private function generateColoursSetCode() : void
private function getForegroundColourCode(bool $bright = false) : string
{
if (!ctype_digit($this->fg)) {
$fgCode = self::$availableForegroundColors[$this->fg];
$fgCode = (int)self::$availableForegroundColors[$this->fg];
$fgCode += ($bright ? 60 : 0);
} else {
$fgCode = sprintf("38;5;%s", $this->fg);
$fgCode = sprintf("38;5;%s", ((int)$this->fg + ($bright ? 60 : 0)));
}

return (string)$fgCode;
}

/**
* Get the ansi escape sequence for the background colour.
*
* @param bool $bright Whether to modify to the ansi bright variation
*
* @return string
*/
private function getBackgroundColourCode(bool $bright = false) : string
{
if (!ctype_digit($this->bg)) {
$bgCode = self::$availableBackgroundColors[$this->bg];
$bgCode = (int)self::$availableBackgroundColors[$this->bg];
$bgCode += ($bright ? 60 : 0);
} else {
$bgCode = sprintf("48;5;%s", $this->bg);
$bgCode = sprintf("48;5;%s", ((int)$this->bg + ($bright ? 60 : 0)));
}

$this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode);
return (string)$bgCode;
}

/**
* Generates the ansi escape sequence to set the colours
*/
private function generateColoursSetCode() : void
{
$this->coloursSetCode = sprintf(
"\033[%s;%sm",
$this->getForegroundColourCode(),
$this->getBackgroundColourCode()
);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions test/MenuItem/SelectableItemRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,14 @@ public function testRenderDisabled() : void
$menuStyle->setWidth(35);
$style = (new SelectableStyle())->setItemExtra('[DONE]');

$item = new SelectableItem('SOME TEXT', function () {
$item = new SelectableItem($testString = 'SOME TEXT', function () {
});
$item->setStyle($style);
$item->showItemExtra();

self::assertEquals(
[
"\033[2m● SOME TEXT\033[22m [DONE]",
],
$renderer->render($menuStyle, $item, true, true)
escapeshellcmd("\033[2m\033[97m● " . $testString . "\033[37m\033[22m [DONE]"),
escapeshellcmd($renderer->render($menuStyle, $item, true, true)[0])
);
}
public function testWrapAndIndentText() : void
Expand Down