Skip to content

Commit 14b8a08

Browse files
authored
Merge pull request #120 from urlund/master
Added str_pad optional arguments
2 parents 166b674 + 2da8f24 commit 14b8a08

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/cli/Colors.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,15 @@ static public function width( $string, $pre_colorized = false, $encoding = false
207207
* @param int $length The display length.
208208
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
209209
* @param string|bool $encoding Optional. The encoding of the string. Default false.
210+
* @param int $pad_type Optional. Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
210211
* @return string
211212
*/
212-
static public function pad( $string, $length, $pre_colorized = false, $encoding = false ) {
213+
static public function pad( $string, $length, $pre_colorized = false, $encoding = false, $pad_type = STR_PAD_RIGHT ) {
213214
$real_length = self::width( $string, $pre_colorized, $encoding );
214215
$diff = strlen( $string ) - $real_length;
215216
$length += $diff;
216217

217-
return str_pad( $string, $length );
218+
return str_pad( $string, $length, ' ', $pad_type );
218219
}
219220

220221
/**

tests/test-cli.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ function test_encoded_string_pad() {
4444
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6 ) ) ); // special characters take one byte
4545
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6 ) ) ); // each character takes two bytes
4646
$this->assertEquals( 17, strlen( \cli\Colors::pad( 'עִבְרִית', 6 ) ) ); // process Hebrew vowels
47+
$this->assertEquals( 6, strlen( \cli\Colors::pad( 'hello', 6, false, false, STR_PAD_RIGHT ) ) );
48+
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6, false, false, STR_PAD_LEFT ) ) ); // special characters take one byte
49+
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6, false, false, STR_PAD_BOTH ) ) ); // each character takes two bytes
50+
$this->assertSame( 4, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_RIGHT ), 'o' ) );
51+
$this->assertSame( 9, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_LEFT ), 'o' ) );
52+
$this->assertSame( 6, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_BOTH ), 'o' ) );
53+
$this->assertSame( 1, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_RIGHT ), 'e' ) );
54+
$this->assertSame( 6, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_LEFT ), 'e' ) );
55+
$this->assertSame( 3, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_BOTH ), 'e' ) );
4756
}
4857

4958
function test_colorized_string_pad() {

0 commit comments

Comments
 (0)