Skip to content

Commit e64f403

Browse files
committed
Add clear option to redraw
1 parent 6922fc0 commit e64f403

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,27 @@ $menu = (new CliMenuBuilder)
415415
$menu->open();
416416
```
417417

418+
If you change the menu drastically, such as making the width smaller, when it redraws you might see artifacts of the previous draw
419+
as `redraw` only draws over the top of the terminal. If this happens you can pass `true` to `redraw` and it will first clear
420+
the terminal before redrawing.
421+
422+
```php
423+
$itemCallable = function (CliMenu $menu) {
424+
$menu->getStyle()->setWidth($menu->getStyle()->getWidth() === 100 ? 80 : 100);
425+
$menu->redraw(true);
426+
};
427+
428+
$menu = (new CliMenuBuilder)
429+
->setTitle('Basic CLI Menu')
430+
->addItem('First Item', $itemCallable)
431+
->addItem('Second Item', $itemCallable)
432+
->addItem('Third Item', $itemCallable)
433+
->addLineBreak('-')
434+
->build();
435+
436+
$menu->open();
437+
```
438+
418439
#### Getting, Removing and Adding items
419440

420441
You can also interact with the menu items in an action. You can add, remove and replace items. If you do this, you

examples/crazy-redraw.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function (MenuItemInterface $item) use ($menu) {
4646
}
4747
);
4848

49-
$menu->redraw();
49+
$menu->redraw(true);
5050
};
5151

5252
$menu = (new CliMenuBuilder)

src/CliMenu.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,18 @@ protected function executeCurrentItem() : void
307307
}
308308

309309
/**
310+
* If true we clear the whole terminal screen, useful
311+
* for example when reducing the width of the menu, to not
312+
* leave leftovers of the previous wider menu.
313+
*
310314
* Redraw the menu
311315
*/
312-
public function redraw() : void
316+
public function redraw(bool $clear = false) : void
313317
{
318+
if ($clear) {
319+
$this->terminal->clear();
320+
}
321+
314322
$this->assertOpen();
315323
$this->draw();
316324
}

test/CliMenuTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,58 @@ public function testReDrawReDrawsImmediately() : void
165165
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
166166
}
167167

168+
public function testRedrawClearsTerminalFirstIfOptionIsPassed() : void
169+
{
170+
$terminal = $this->createMock(Terminal::class);
171+
$terminal->expects($this->any())
172+
->method('isInteractive')
173+
->willReturn(true);
174+
175+
$terminal->expects($this->any())
176+
->method('getWidth')
177+
->willReturn(80);
178+
179+
$terminal->expects($this->any())
180+
->method('write')
181+
->will($this->returnCallback(function ($buffer) {
182+
$this->output->write($buffer);
183+
}));
184+
185+
$terminal->expects($this->exactly(3))
186+
->method('read')
187+
->willReturn("\n", "\n", "\n");
188+
189+
$terminal->expects($this->atLeast(2))
190+
->method('clear');
191+
192+
$style = $this->getStyle($terminal);
193+
$style->setWidth(70);
194+
195+
$hits = 0;
196+
$item = new SelectableItem('Item 1', function (CliMenu $menu) use (&$hits) {
197+
if ($hits === 0) {
198+
$menu->getStyle()->setWidth(50);
199+
$menu->redraw(true);
200+
}
201+
202+
if ($hits === 1) {
203+
$menu->getStyle()->setWidth(70);
204+
$menu->redraw(true);
205+
}
206+
207+
if ($hits === 2) {
208+
$menu->close();
209+
}
210+
211+
$hits++;
212+
});
213+
214+
$menu = new CliMenu('PHP School FTW', [$item], $terminal, $style);
215+
$menu->open();
216+
217+
static::assertStringEqualsFile($this->getTestFile(), $this->output->fetch());
218+
}
219+
168220
public function testGetItems() : void
169221
{
170222
$item1 = new LineBreakItem();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
 
4+
 PHP School FTW 
5+
 ================================================================== 
6+
 ● Item 1 
7+
 
8+
9+
10+
11+
12+
 
13+
 PHP School FTW 
14+
 ============================================== 
15+
 ● Item 1 
16+
 
17+
18+
19+
20+
21+
 
22+
 PHP School FTW 
23+
 ================================================================== 
24+
 ● Item 1 
25+
 
26+
27+

0 commit comments

Comments
 (0)