| 1 | package Data::Unixish::centerpad; | ||
| 2 | |||
| 3 | 2 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use locale; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | #use Log::Any '$log'; | ||
| 9 | |||
| 10 | 2 | ▶ | use Data::Unixish::_pad; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Data::Unixish::Util qw(%common_args); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 14 | 2 | our $DATE = '2025-02-24'; # DATE | |
| 15 | 2 | our $DIST = 'Data-Unixish'; # DIST | |
| 16 | 2 | our $VERSION = '1.574'; # VERSION | |
| 17 | |||
| 18 | 2 | our %SPEC; | |
| 19 | |||
| 20 | $SPEC{centerpad} = { | ||
| 21 | 2 | v => 1.1, | |
| 22 | summary => 'Center text to a certain column width', | ||
| 23 | description => <<'MARKDOWN', | ||
| 24 | |||
| 25 | This function can handle text containing wide characters and ANSI escape codes. | ||
| 26 | |||
| 27 | Note: to center to a certain character length instead of column width (note that | ||
| 28 | wide characters like Chinese can have width of more than 1 column in terminal), | ||
| 29 | you can turn of `mb` option even when your text contains wide characters. | ||
| 30 | |||
| 31 | MARKDOWN | ||
| 32 | args => { | ||
| 33 | %common_args, | ||
| 34 | width => { | ||
| 35 | schema => ['int*', min => 0], | ||
| 36 | req => 1, | ||
| 37 | pos => 0, | ||
| 38 | cmdline_aliases => { w => {} }, | ||
| 39 | }, | ||
| 40 | ansi => { | ||
| 41 | summary => 'Whether to handle ANSI escape codes', | ||
| 42 | schema => ['bool', default => 0], | ||
| 43 | }, | ||
| 44 | mb => { | ||
| 45 | summary => 'Whether to handle wide characters', | ||
| 46 | schema => ['bool', default => 0], | ||
| 47 | }, | ||
| 48 | char => { | ||
| 49 | summary => 'Character to use for padding', | ||
| 50 | schema => ['str*', len=>1, default=>' '], | ||
| 51 | description => <<'MARKDOWN', | ||
| 52 | |||
| 53 | Character should have column width of 1. The default is space (ASCII 32). | ||
| 54 | |||
| 55 | MARKDOWN | ||
| 56 | cmdline_aliases => { c => {} }, | ||
| 57 | }, | ||
| 58 | trunc => { | ||
| 59 | summary => 'Whether to truncate text wider than specified width', | ||
| 60 | schema => ['bool', default => 0], | ||
| 61 | }, | ||
| 62 | }, | ||
| 63 | tags => [qw/itemfunc text/], | ||
| 64 | }; | ||
| 65 | 2 | ▶ | sub centerpad { |
|
Subroutine
centerpad: called CC=1 SCAR=0.0
Pod
documented
| |||
| 66 | 2 | my %args = @_; | |
| 67 | 2 | Data::Unixish::_pad::_pad("c", %args); | |
| 68 | } | ||
| 69 | |||
| 70 | 2 | ▶ | sub _centerpad_begin { Data::Unixish::_pad::__pad_begin('c', @_) } |
|
Subroutine
_centerpad_begin: called CC=1 SCAR=0.0
| |||
| 71 | 12 | ▶ | sub _centerpad_item { Data::Unixish::_pad::__pad_item('c', @_) } |
|
Subroutine
_centerpad_item: called CC=1 SCAR=0.0
| |||
| 72 | |||
| 73 | 2 | 1; | |
| 74 | # ABSTRACT: Center text to a certain column width | ||
| 75 | |||
| 76 | __END__ | ||