Skip to content

Commit 327c85f

Browse files
committed
Split Item - Item Extra
1 parent 1bf30f8 commit 327c85f

File tree

2 files changed

+140
-13
lines changed

2 files changed

+140
-13
lines changed

src/MenuItem/SplitItem.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
108108
$this->setDefaultSelectedItem();
109109
}
110110

111-
$length = floor($style->getContentWidth() / $numberOfItems) - $this->margin;
111+
$length = $style->getDisplaysExtra()
112+
? floor($style->getContentWidth() / $numberOfItems) - (mb_strlen($style->getItemExtra()) + 2)
113+
: floor($style->getContentWidth() / $numberOfItems);
114+
115+
$length -= $this->margin;
116+
112117
$missingLength = $style->getContentWidth() % $numberOfItems;
113118

114119
return $this->buildRows(
@@ -118,37 +123,48 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
118123
? sprintf('%s ', $style->getMarker($isSelected))
119124
: '';
120125

126+
$itemExtra = '';
127+
if ($style->getDisplaysExtra()) {
128+
$itemExtra = $item->showsItemExtra()
129+
? sprintf(' %s', $style->getItemExtra())
130+
: sprintf(' %s', str_repeat(' ', mb_strlen($style->getItemExtra())));
131+
}
132+
121133
return $this->buildCell(
122134
explode("\n", StringUtil::wordwrap(sprintf('%s%s', $marker, $item->getText()), $length)),
123135
$length,
124136
$style,
125-
$isSelected
137+
$isSelected,
138+
$itemExtra
126139
);
127140
}, array_keys($this->items), $this->items),
141+
$style,
128142
$missingLength,
129143
$length
130144
);
131145
}
132146

133-
private function buildRows(array $cells, int $missingLength, int $length) : array
147+
private function buildRows(array $cells, MenuStyle $style, int $missingLength, int $length) : array
134148
{
149+
$extraPadLength = $style->getDisplaysExtra() ? 2 + mb_strlen($style->getItemExtra()) : 0;
150+
135151
return array_map(
136-
function ($i) use ($cells, $length, $missingLength) {
137-
return $this->buildRow($cells, $i, $length, $missingLength);
152+
function ($i) use ($cells, $length, $missingLength, $extraPadLength) {
153+
return $this->buildRow($cells, $i, $length, $missingLength, $extraPadLength);
138154
},
139155
range(0, max(array_map('count', $cells)) - 1)
140156
);
141157
}
142158

143-
private function buildRow(array $cells, int $index, int $length, int $missingLength) : string
159+
private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string
144160
{
145161
return sprintf(
146162
'%s%s',
147163
implode(
148164
'',
149165
array_map(
150-
function ($cell) use ($index, $length) {
151-
return $cell[$index] ?? str_repeat(' ', $length + $this->margin);
166+
function ($cell) use ($index, $length, $extraPadLength) {
167+
return $cell[$index] ?? str_repeat(' ', $length + $this->margin + $extraPadLength);
152168
},
153169
$cells
154170
)
@@ -157,9 +173,14 @@ function ($cell) use ($index, $length) {
157173
);
158174
}
159175

160-
private function buildCell(array $content, int $length, MenuStyle $style, bool $isSelected) : array
161-
{
162-
return array_map(function ($row) use ($length, $style, $isSelected) {
176+
private function buildCell(
177+
array $content,
178+
int $length,
179+
MenuStyle $style,
180+
bool $isSelected,
181+
string $itemExtra
182+
) : array {
183+
return array_map(function ($row, $index) use ($length, $style, $isSelected, $itemExtra) {
163184
$invertedColoursSetCode = $isSelected
164185
? $style->getInvertedColoursSetCode()
165186
: '';
@@ -168,14 +189,15 @@ private function buildCell(array $content, int $length, MenuStyle $style, bool $
168189
: '';
169190

170191
return sprintf(
171-
'%s%s%s%s%s',
192+
'%s%s%s%s%s%s',
172193
$invertedColoursSetCode,
173194
$row,
174195
str_repeat(' ', $length - mb_strlen($row)),
196+
$index === 0 ? $itemExtra : str_repeat(' ', mb_strlen($itemExtra)),
175197
$invertedColoursUnsetCode,
176198
str_repeat(' ', $this->margin)
177199
);
178-
}, $content);
200+
}, $content, array_keys($content));
179201
}
180202

181203
public function setSelectedItemIndex(int $index) : void

test/MenuItem/SplitItemTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,111 @@ public function testGetRowsWithMultipleLinesWithOneItemSelected() : void
262262
$item->getRows($menuStyle, true)
263263
);
264264
}
265+
266+
public function testGetRowsWithItemExtra() : void
267+
{
268+
$menuStyle = $this->createMock(MenuStyle::class);
269+
270+
$menuStyle
271+
->expects($this->any())
272+
->method('getContentWidth')
273+
->will($this->returnValue(50));
274+
275+
$menuStyle
276+
->expects($this->any())
277+
->method('getItemExtra')
278+
->will($this->returnValue('[EXTRA]'));
279+
280+
$menuStyle
281+
->expects($this->any())
282+
->method('getDisplaysExtra')
283+
->willReturn(true);
284+
285+
$item = new SplitItem(
286+
[
287+
new SelectableItem('Item 1', function () {
288+
}, true),
289+
new SelectableItem('Item 2', function () {
290+
}, true)
291+
]
292+
);
293+
294+
self::assertEquals([' Item 1 [EXTRA] Item 2 [EXTRA] '], $item->getRows($menuStyle));
295+
}
296+
297+
public function testGetRowsWithMultipleLinesWithItemExtra() : void
298+
{
299+
$menuStyle = $this->createMock(MenuStyle::class);
300+
301+
$menuStyle
302+
->expects($this->any())
303+
->method('getContentWidth')
304+
->will($this->returnValue(50));
305+
306+
$menuStyle
307+
->expects($this->any())
308+
->method('getItemExtra')
309+
->will($this->returnValue('[EXTRA]'));
310+
311+
$menuStyle
312+
->expects($this->any())
313+
->method('getDisplaysExtra')
314+
->willReturn(true);
315+
316+
$item = new SplitItem(
317+
[
318+
new SelectableItem("Item 1\nItem 1", function () {
319+
}, true),
320+
new SelectableItem("Item 2\nItem 2", function () {
321+
}, true)
322+
]
323+
);
324+
325+
self::assertEquals(
326+
[
327+
' Item 1 [EXTRA] Item 2 [EXTRA] ',
328+
'Item 1 Item 2 ',
329+
],
330+
$item->getRows($menuStyle)
331+
);
332+
}
333+
334+
public function testGetRowsWithMultipleLinesWithItemExtraOnOne() : void
335+
{
336+
$menuStyle = $this->createMock(MenuStyle::class);
337+
338+
$menuStyle
339+
->expects($this->any())
340+
->method('getContentWidth')
341+
->will($this->returnValue(50));
342+
343+
$menuStyle
344+
->expects($this->any())
345+
->method('getItemExtra')
346+
->will($this->returnValue('[EXTRA]'));
347+
348+
$menuStyle
349+
->expects($this->any())
350+
->method('getDisplaysExtra')
351+
->willReturn(true);
352+
353+
$item = new SplitItem(
354+
[
355+
new SelectableItem("Item 1\nItem 1", function () {
356+
}),
357+
new SelectableItem("Item 2\nItem 2", function () {
358+
}, true)
359+
]
360+
);
361+
362+
self::assertEquals(
363+
[
364+
' Item 1 Item 2 [EXTRA] ',
365+
'Item 1 Item 2 ',
366+
],
367+
$item->getRows($menuStyle)
368+
);
369+
}
265370

266371
public function testGetTextThrowsAnException() : void
267372
{

0 commit comments

Comments
 (0)