package Data::Sah::Compiler::TextResultRole;
our $DATE = '2016-06-12'; # DATE
our $VERSION = '0.83'; # VERSION
use 5.010;
use strict;
use warnings;
use Mo qw(default);
use Role::Tiny;
use String::Indent ();
# can be changed to tab, for example
has indent_character => (is => 'rw', default => sub {''});
sub add_result {
my ($self, $cd, @args) = @_;
$cd->{result} //= [];
push @{ $cd->{result} }, $self->indent($cd, join("", @args));
$self;
}
sub indent {
my ($self, $cd, $str) = @_;
String::Indent::indent(
$self->indent_character x $cd->{indent_level},
$str,
);
}
sub inc_indent {
my ($self, $cd) = @_;
$cd->{indent_level}++;
}
sub dec_indent {
my ($self, $cd) = @_;
$cd->{indent_level}--;
}
sub indent_str {
my ($self, $cd) = @_;
$self->indent_character x $cd->{indent_level};
}
1;
# ABSTRACT: Role for compilers that produce text result (array of lines)
__END__
=pod
=encoding UTF-8
=head1 NAME
Data::Sah::Compiler::TextResultRole - Role for compilers that produce text result (array of lines)
=head1 VERSION
This document describes version 0.83 of Data::Sah::Compiler::TextResultRole (from Perl distribution Data-Sah), released on 2016-06-12.
=head1 ATTRIBUTES
=head2 indent_character => STR
=head1 METHODS
=head2 $c->add_result($cd, @arg)
Append result to C<< $cd->{result} >>. Will use C<< $cd->{indent_level} >> to
indent the line. Used by compiler; users normally do not need this.
=head2 $c->inc_indent($cd)
Increase indent level. This is done by increasing C<< $cd->{indent_level} >> by
1.
=head2 $c->dec_indent($cd)
Decrease indent level. This is done by decreasing C<< $cd->{indent_level} >> by
1.
=head2 $c->indent_str($cd)
Shortcut for C<< $c->indent_character x $cd->{indent_level} >>.
=head2 $c->indent($cd, $str) => STR
Indent each line in $str with indent_str and return the result.
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah>.
=head1 SOURCE
Source repository is at L<https://github.com/sharyanto/perl-Data-Sah>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah>
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) 2016 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