package String::Pad;

our $DATE = '2018-09-26'; # DATE
our $VERSION = '0.020'; # VERSION

use 5.010001;
use strict;
use warnings;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
                       pad
               );

sub pad {
    my ($text, $width, $which, $padchar, $is_trunc) = @_;
    if ($which) {
        $which = substr($which, 0, 1);
    } else {
        $which = "r";
    }
    $padchar //= " ";

    my $w = length($text);
    if ($is_trunc && $w > $width) {
        $text = substr($text, 0, $width, 1);
    } else {
        if ($which eq 'l') {
            $text = ($padchar x ($width-$w)) . $text;
        } elsif ($which eq 'c') {
            my $n = int(($width-$w)/2);
            $text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n));
        } else {
            $text .= ($padchar x ($width-$w));
        }
    }
    $text;
}

1;
# ABSTRACT: String padding routines

__END__

=pod

=encoding UTF-8

=head1 NAME

String::Pad - String padding routines

=head1 VERSION

This document describes version 0.020 of String::Pad (from Perl distribution String-Pad), released on 2018-09-26.

=head1 FUNCTIONS

=head2 pad($text, $width[, $which[, $padchar[, $truncate]]]) => STR

Return C<$text> padded with C<$padchar> to C<$width> columns.

C<$which> is either "r" or "right" for padding on the right (the default if not
specified), "l" or "left" for padding on the right, or "c" or "center" or
"centre" for left+right padding to center the text.

C<$padchar> is whitespace if not specified. It should be string having the width
of 1 column.

C<$truncate> is boolean. When set to 1, then text will be truncated when it is
longer than C<$width>.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/String-Pad>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-String-Pad>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=String-Pad>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2018, 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
