Skip to content

Commit

Permalink
clean up docs, fix some tests, fix imports on other objects, and vari…
Browse files Browse the repository at this point in the history
…ous other things
  • Loading branch information
Paul Miller committed Jun 22, 2014
1 parent d4a9a6a commit 71632ac
Show file tree
Hide file tree
Showing 14 changed files with 521 additions and 302 deletions.
9 changes: 5 additions & 4 deletions .gitignore
@@ -1,8 +1,9 @@
Makefile
Makefile.old
blib
pm_to_blib
TEST
dist
Makefile
Makefile.old
MYMETA.json
MYMETA.yml
pm_to_blib
t.html
TEST
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@
(There's also a coloralias() sub now, but you can't replace standard
colors, so it's not useful here.)
- use color() and colorvalid() instead of more scope hacking
- export color() and colorvalid() (et al) insteadof scope hacking
- move most of the hi command to App::HI

2.7186: Sat Jun 21 2014
Expand Down
3 changes: 1 addition & 2 deletions MANIFEST
Expand Up @@ -2,13 +2,12 @@ bin/hi
Changes
lib/App/HI.pm
lib/Term/ANSIColorx/AutoFilterFH.pm
lib/Term/ANSIColorx/AutoFilterFH.pod
lib/Term/ANSIColorx/ColorNicknames.pm
lib/Term/ANSIColorx/ColorNicknames.pod
Makefile.PL
MANIFEST
README
t/01_color_some_things.t
t/02_fix_some_things.t
t/05_color_file_handle.t
t/07_test_hi.t
t/09_test_no_extra_and_trunc.t
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.SKIP
Expand Up @@ -4,4 +4,5 @@
^Makefile.old
^MANIFEST\.SKIP$
new-version.sh
t.html
TEST
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -12,6 +12,7 @@ WriteMakefile(
PREREQ_PM => {
'Term::ANSIColor' => 4.0,
'Text::Table' => 0,
'Text::Size' => 0,
},

($ExtUtils::MakeMaker::VERSION ge '6.48'?
Expand Down
73 changes: 67 additions & 6 deletions lib/App/HI.pm
Expand Up @@ -3,20 +3,33 @@ package App::HI;

use strict;
use Text::Table;
use Term::Size;

our $VERSION = '2.7187';

sub fire_filter {
my $truncate = shift;
sub top_matter {
my $no_extra = shift;

unless( $no_extra ) {
eval q { use Term::ANSIColorx::ColorNicknames; 1 }
if( $no_extra ) {
eval q { use Term::ANSIColorx::AutoFilterFH qw(filtered_handle); 1 }
or die $@;

} else {
eval q { use Term::ANSIColorx::AutoFilterFH qw(colorpackage=Term::ANSIColorx::ColorNicknames filtered_handle); 1 }
or die $@;
}

eval q { use Term::ANSIColorx::AutoFilterFH qw(filtered_handle); 1 }
or die $@;
if( @_ ) {
eval qq/ use Term::ANSIColor qw(@_); 1 /
or die $@;
}
}

sub fire_filter {
my $truncate = shift;
my $no_extra = shift;

top_matter( $no_extra );

my $newstdout = filtered_handle(\*STDOUT, @ARGV);
$| = 1; my $oldstdout = select $newstdout; $|=1;
Expand All @@ -31,6 +44,54 @@ sub fire_filter {
}

sub list_colors {
my $truncate = shift;
my $no_extra = shift;

top_matter( $no_extra => qw(color colorvalid) );

my $table;

my @colors = (
qw( black red green yellow blue magenta cyan white ),
map("bold $_", qw( black red green yellow blue magenta cyan white )),
map("bright_$_", qw( black red green yellow blue magenta cyan white )),

"white on_black", "white on_red", "blue on_green", "black on_yellow",
"white on_blue", "white on_magenta", "black on_cyan", "black on_white"
);

@colors = grep {
my $valid = colorvalid($_);
warn "$_ isn't valid" unless $valid;

$valid} @colors;

my ($columns, $rows) = Term::Size::chars *STDOUT;

$columns --;

my $m = 20;
UGH_SO_BAD: {
# XXX: this is so in-efficient it makes my soul hurt
$table = Text::Table->new;

my @row;
for(@colors) {
push @row, $_;

unless( @row % $m ) {
$table->add(map {color($_) . $_ . color("reset")} @row);
@row = ();
}
}

$table->add(@row) if @row;

$m -= 2;
redo UGH_SO_BAD if $table->width > $columns;
}

print $table;
}

__END__
Expand Down
113 changes: 100 additions & 13 deletions lib/Term/ANSIColorx/AutoFilterFH.pm
@@ -1,28 +1,42 @@

package Term::ANSIColorx::AutoFilterFH;

use strict;
use warnings;
no warnings 'uninitialized'; # sometimes it's ok to compare undef... jesus

use Carp;
use Symbol;
use Tie::Handle;
use Term::ANSIColor qw(color colorvalid);
use base 'Tie::StdHandle';
use base 'Exporter';

our $VERSION = '2.7187';
sub import {
my @__;
my $color_package = Term::ANSIColorx::ColorNicknames->can("import") ? "Term::ANSIColorx::ColorNicknames" : "Term::ANSIColor";

for(@_) {
if( m/\Acolor.?package\s*=\s*(\S+)\z/ ) {
$color_package = $1

} else {
push @__, $_
}
}

eval qq{ use $color_package qw(color colorvalid); 1 }
or die $@;

__PACKAGE__->export_to_level(1, @__);
}

use common::sense;

our $VERSION = '2.7187';
our @EXPORT_OK = qw(filtered_handle);

my %pf2t;
my %orig;
my %pats;
my %trun;

my @icolors = ("");
my $RESET = color("reset");
my (@icolors, $RESET);

# DESTROY {{{
sub DESTROY {
Expand Down Expand Up @@ -94,6 +108,9 @@ sub filtered_handle {
my ($fh, @patterns) = @_;
croak "filtered_handle(globref, \@patterns)" unless ref($fh) eq "GLOB";

@icolors = ("");
$RESET = color("reset");

my @pats;
while( (my ($pat,$color) = splice @patterns, 0, 2) ) {
croak "\@patterns should contain an even number of items" unless defined $color;
Expand All @@ -106,13 +123,10 @@ sub filtered_handle {
# die unless all the elements of @uc are all caps exports of
# Term::ANSIColor

$color =~ s/[^\w]/ /g;
$color =~ s/on (\w+)/on_$1/g;

croak "color \"$color\" unknown" unless colorvalid($color);
$color = color($color);

my $color = color($color);
my ($l) = grep {$color eq $icolors[$_]} 0 .. $#icolors;
my ($l) = grep {$color eq $icolors[$_]} 0 .. $#icolors;

unless($l) {
push @icolors, $color;
Expand All @@ -136,3 +150,76 @@ sub filtered_handle {
# }}}

"true";

__END__
=head1 NAME
Term::ANSIColorx::AutoFilterFH - automatically color-highlight a stream
=head1 SYNOPSIS
use Term::ANSIColorx::ColorNicknames; # optional
use Term::ANSIColorx::AutoFilterFH qw(filtered_handle);
my $filtered_stdout = filtered_handle(\*STDOUT,
'jettero' => 'bold-blue',
'nobody' => 'sky', # same as jettero under ColorNicks, or error
'root' => 'red',
);
print "This has colors: jettero nobody root\n";
select $filtered_stdout;
print "This also has colors. -jettero\n";
$filtered_stdout->set_truncate(80);
print "This line is only 80 characters... ", ("." x 120), "\n";
=head1 DESCRIPTION
I wanted a way to inject colors into places that didn't otherwise support it. I
also wanted to make my L<hi> utility as short as possible -- and it worked.
L<hi> is barely three lines, not including the options.
=head1 C<filter_handle()>
This function returns a tied handle with some magic installed. You can print to
it and select it. It has one method you can invoke as well.
=head1 C<set_truncate()>
Use this method to set a characters-per-line limit. Give it an C<undef> or a
C<0> to disable it again. Caveat: The truncator assumes input to C<PRINT()>
is a I<line> and as such, the results will seem incorrect when the printing
non-lines. For example, this will not work right:
$truncated_handle->set_truncate(80)
select $truncated_handle;
print "neato: ", ("." x 120); # this will gain a newline at char 81
print "\n";
=head1 FAQ
Q: You don't seem to understand Tie::Handle, shouldn't you fix it using my
immense knowledge of perl FH globs?
A: You got that right -- although the module functions correctly -- if you want
to help, let me know, or fork the project on github.
=head1 REPORTING BUGS
You can report bugs either via rt.cpan.org or via the issue tracking system on
github. I'm likely to notice either fairly quickly.
=head1 AUTHOR
Paul Miller C<< <jettero@cpan.org> >>
=head1 COPYRIGHT
Copyright 2009 Paul Miller -- released under the GPL
=head1 SEE ALSO
perl(1), L<Term::ANSIColor>, L<Tie::Handle>
71 changes: 0 additions & 71 deletions lib/Term/ANSIColorx/AutoFilterFH.pod

This file was deleted.

0 comments on commit 71632ac

Please sign in to comment.