Skip to content

Commit 7d79121

Browse files
authored
Merge pull request #154 from php-school/remove-marker-space
Remove the mandatory space after markers
2 parents 0dae3f2 + 2b4c91d commit 7d79121

File tree

7 files changed

+99
-124
lines changed

7 files changed

+99
-124
lines changed

src/MenuItem/SelectableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ trait SelectableTrait
3030
*/
3131
public function getRows(MenuStyle $style, bool $selected = false) : array
3232
{
33-
$marker = sprintf("%s ", $style->getMarker($selected));
33+
$marker = sprintf("%s", $style->getMarker($selected));
3434

3535
$length = $style->getDisplaysExtra()
3636
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)

src/MenuItem/SplitItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
126126
array_map(function ($index, $item) use ($selected, $length, $style) {
127127
$isSelected = $selected && $index === $this->selectedItemIndex;
128128
$marker = $item->canSelect()
129-
? sprintf('%s ', $style->getMarker($isSelected))
129+
? sprintf('%s', $style->getMarker($isSelected))
130130
: '';
131131

132132
$itemExtra = '';

src/MenuStyle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class MenuStyle
157157
'paddingTopBottom' => 1,
158158
'paddingLeftRight' => 2,
159159
'margin' => 2,
160-
'selectedMarker' => '',
161-
'unselectedMarker' => '',
160+
'selectedMarker' => ' ',
161+
'unselectedMarker' => ' ',
162162
'itemExtra' => '',
163163
'displaysExtra' => false,
164164
'titleSeparator' => '=',

test/MenuItem/MenuMenuItemTest.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSchool\CliMenu\CliMenu;
66
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
77
use PhpSchool\CliMenu\MenuStyle;
8+
use PhpSchool\Terminal\Terminal;
89
use PHPUnit\Framework\TestCase;
910

1011
/**
@@ -51,33 +52,29 @@ public function testGetText() : void
5152

5253
public function testGetRows() : void
5354
{
54-
$menuStyle = $this->createMock(MenuStyle::class);
55+
$terminal = $this->createMock(Terminal::class);
56+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
5557

56-
$menuStyle
57-
->expects($this->any())
58-
->method('getContentWidth')
59-
->will($this->returnValue(10));
58+
$menuStyle = new MenuStyle($terminal);
59+
$menuStyle->setPaddingLeftRight(0);
60+
$menuStyle->setWidth(10);
61+
$menuStyle->setUnselectedMarker('* ');
6062

6163
$subMenu = $this->createMock(CliMenu::class);
6264

6365
$item = new MenuMenuItem('Item', $subMenu);
64-
$this->assertEquals([' Item'], $item->getRows($menuStyle));
66+
$this->assertEquals(['* Item'], $item->getRows($menuStyle));
6567
}
6668

6769
public function testGetRowsWithUnSelectedMarker() : void
6870
{
69-
$menuStyle = $this->createMock(MenuStyle::class);
71+
$terminal = $this->createMock(Terminal::class);
72+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
7073

71-
$menuStyle
72-
->expects($this->any())
73-
->method('getContentWidth')
74-
->will($this->returnValue(10));
75-
76-
$menuStyle
77-
->expects($this->exactly(2))
78-
->method('getMarker')
79-
->with(false)
80-
->will($this->returnValue('*'));
74+
$menuStyle = new MenuStyle($terminal);
75+
$menuStyle->setPaddingLeftRight(0);
76+
$menuStyle->setWidth(10);
77+
$menuStyle->setUnselectedMarker('* ');
8178

8279
$subMenu = $this->createMock(CliMenu::class);
8380

@@ -99,7 +96,7 @@ public function testGetRowsWithSelectedMarker() : void
9996
->expects($this->once())
10097
->method('getMarker')
10198
->with(true)
102-
->will($this->returnValue('='));
99+
->will($this->returnValue('= '));
103100

104101
$subMenu = $this->getMockBuilder(CliMenu::class)
105102
->disableOriginalConstructor()
@@ -112,20 +109,21 @@ public function testGetRowsWithSelectedMarker() : void
112109

113110
public function testGetRowsWithMultipleLines() : void
114111
{
115-
$menuStyle = $this->createMock(MenuStyle::class);
112+
$terminal = $this->createMock(Terminal::class);
113+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
116114

117-
$menuStyle
118-
->expects($this->any())
119-
->method('getContentWidth')
120-
->will($this->returnValue(10));
115+
$menuStyle = new MenuStyle($terminal);
116+
$menuStyle->setPaddingLeftRight(0);
117+
$menuStyle->setWidth(10);
118+
$menuStyle->setUnselectedMarker('* ');
121119

122120
$subMenu = $this->createMock(CliMenu::class);
123121

124122
$item = new MenuMenuItem('LONG ITEM LINE', $subMenu);
125123
$this->assertEquals(
126124
[
127-
" LONG ITEM",
128-
" LINE",
125+
"* LONG",
126+
" ITEM LINE",
129127
],
130128
$item->getRows($menuStyle)
131129
);

test/MenuItem/SelectableItemTest.php

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\CliMenu\MenuItem\SelectableItem;
66
use PhpSchool\CliMenu\MenuStyle;
7+
use PhpSchool\Terminal\Terminal;
78
use PHPUnit\Framework\TestCase;
89

910
/**
@@ -46,35 +47,35 @@ public function testGetText() : void
4647

4748
public function testGetRows() : void
4849
{
49-
$menuStyle = $this->createMock(MenuStyle::class);
50+
$terminal = $this->createMock(Terminal::class);
51+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
5052

51-
$menuStyle
52-
->expects($this->any())
53-
->method('getContentWidth')
54-
->will($this->returnValue(10));
53+
$menuStyle = new MenuStyle($terminal);
54+
$menuStyle->setPaddingLeftRight(0);
55+
$menuStyle->setWidth(10);
5556

5657
$item = new SelectableItem('Item', function () {
5758
});
58-
$this->assertEquals([' Item'], $item->getRows($menuStyle));
59-
$this->assertEquals([' Item'], $item->getRows($menuStyle, false));
60-
$this->assertEquals([' Item'], $item->getRows($menuStyle, true));
59+
$this->assertEquals([' Item'], $item->getRows($menuStyle));
60+
$this->assertEquals([' Item'], $item->getRows($menuStyle, false));
61+
$this->assertEquals([' Item'], $item->getRows($menuStyle, true));
6162
}
6263

6364
public function testSetText() : void
6465
{
65-
$menuStyle = $this->createMock(MenuStyle::class);
66+
$terminal = $this->createMock(Terminal::class);
67+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
68+
69+
$menuStyle = new MenuStyle($terminal);
70+
$menuStyle->setPaddingLeftRight(0);
71+
$menuStyle->setWidth(10);
6672

67-
$menuStyle
68-
->expects($this->any())
69-
->method('getContentWidth')
70-
->will($this->returnValue(10));
71-
7273
$item = new SelectableItem('Item', function () {
7374
});
7475
$item->setText('New Text');
75-
$this->assertEquals([' New Text'], $item->getRows($menuStyle));
76-
$this->assertEquals([' New Text'], $item->getRows($menuStyle, false));
77-
$this->assertEquals([' New Text'], $item->getRows($menuStyle, true));
76+
$this->assertEquals([' New Text'], $item->getRows($menuStyle));
77+
$this->assertEquals([' New Text'], $item->getRows($menuStyle, false));
78+
$this->assertEquals([' New Text'], $item->getRows($menuStyle, true));
7879
}
7980

8081
public function testGetRowsWithUnSelectedMarker() : void
@@ -90,7 +91,7 @@ public function testGetRowsWithUnSelectedMarker() : void
9091
->expects($this->exactly(2))
9192
->method('getMarker')
9293
->with(false)
93-
->will($this->returnValue('*'));
94+
->will($this->returnValue('* '));
9495

9596
$item = new SelectableItem('Item', function () {
9697
});
@@ -111,7 +112,7 @@ public function testGetRowsWithSelectedMarker() : void
111112
->expects($this->once())
112113
->method('getMarker')
113114
->with(true)
114-
->will($this->returnValue('='));
115+
->will($this->returnValue('= '));
115116

116117
$item = new SelectableItem('Item', function () {
117118
});
@@ -120,43 +121,39 @@ public function testGetRowsWithSelectedMarker() : void
120121

121122
public function testGetRowsWithItemExtra() : void
122123
{
123-
$menuStyle = $this->createMock(MenuStyle::class);
124-
125-
$menuStyle
126-
->expects($this->any())
127-
->method('getContentWidth')
128-
->will($this->returnValue(10));
124+
$terminal = $this->createMock(Terminal::class);
125+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
129126

130-
$menuStyle
131-
->expects($this->once())
132-
->method('getItemExtra')
133-
->will($this->returnValue('[EXTRA]'));
127+
$menuStyle = new MenuStyle($terminal);
128+
$menuStyle->setPaddingLeftRight(0);
129+
$menuStyle->setWidth(20);
130+
$menuStyle->setItemExtra('[EXTRA]');
131+
$menuStyle->setDisplaysExtra(true);
132+
$menuStyle->setUnselectedMarker('* ');
134133

135134
$item = new SelectableItem('Item', function () {
136135
}, true);
137-
$this->assertEquals([' Item [EXTRA]'], $item->getRows($menuStyle));
136+
$this->assertEquals(['* Item [EXTRA]'], $item->getRows($menuStyle));
138137
}
139138

140139
public function testGetRowsWithMultipleLinesWithItemExtra() : void
141140
{
142-
$menuStyle = $this->createMock(MenuStyle::class);
141+
$terminal = $this->createMock(Terminal::class);
142+
$terminal->expects($this->any())->method('getWidth')->willReturn(100);
143143

144-
$menuStyle
145-
->expects($this->any())
146-
->method('getContentWidth')
147-
->will($this->returnValue(10));
148-
149-
$menuStyle
150-
->expects($this->once())
151-
->method('getItemExtra')
152-
->will($this->returnValue('[EXTRA]'));
144+
$menuStyle = new MenuStyle($terminal);
145+
$menuStyle->setPaddingLeftRight(0);
146+
$menuStyle->setWidth(20);
147+
$menuStyle->setItemExtra('[EXTRA]');
148+
$menuStyle->setDisplaysExtra(true);
149+
$menuStyle->setUnselectedMarker('* ');
153150

154151
$item = new SelectableItem('LONG ITEM LINE', function () {
155152
}, true);
156153
$this->assertEquals(
157154
[
158-
" LONG ITEM [EXTRA]",
159-
" LINE",
155+
"* LONG ITEM [EXTRA]",
156+
" LINE",
160157
],
161158
$item->getRows($menuStyle)
162159
);

0 commit comments

Comments
 (0)