App-chalk
view release on metacpan or search on metacpan
script/chalk view on Meta::CPAN
#!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>
( run in 1.624 second using v1.01-cache-2.11-cpan-39bf76dae61 )