#!perl

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

use 5.010;
use strict;
use warnings;

sub transform_bold {
    if ($_[0] == 1) {
        return @_;
    } else {
        return (1, @_);
    }
}

sub transform_inverse {
    if ($_[0] == 7) {
        return ();
    } else {
        return (7, @_);
    }
}

sub transform_mono {
    if ($_[0] >= 30 && $_[0] <= 38) {
        return (37);
    } elsif ($_[0] >= 40 && $_[0] <= 48) {
        return (47);
    } else {
        return @_;
    }
}

sub transform_nobold {
    if ($_[0] == 1) {
        return ();
    } else {
        return @_;
    }
}

sub transform_noop { @_ }

sub transform_pastel {
    state $pastel_ansifgs = [
        [38, 5,   0], # 0 black
        [38, 5, 203], # 1 red
        [38, 5, 113], # 2 green
        [38, 5, 192], # 3 yellow
        [38, 5,  69], # 4 blue
        [38, 5, 104], # 5 magenta
        [38, 5,  74], # 6 cyan
        [38, 5, 246], # 7 white (gray)
    ];
    state $pastel_ansibgs = [map { [48, 5, $_->[2]] } @$pastel_ansifgs];

    if ($_[0] >= 30 && $_[0] <= 37) {
        return @{ $pastel_ansifgs->[$_[0]-30] };
    } elsif ($_[0] >= 40 && $_[0] <= 47) {
        return  @{ $pastel_ansibgs->[$_[0]-40] };
    } else {
        return @_;
    }
}

if (@ARGV != 1 || (grep {$_ eq '--help'} @ARGV)) {
    print <<'EOF';
lens - Transform colors in terminal output

Usage:
  % command-that-produces-colored-output | lens <transform-name>

List of available transforms:
  mono - replace all colors with gray
  nobold - remove bold
  noop - do no transform
  pastel - pastelize colors

EOF
    exit 0;
} else {
    my $tname = shift;
    defined &{"transform_$tname"} or die "Unknown transform '$tname'\n";
    my $tsub = \&{"transform_$tname"};
    my $transform = sub {
        my @codes = split ';', shift;
        my @res;
        while (@codes) {
            my @args;
            if (($codes[0] == 38 || $codes[0] == 48) && @codes > 1) {
                if ($codes[1] == 2) {
                    @args = splice @codes, 0, 5;
                } elsif ($codes[1] == 5) {
                    @args = splice @codes, 0, 3;
                } else {
                    @args = (shift @codes);
                }
            } else {
                @args = (shift @codes);
            }
            push @res, $tsub->(@args);
        }
        join ';', @res;
    };
    while (<STDIN>) {
        s/\e\[(.+?)m/"\e[" . $transform->($1) . "m"/eg;
        print;
    }
}

1;
# ABSTRACT: Transform colors in terminal output
# PODNAME: lens

__END__

=pod

=encoding UTF-8

=head1 NAME

lens - Transform colors in terminal output

=head1 VERSION

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

=head1 SYNOPSIS

Usage:

 % command-that-produces-colored-output | lens <transform-name>

Available transforms:

=over

=item * bold

Give bold to all colors.

=item * inverse

Inverse all colors.

=item * mono

Replace all colors with gray.

=item * nobold

Remove bold.

=item * noop

Do no transform.

=item * pastel

Pastelize colors.

=back

=head1 DESCRIPTION

=head1 SEE ALSO

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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
