forked from php-school/cli-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectableTrait.php
More file actions
89 lines (74 loc) · 1.95 KB
/
Copy pathSelectableTrait.php
File metadata and controls
89 lines (74 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace PhpSchool\CliMenu\MenuItem;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;
/**
* @author Michael Woodward <[email protected]>
*/
trait SelectableTrait
{
/**
* @var string
*/
private $text = '';
/**
* @var bool
*/
private $showItemExtra = false;
/**
* @var bool
*/
private $disabled = false;
/**
* The output text for the item
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
$marker = sprintf("%s", $style->getMarker($selected));
$length = $style->getDisplaysExtra()
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)
: $style->getContentWidth();
$rows = explode(
"\n",
StringUtil::wordwrap(
sprintf('%s%s', $marker, $this->text),
$length,
sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
)
);
return array_map(function ($row, $key) use ($style, $length) {
$text = $this->disabled ? $style->getDisabledItemText($row) : $row;
if ($key === 0) {
return $this->showItemExtra
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra())
: $text;
}
return $text;
}, $rows, array_keys($rows));
}
/**
* Can the item be selected
*/
public function canSelect() : bool
{
return !$this->disabled;
}
public function showsItemExtra() : bool
{
return $this->showItemExtra;
}
/**
* Enable showing item extra
*/
public function showItemExtra() : void
{
$this->showItemExtra = true;
}
/**
* Disable showing item extra
*/
public function hideItemExtra() : void
{
$this->showItemExtra = false;
}
}