Completed
Push — allow-padding-br ( f14d7c )
by Craig
04:42 queued 51s
created

Padding::br()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
5
class Padding extends DynamicTerminalObject
6
{
7
    /**
8
     * The length that lines should be padded to
9
     *
10
     * @var integer $length
11
     */
12
    protected $length = 0;
13
14
    /**
15
     * The character(s) that should be used to pad
16
     *
17
     * @var string $char
18
     */
19
    protected $char = '.';
20
21
22
    /**
23
     * If they pass in a padding character, set the char
24
     *
25
     * @param int $length
26
     * @param string $char
27
     */
28
    public function __construct($length = null, $char = null)
29
    {
30
        if ($length !== null) {
31
            $this->length($length);
32
        }
33
34
        if (is_string($char)) {
35
            $this->char($char);
36
        }
37
    }
38
39
    /**
40
     * Set the character(s) that should be used to pad
41
     *
42
     * @param string $char
43
     *
44
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
45
     */
46
    public function char($char)
47
    {
48
        $this->char = $char;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Set the length of the line that should be generated
55
     *
56
     * @param integer $length
57
     *
58
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
59
     */
60
    public function length($length)
61
    {
62
        $this->length = $length;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get the length of the line based on the width of the terminal window
69
     *
70
     * @return integer
71
     */
72
    protected function getLength()
73
    {
74
        if (!$this->length) {
75
            $this->length = $this->util->width();
76
        }
77
78
        return $this->length;
79
    }
80
81
    /**
82
     * Pad the content with the characters
83
     *
84
     * @param string $content
85
     *
86
     * @return string
87
     */
88
    protected function padContent($content)
89
    {
90
        if (strlen($this->char) > 0) {
91
            $length = $this->getLength();
92
            $padding_length = ceil($length / mb_strlen($this->char));
93
94
            $padding = str_repeat($this->char, $padding_length);
95
            $content .= mb_substr($padding, 0, $length - mb_strlen($content));
96
        }
97
98
        return $content;
99
    }
100
101
    /**
102
     * Output the content and pad to the previously defined length
103
     *
104
     * @param string $content
105
     *
106
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
107
     */
108
    public function label($content)
109
    {
110
        // Handle long labels by splitting them across several lines
111
        $lines = [];
112
        $stop = mb_strlen($content);
113
        $width = $this->util->width();
114
        for ($i = 0; $i < $stop; $i += $width) {
115
            $lines[] = mb_substr($content, $i, $width);
116
        }
117
        $content = array_pop($lines);
118
119
        foreach ($lines as $line) {
120
            $this->output->write($this->parser->apply($line));
121
        }
122
123
        $content = $this->padContent($content);
124
        $content = $this->parser->apply($content);
125
126
        $this->output->sameLine();
127
        $this->output->write($content);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Output result
134
     *
135
     * @param string $content
136
     */
137
    public function result($content)
138
    {
139
        $this->output->write($this->parser->apply(' ' . $content));
140
    }
141
142
143
    /**
144
     * Output a line break
145
     */
146
147
    public function br()
148
    {
149
        $this->output->write("");
150
    }
151
}
152