#!perl

use strict;
use warnings;

use Getopt::Long qw(:config gnu_getopt no_ignore_case);
BEGIN { require Win32::Console::ANSI if $^O eq 'MSWin32' }

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-09-29'; # DATE
our $DIST = 'App-chalk'; # DIST
our $VERSION = '0.050'; # VERSION

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",
);

my %opts;
GetOptions(
    'help|h' => \$opts{help},
    'n'      => \$opts{no_newline},
);

if ($opts{help}) {
    print <<'EOF';
chalk - Colorize text for terminal output

Usage:
  % chalk [options] <style> ... <string>
  % echo <string> | chalk [options] <style> ...

Example:
  % chalk red bold 'Unicorns & Rainbows'

Options:
  -n          Do not output the trailing newline
  --help, -h  Display help

EOF
    exit 0;
}

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

my $in_interactive  = (-t STDIN); ## no critic: InputOutput::ProhibitInteractiveTest
my $out_interactive = (-t STDOUT); ## no critic: InputOutput::ProhibitInteractiveTest
my $supports_color  = $ENV{FORCE_COLOR} ? 1 :
    defined($ENV{COLOR}) ? $ENV{COLOR} :
    $out_interactive;

if ($in_interactive) {
    die "Input required\n" unless @ARGV >= 2;
    my $string = pop @ARGV;
    my $ansi = parse_styles(@ARGV);
    print $ansi         if $supports_color;
    print $string;
    print $CODES{reset} if $supports_color;
    print "\n" unless $opts{no_newline};
} else {
    die "Input required\n" unless @ARGV >= 1;
    my $ansi = parse_styles(@ARGV);
    while (<STDIN>) {
        chomp;
        print $ansi         if $supports_color;
        print;
        print $CODES{reset} if $supports_color;
        print "\n" unless $opts{no_newline};
    }
}

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.050 of chalk (from Perl distribution App-chalk), released on 2022-09-29.

=head1 SYNOPSIS

Usage:

 % chalk [options] <style> ... <string>
 % echo <string> | chalk [options] <style> ...

Example:

 % chalk red bold 'Unicorns & Rainbows'

=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 OPTIONS

=head2 --help, -h

Display help message and exit.

=head2 -n

Do not output the trailing newline.

=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 ENVIRONMENT

=head2 COLOR => bool

Can be set to 0 or 1 to always disable or always enable color.

=head2 FORCE_COLOR => bool

Can be set to 1 to always enable color.

=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 SEE ALSO

L<Term::ANSIColor>

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

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

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2022, 2015 by perlancar <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.

=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.

=cut
