Skip to content

Commit 6f857e1

Browse files
committed
Attempt to resize menu on terminal resize
1 parent 587fd8b commit 6f857e1

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php" : ">=7.1",
2323
"beberlei/assert": "^2.4 | ^3",
24-
"php-school/terminal": "^0.2.1",
24+
"php-school/terminal": "dev-feature/signals",
2525
"ext-posix": "*"
2626
},
2727
"autoload" : {

examples/submenu.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
->addLineBreak('-');
2323
})
2424
->setWidth(70)
25+
->setMarginAuto()
2526
->setBackgroundColour('yellow')
2627
->build();
2728

src/CliMenu.php

+14
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public function __construct(
116116
$this->radioStyle = new RadioStyle();
117117
$this->selectableStyle = new SelectableStyle();
118118

119+
$this->terminal->onSignal(SIGWINCH, function () {
120+
$this->style->windowResize();
121+
122+
if ($this->isOpen()) {
123+
$this->redraw(true);
124+
}
125+
});
126+
119127
$this->selectFirstItem();
120128
}
121129

@@ -295,6 +303,12 @@ private function display() : void
295303

296304
while ($this->isOpen()) {
297305
$char = $reader->readCharacter();
306+
307+
if (null === $char) {
308+
usleep(10000);
309+
continue;
310+
}
311+
298312
if (!$char->isHandledControl()) {
299313
$rawChar = $char->get();
300314
if (isset($this->customControlMappings[$rawChar])) {

src/MenuStyle.php

+28
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ class MenuStyle
5252
*/
5353
private $requestedWidth;
5454

55+
/**
56+
* If the window was resized, we store the original
57+
* size, before any resizing, to attempt to restore
58+
* it on a later resize.
59+
*
60+
* @var int
61+
*/
62+
private $widthBeforeResize;
63+
5564
/**
5665
* @var int
5766
*/
@@ -402,6 +411,7 @@ public function setWidth(int $width) : self
402411
$this->requestedWidth = $width;
403412
$width = $this->maybeShrinkWidth($this->margin, $width);
404413

414+
$this->widthBeforeResize = null;
405415
$this->width = $width;
406416
if ($this->marginAuto) {
407417
$this->calculateMarginAuto($width);
@@ -423,6 +433,24 @@ private function maybeShrinkWidth(int $margin, int $width) : int
423433
return $width;
424434
}
425435

436+
public function windowResize() : void
437+
{
438+
if (null === $this->widthBeforeResize) {
439+
$this->widthBeforeResize = $this->width;
440+
}
441+
442+
$width = $this->maybeShrinkWidth($this->margin, $this->widthBeforeResize);
443+
444+
$this->width = $width;
445+
if ($this->marginAuto) {
446+
$this->calculateMarginAuto($width);
447+
}
448+
449+
$this->calculateContentWidth();
450+
$this->generateBorderRows();
451+
$this->generatePaddingTopBottomRows();
452+
}
453+
426454
public function getPaddingTopBottom() : int
427455
{
428456
return $this->paddingTopBottom;

src/Terminal/TerminalFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpSchool\CliMenu\Terminal;
44

5+
use PhpSchool\Terminal\IO\NonBlockingResourceInputStream;
56
use PhpSchool\Terminal\IO\ResourceInputStream;
67
use PhpSchool\Terminal\IO\ResourceOutputStream;
78
use PhpSchool\Terminal\Terminal;
@@ -14,6 +15,6 @@ class TerminalFactory
1415
{
1516
public static function fromSystem() : Terminal
1617
{
17-
return new UnixTerminal(new ResourceInputStream, new ResourceOutputStream);
18+
return new UnixTerminal(new NonBlockingResourceInputStream, new ResourceOutputStream);
1819
}
1920
}

0 commit comments

Comments
 (0)