#!perl

our $DATE = '2015-07-21'; # DATE
our $VERSION = '0.01'; # VERSION

# IFUNBUILT
use strict;
# END IFUNBUILT

my %CODES = (
    reset         => "\e[0m",
    bold          => "\e[1m",
    dim           => "\e[2m",
    italic        => "\e[3m",
    underline     => "\e[4m",
    inverse       => "\e[7m",
    hidden        => "\e[8m",
    strikethrough => "\e[9m",

    black   => "\e[30m",
    red     => "\e[31m",
    green   => "\e[32m",
    yellow  => "\e[33m",
    blue    => "\e[34m",
    magenta => "\e[35m",
    cyan    => "\e[36m",
    white   => "\e[37m",
    gray    => "\e[37m",

    bgBlack   => "\e[40m",
    bgRed     => "\e[41m",
    bgGreen   => "\e[42m",
    bgYellow  => "\e[43m",
    bgBlue    => "\e[44m",
    bgMagenta => "\e[45m",
    bgCyan    => "\e[46m",
    bgWhite   => "\e[47m",
);

sub parse_styles {
    my $ansi = "";
    my $bold;
    for (@_) {
        $CODES{$_} or die "Invalid style: $_\n";
        $ansi .= $CODES{$_};
    }
    $ansi;
}

require Win32::Console::ANSI if $^O eq 'MSWin32';

if (grep {$_ eq '--help'} @ARGV) {
    print <<'EOF';
chalk - Colorize text for terminal output

Usage:
  % chalk <style> ... <string>
  % echo <string> | chalk <style> ...

Example:
  % chalk red bold 'Unicorns & Rainbows'

EOF
    exit 0;
} elsif (-t STDIN) {
    die "Input required\n" unless @ARGV >= 2;
    my $string = pop @ARGV;
    my $ansi = parse_styles(@ARGV);
    print $ansi if (-t STDOUT);
    print $string, "\n";
    print $CODES{reset} if (-t STDOUT);
} else {
    die "Input required\n" unless @ARGV >= 1;
    my $ansi = parse_styles(@ARGV);
    print $ansi if (-t STDOUT);
    print while <STDIN>;
    print $CODES{reset};
}

1;
# ABSTRACT: Colorize text for terminal output
# PODNAME: chalk

__END__

=pod

=encoding UTF-8

=head1 NAME

chalk - Colorize text for terminal output

=head1 VERSION

This document describes version 0.01 of chalk (from Perl distribution App-chalk), released on 2015-07-21.

=head1 SYNOPSIS

Usage:

 % chalk <style> ... <string>
 % echo <string> | chalk <style> ...

Example:

 % chalk red bold 'Unicorns & Rainbows'

or:

 % command-that-produce-some-text | chalk red bold

=head1 DESCRIPTION

This is a Perl port of node.js' chalk-cli utility
(L<https://www.npmjs.com/package/chalk-cli>). This Perl port is basically the
same as the node.js' version, but with a smaller startup overhead.

=head1 FAQ

=head2 What are the supported styles?

Modifiers:

    reset
    bold
    dim
    italic (not widely supported)
    underline
    inverse
    hidden
    strikethrough (not widely supported)

Colors:

    black
    red
    green
    yellow
    blue
    magenta
    cyan
    white
    gray

Background colors:

    bgBlack
    bgRed
    bgGreen
    bgYellow
    bgBlue
    bgMagenta
    bgCyan
    bgWhite

=head2 What about the library version of chalk?

We already have L<Term::ANSIColor> in Perl. Use that.

=head1 SEE ALSO

L<Term::ANSIColor>

L<https://www.npmjs.com/package/chalk>

L<https://www.npmjs.com/package/chalk-cli>

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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) 2015 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
