Skip to content

Commit 42a6491

Browse files
committed
Seperated the classes
1 parent b17b544 commit 42a6491

11 files changed

+164
-85
lines changed

examples/confirm-cancellable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require_once(__DIR__ . '/../vendor/autoload.php');
77

88
$itemCallable = function (CliMenu $menu) {
9-
$continue = $menu->confirm('PHP School FTW!', null, true)
9+
$continue = $menu->cancellableConfirm('PHP School FTW!', null, true)
1010
->display('OK', 'Cancel');
1111

1212
if ($continue) {

src/CliMenu.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpSchool\CliMenu;
44

5+
use PhpSchool\CliMenu\Dialogue\CancellableConfirm;
56
use PhpSchool\CliMenu\Exception\InvalidTerminalException;
67
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
78
use PhpSchool\CliMenu\Input\InputIO;
@@ -694,18 +695,29 @@ public function flash(string $text, MenuStyle $style = null) : Flash
694695
->setBg('yellow')
695696
->setFg('red');
696697

697-
return new Flash($this, $style, $this->terminal, $text, false);
698+
return new Flash($this, $style, $this->terminal, $text);
698699
}
699700

700-
public function confirm(string $text, MenuStyle $style = null, bool $cancellable = false) : Confirm
701+
public function confirm(string $text, MenuStyle $style = null) : Confirm
701702
{
702703
$this->guardSingleLine($text);
703704

704705
$style = $style ?? (new MenuStyle($this->terminal))
705706
->setBg('yellow')
706707
->setFg('red');
707708

708-
return new Confirm($this, $style, $this->terminal, $text, $cancellable);
709+
return new Confirm($this, $style, $this->terminal, $text);
710+
}
711+
712+
public function cancellableConfirm(string $text, MenuStyle $style = null) : CancellableConfirm
713+
{
714+
$this->guardSingleLine($text);
715+
716+
$style = $style ?? (new MenuStyle($this->terminal))
717+
->setBg('yellow')
718+
->setFg('red');
719+
720+
return new CancellableConfirm($this, $style, $this->terminal, $text);
709721
}
710722

711723
public function askNumber(MenuStyle $style = null) : Number

src/Dialogue/CancellableConfirm.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenu\Dialogue;
4+
5+
use PhpSchool\Terminal\InputCharacter;
6+
use PhpSchool\Terminal\NonCanonicalReader;
7+
8+
/**
9+
* @author Aydin Hassan <aydin@hotmail.co.uk>
10+
*/
11+
class CancellableConfirm extends Dialogue
12+
{
13+
/**
14+
* @var bool
15+
*/
16+
private $confirm = true;
17+
18+
/**
19+
* Display confirmation with a button with the given text
20+
*/
21+
public function display(string $confirmText = 'OK', string $cancelText = 'Cancel') : bool
22+
{
23+
$this->drawDialog($confirmText, $cancelText);
24+
25+
$reader = new NonCanonicalReader($this->terminal);
26+
27+
while ($char = $reader->readCharacter()) {
28+
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) {
29+
$this->parentMenu->redraw();
30+
return $this->confirm;
31+
} elseif ($char->isControl() && $char->getControl() === InputCharacter::TAB ||
32+
($char->isControl() && $char->getControl() === InputCharacter::RIGHT && $this->confirm) ||
33+
($char->isControl() && $char->getControl() === InputCharacter::LEFT && !$this->confirm)
34+
) {
35+
$this->confirm = !$this->confirm;
36+
$this->drawDialog($confirmText, $cancelText);
37+
}
38+
}
39+
}
40+
41+
private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Cancel'): void
42+
{
43+
$this->assertMenuOpen();
44+
45+
$this->terminal->moveCursorToRow($this->y);
46+
47+
$promptWidth = mb_strlen($this->text) + 4;
48+
49+
$buttonLength = mb_strlen($confirmText) + 6;
50+
$buttonLength += mb_strlen($cancelText) + 7;
51+
52+
$confirmButton = sprintf(
53+
'%s%s < %s > %s%s',
54+
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim'),
55+
$this->style->getInvertedColoursSetCode(),
56+
$confirmText,
57+
$this->style->getInvertedColoursUnsetCode(),
58+
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim', false)
59+
);
60+
61+
$cancelButton = sprintf(
62+
'%s%s < %s > %s%s',
63+
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold'),
64+
$this->style->getInvertedColoursSetCode(),
65+
$cancelText,
66+
$this->style->getInvertedColoursUnsetCode(),
67+
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold', false)
68+
);
69+
70+
$buttonRow = $confirmButton . " " . $cancelButton;
71+
72+
if ($promptWidth < $buttonLength) {
73+
$pad = ($buttonLength - $promptWidth) / 2;
74+
$this->text = sprintf(
75+
'%s%s%s',
76+
str_repeat(' ', intval(round($pad, 0, 2) + $this->style->getPaddingLeftRight())),
77+
$this->text,
78+
str_repeat(' ', intval(round($pad, 0, 1) + $this->style->getPaddingLeftRight()))
79+
);
80+
$promptWidth = mb_strlen($this->text) + 4;
81+
}
82+
83+
$leftFill = (int) (($promptWidth / 2) - ($buttonLength / 2));
84+
85+
$this->emptyRow();
86+
87+
$this->write(sprintf(
88+
"%s%s%s%s%s\n",
89+
$this->style->getColoursSetCode(),
90+
str_repeat(' ', $this->style->getPaddingLeftRight()),
91+
$this->text,
92+
str_repeat(' ', $this->style->getPaddingLeftRight()),
93+
$this->style->getColoursResetCode()
94+
));
95+
96+
$this->emptyRow();
97+
98+
$this->write(sprintf(
99+
"%s%s%s%s%s\n",
100+
$this->style->getColoursSetCode(),
101+
str_repeat(' ', $leftFill),
102+
$buttonRow,
103+
str_repeat(' ', (int) ceil($promptWidth - $leftFill - $buttonLength)),
104+
$this->style->getColoursResetCode()
105+
));
106+
107+
$this->emptyRow();
108+
109+
$this->terminal->moveCursorToTop();
110+
}
111+
}

src/Dialogue/Confirm.php

Lines changed: 26 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,18 @@
1010
*/
1111
class Confirm extends Dialogue
1212
{
13-
/**
14-
* @var bool
15-
*/
16-
private $confirm = true;
1713

1814
/**
1915
* Display confirmation with a button with the given text
2016
*/
21-
public function display(string $confirmText = 'OK', string $cancelText = 'Cancel') : bool
22-
{
23-
$this->drawDialog($confirmText, $cancelText);
24-
25-
$reader = new NonCanonicalReader($this->terminal);
26-
27-
while ($char = $reader->readCharacter()) {
28-
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) {
29-
$this->parentMenu->redraw();
30-
return $this->confirm;
31-
} elseif ($char->isControl() && $char->getControl() === InputCharacter::TAB ||
32-
($char->isControl() && $char->getControl() === InputCharacter::RIGHT && $this->confirm) ||
33-
($char->isControl() && $char->getControl() === InputCharacter::LEFT && !$this->confirm)
34-
) {
35-
$this->confirm = !$this->confirm;
36-
$this->drawDialog($confirmText, $cancelText);
37-
}
38-
}
39-
}
40-
41-
private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Cancel'): void
17+
public function display(string $confirmText = 'OK') : void
4218
{
4319
$this->assertMenuOpen();
4420

4521
$this->terminal->moveCursorToRow($this->y);
4622

4723
$promptWidth = mb_strlen($this->text) + 4;
4824

49-
$buttonLength = mb_strlen($confirmText) + 6;
50-
if ($this->cancellable) {
51-
$buttonLength += mb_strlen($cancelText) + 7;
52-
}
53-
54-
$confirmButton = sprintf(
55-
'%s%s < %s > %s%s',
56-
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim'),
57-
$this->style->getInvertedColoursSetCode(),
58-
$confirmText,
59-
$this->style->getInvertedColoursUnsetCode(),
60-
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim', false)
61-
);
62-
63-
$cancelButton = sprintf(
64-
'%s%s < %s > %s%s',
65-
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold'),
66-
$this->style->getInvertedColoursSetCode(),
67-
$cancelText,
68-
$this->style->getInvertedColoursUnsetCode(),
69-
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold', false)
70-
);
71-
72-
$buttonRow = $confirmButton . ($this->cancellable ? " $cancelButton" : '');
73-
74-
if ($promptWidth < $buttonLength) {
75-
$pad = ($buttonLength - $promptWidth) / 2;
76-
$this->text = sprintf(
77-
'%s%s%s',
78-
str_repeat(' ', intval(round($pad, 0, 2) + $this->style->getPaddingLeftRight())),
79-
$this->text,
80-
str_repeat(' ', intval(round($pad, 0, 1) + $this->style->getPaddingLeftRight()))
81-
);
82-
$promptWidth = mb_strlen($this->text) + 4;
83-
}
84-
85-
$leftFill = (int) (($promptWidth / 2) - ($buttonLength / 2));
86-
8725
$this->emptyRow();
8826

8927
$this->write(sprintf(
@@ -97,17 +35,38 @@ private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Ca
9735

9836
$this->emptyRow();
9937

38+
$confirmText = sprintf(' < %s > ', $confirmText);
39+
$leftFill = (int) (($promptWidth / 2) - (mb_strlen($confirmText) / 2));
40+
10041
$this->write(sprintf(
101-
"%s%s%s%s%s\n",
42+
"%s%s%s%s%s%s%s\n",
10243
$this->style->getColoursSetCode(),
10344
str_repeat(' ', $leftFill),
104-
$buttonRow,
105-
str_repeat(' ', (int) ceil($promptWidth - $leftFill - $buttonLength)),
45+
$this->style->getInvertedColoursSetCode(),
46+
$confirmText,
47+
$this->style->getInvertedColoursUnsetCode(),
48+
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strlen($confirmText))),
10649
$this->style->getColoursResetCode()
10750
));
10851

109-
$this->emptyRow();
52+
$this->write(sprintf(
53+
"%s%s%s%s%s\n",
54+
$this->style->getColoursSetCode(),
55+
str_repeat(' ', $this->style->getPaddingLeftRight()),
56+
str_repeat(' ', mb_strlen($this->text)),
57+
str_repeat(' ', $this->style->getPaddingLeftRight()),
58+
$this->style->getColoursResetCode()
59+
));
11060

11161
$this->terminal->moveCursorToTop();
62+
63+
$reader = new NonCanonicalReader($this->terminal);
64+
65+
while ($char = $reader->readCharacter()) {
66+
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) {
67+
$this->parentMenu->redraw();
68+
return;
69+
}
70+
}
11271
}
11372
}

src/Dialogue/Dialogue.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ public function __construct(
5151
CliMenu $parentMenu,
5252
MenuStyle $menuStyle,
5353
Terminal $terminal,
54-
string $text,
55-
bool $cancellable
54+
string $text
5655
) {
5756
$this->style = $menuStyle;
5857
$this->terminal = $terminal;
5958
$this->text = $text;
6059
$this->parentMenu = $parentMenu;
61-
$this->cancellable = $cancellable;
6260

6361
$this->calculateCoordinates();
6462
}

test/Dialogue/ConfirmTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpSchool\CliMenu\CliMenu;
66
use PhpSchool\CliMenu\MenuItem\SelectableItem;
77
use PhpSchool\CliMenu\MenuStyle;
8-
use PhpSchool\Terminal\InputCharacter;
98
use PhpSchool\Terminal\IO\BufferedOutput;
109
use PhpSchool\Terminal\Terminal;
1110
use PHPUnit\Framework\TestCase;
@@ -153,7 +152,7 @@ public function testConfirmCancellableWithShortPrompt(): void
153152
$style = $this->getStyle($this->terminal);
154153

155154
$item = new SelectableItem('Item 1', function (CliMenu $menu) {
156-
$menu->confirm('PHP', null, true)
155+
$menu->cancellableConfirm('PHP', null, true)
157156
->display('OK', 'Cancel');
158157

159158
$menu->close();
@@ -177,7 +176,7 @@ public function testConfirmCancellableWithLongPrompt(): void
177176
$style = $this->getStyle($this->terminal);
178177

179178
$item = new SelectableItem('Item 1', function (CliMenu $menu) {
180-
$menu->confirm('PHP School Rocks FTW!', null, true)
179+
$menu->cancellableConfirm('PHP School Rocks FTW!', null, true)
181180
->display('OK', 'Cancel');
182181

183182
$menu->close();
@@ -230,7 +229,7 @@ public function testConfirmOkNonCancellableReturnsTrue()
230229
$return = '';
231230

232231
$item = new SelectableItem('Item 1', function (CliMenu $menu) use (&$return) {
233-
$return = $menu->confirm('PHP School FTW!')
232+
$return = $menu->cancellableConfirm('PHP School FTW!')
234233
->display('OK');
235234

236235
$menu->close();
@@ -253,7 +252,7 @@ public function testConfirmOkCancellableReturnsTrue()
253252
$return = '';
254253

255254
$item = new SelectableItem('Item 1', function (CliMenu $menu) use (&$return) {
256-
$return = $menu->confirm('PHP School FTW!')
255+
$return = $menu->cancellableConfirm('PHP School FTW!')
257256
->display('OK', 'Cancel');
258257

259258
$menu->close();
@@ -276,7 +275,7 @@ public function testConfirmCancelCancellableReturnsFalse()
276275
$return = '';
277276

278277
$item = new SelectableItem('Item 1', function (CliMenu $menu) use (&$return) {
279-
$return = $menu->confirm('PHP School FTW!', null, true)
278+
$return = $menu->cancellableConfirm('PHP School FTW!', null)
280279
->display('OK', 'Cancel');
281280

282281
$menu->close();

test/res/testConfirmCanOnlyBeClosedWithEnter.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
 
1111
 PHP School FTW! 
1212
 
13-
  < OK >  
13+
  < OK >  
1414
 
1515

1616

test/res/testConfirmWithEvenLengthConfirmAndButton.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
 
1111
 PHP School FTW 
1212
 
13-
  < OK >  
13+
  < OK >  
1414
 
1515

1616

test/res/testConfirmWithEvenLengthConfirmAndOddLengthButton.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
 
1111
 PHP School FTW 
1212
 
13-
  < OK! >  
13+
  < OK! >  
1414
 
1515

1616

test/res/testConfirmWithOddLengthConfirmAndButton.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
 
1111
 PHP School FTW! 
1212
 
13-
  < OK! >  
13+
  < OK! >  
1414
 
1515

1616

0 commit comments

Comments
 (0)