1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
6
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Michael Woodward <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
trait SelectableTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $text = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $showItemExtra = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
private $disabled = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $numberOfRows = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $startRowNumber = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the number of terminal rows the item takes |
40
|
|
|
*/ |
41
|
|
|
public function getNumberOfRows() { |
42
|
|
|
return $this->numberOfRows; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets the row number the item starts at in the frame |
47
|
|
|
*/ |
48
|
|
|
public function setStartRowNumber(int $rowNumber) { |
49
|
|
|
$this->startRowNumber = $rowNumber; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the row number the item starts at in the frame |
54
|
|
|
*/ |
55
|
|
|
public function getStartRowNumber() { |
56
|
|
|
return $this->startRowNumber; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The output text for the item |
61
|
|
|
*/ |
62
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
63
|
|
|
{ |
64
|
|
|
$marker = sprintf("%s ", $style->getMarker($selected)); |
65
|
|
|
|
66
|
|
|
$length = $style->getDisplaysExtra() |
67
|
|
|
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2) |
68
|
|
|
: $style->getContentWidth(); |
69
|
|
|
|
70
|
|
|
$rows = explode( |
71
|
|
|
"\n", |
72
|
|
|
StringUtil::wordwrap( |
73
|
|
|
sprintf('%s%s', $marker, $this->text), |
74
|
|
|
$length, |
75
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->numberOfRows = count($rows); |
80
|
|
|
|
81
|
|
|
return array_map(function ($row, $key) use ($style, $length) { |
82
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
83
|
|
|
|
84
|
|
|
if ($key === 0) { |
85
|
|
|
return $this->showItemExtra |
86
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra()) |
87
|
|
|
: $text; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $text; |
91
|
|
|
}, $rows, array_keys($rows)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Can the item be selected |
96
|
|
|
*/ |
97
|
|
|
public function canSelect() : bool |
98
|
|
|
{ |
99
|
|
|
return !$this->disabled; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function showsItemExtra() : bool |
103
|
|
|
{ |
104
|
|
|
return $this->showItemExtra; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Enable showing item extra |
109
|
|
|
*/ |
110
|
|
|
public function showItemExtra() : void |
111
|
|
|
{ |
112
|
|
|
$this->showItemExtra = true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Disable showing item extra |
117
|
|
|
*/ |
118
|
|
|
public function hideItemExtra() : void |
119
|
|
|
{ |
120
|
|
|
$this->showItemExtra = false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|