Skip to content

Auto resize width #222

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php" : ">=7.1",
"beberlei/assert": "^2.4 | ^3",
"php-school/terminal": "^0.2.1",
"php-school/terminal": "dev-feature/signals",
"ext-posix": "*"
},
"autoload" : {
Expand Down
1 change: 1 addition & 0 deletions examples/submenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
->addLineBreak('-');
})
->setWidth(70)
->setMarginAuto()
->setBackgroundColour('yellow')
->build();

Expand Down
14 changes: 14 additions & 0 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public function __construct(
$this->radioStyle = new RadioStyle();
$this->selectableStyle = new SelectableStyle();

$this->terminal->onSignal(SIGWINCH, function () {
$this->style->windowResize();

if ($this->isOpen()) {
$this->redraw(true);
}
});

$this->selectFirstItem();
}

Expand Down Expand Up @@ -295,6 +303,12 @@ private function display() : void

while ($this->isOpen()) {
$char = $reader->readCharacter();

if (null === $char) {
usleep(10000);
continue;
}

if (!$char->isHandledControl()) {
$rawChar = $char->get();
if (isset($this->customControlMappings[$rawChar])) {
Expand Down
28 changes: 28 additions & 0 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class MenuStyle
*/
private $requestedWidth;

/**
* If the window was resized, we store the original
* size, before any resizing, to attempt to restore
* it on a later resize.
*
* @var int
*/
private $widthBeforeResize;

/**
* @var int
*/
Expand Down Expand Up @@ -402,6 +411,7 @@ public function setWidth(int $width) : self
$this->requestedWidth = $width;
$width = $this->maybeShrinkWidth($this->margin, $width);

$this->widthBeforeResize = null;
$this->width = $width;
if ($this->marginAuto) {
$this->calculateMarginAuto($width);
Expand All @@ -423,6 +433,24 @@ private function maybeShrinkWidth(int $margin, int $width) : int
return $width;
}

public function windowResize() : void
{
if (null === $this->widthBeforeResize) {
$this->widthBeforeResize = $this->width;
}

$width = $this->maybeShrinkWidth($this->margin, $this->widthBeforeResize);

$this->width = $width;
if ($this->marginAuto) {
$this->calculateMarginAuto($width);
}

$this->calculateContentWidth();
$this->generateBorderRows();
$this->generatePaddingTopBottomRows();
}

public function getPaddingTopBottom() : int
{
return $this->paddingTopBottom;
Expand Down
3 changes: 2 additions & 1 deletion src/Terminal/TerminalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpSchool\CliMenu\Terminal;

use PhpSchool\Terminal\IO\NonBlockingResourceInputStream;
use PhpSchool\Terminal\IO\ResourceInputStream;
use PhpSchool\Terminal\IO\ResourceOutputStream;
use PhpSchool\Terminal\Terminal;
Expand All @@ -14,6 +15,6 @@ class TerminalFactory
{
public static function fromSystem() : Terminal
{
return new UnixTerminal(new ResourceInputStream, new ResourceOutputStream);
return new UnixTerminal(new NonBlockingResourceInputStream, new ResourceOutputStream);
}
}