Skip to content

Commit

Permalink
1.17 - Added table command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timofey Potapov committed Jun 10, 2024
1 parent fb9376f commit 2ed9123
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 28 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ my $builder = $class->new(
'YAML::XS' => '0',
'Data::Printer' => '0',
'App::Pod' => '0.36',
'Term::Table' => '0',
},
add_to_cleanup => [ 'e-*', 'MANIFEST*.bak', 'blib/' ],
meta_merge => {
Expand Down
7 changes: 7 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Revision history for e

=================
1.17 - 2024-06-10
=================

(Perl 5.40)
Added table command.

=================
1.16 - 2024-06-05
=================
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Watch a reference for changes:
A::f2();
'

Benchmark two snippets of code:

perl -Me -e 'n { slow => sub{ ... }, fast => sub{ ... }}, 10000'

Launch the Runtime::Debugger:

perl -Me -e 'repl'
Expand Down Expand Up @@ -84,6 +88,16 @@ Devel::Peek dump a data structure:

perl -Me -e 'dd { a => [ 1..3] }'

Print data as a table:

perl -Me -e 'table( [qw(key value)], [qw(red 111)], [qw(blue 222)] )'
+------+-------+
| key | value |
+------+-------+
| red | 111 |
| blue | 222 |
+------+-------+

# DESCRIPTION

This module imports many features that make
Expand Down Expand Up @@ -219,6 +233,24 @@ Color a string.

say dye( "HEY", "RED" );

## table

Print data as a table:

perl -Me -e 'table( [qw(key value)], [qw(red 111)], [qw(blue 222)] )'
+------+-------+
| key | value |
+------+-------+
| red | 111 |
| blue | 222 |
+------+-------+

Context sensitive!

- Void - output table.
- List - return individual lines.
- Scalar - return entire table as a string.

## g

Perform a get request.
Expand Down
52 changes: 51 additions & 1 deletion lib/e.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use 5.006;
use strict;
use warnings;

our $VERSION = '1.16';
our $VERSION = '1.17';

=head1 SYNOPSIS
Expand Down Expand Up @@ -66,6 +66,10 @@ Watch a reference for changes:
A::f2();
'
Benchmark two snippets of code:
perl -Me -e 'n { slow => sub{ ... }, fast => sub{ ... }}, 10000'
Launch the Runtime::Debugger:
perl -Me -e 'repl'
Expand Down Expand Up @@ -94,6 +98,16 @@ Devel::Peek dump a data structure:
perl -Me -e 'dd { a => [ 1..3] }'
Print data as a table:
perl -Me -e 'table( [qw(key value)], [qw(red 111)], [qw(blue 222)] )'
+------+-------+
| key | value |
+------+-------+
| red | 111 |
| blue | 222 |
+------+-------+
=cut

=head1 DESCRIPTION
Expand Down Expand Up @@ -233,6 +247,24 @@ Color a string.
say dye( "HEY", "RED" );
=head2 table
Print data as a table:
perl -Me -e 'table( [qw(key value)], [qw(red 111)], [qw(blue 222)] )'
+------+-------+
| key | value |
+------+-------+
| red | 111 |
| blue | 222 |
+------+-------+
Context sensitive!
- Void - output table.
- List - return individual lines.
- Scalar - return entire table as a string.
=head2 g
Perform a get request.
Expand Down Expand Up @@ -453,6 +485,24 @@ sub import {
Term::ANSIColor::colored( @_ );
},

# Table.
table => sub {
if ( !$imported{$caller}{"Term::Table"}++ ) {
require Term::Table;
}

my ($header,@rows) = @_;
my @lines = Term::Table->new(
header => $header,
rows => \@rows,
)->render;

return @lines if wantarray;
return join "\n", @lines if defined wantarray;

print "$_\n" for @lines;
},

######################################
# Web Related
######################################
Expand Down
116 changes: 89 additions & 27 deletions t/01-simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,11 @@ sub run {
$out;
}

is b( "abc" )->size, 3, "b - size";

is_deeply
c( 1, 2, 1, 3 )->uniq->to_array,
[ 1, 2, 3 ],
"c - uniq";

is
f( "/proc/cpuinfo" )->basename,
"cpuinfo",
"f - basename";

is
j( { a => 1 } ),
q({"a":1}),
"j - ref to json string";

is_deeply
j( q({"a":1}) ),
{ a => 1 },
"j - json string to ref";

monkey_patch( "A", func => sub { 111 } );
is
A::func(),
111,
"monkey_patch - simple example";
######################################
# Investigation
######################################

# Benchmark/timing.
like run(
sub {
n sub { }
Expand Down Expand Up @@ -79,11 +56,28 @@ like run(
qr{ ^ [ab] \s+ \d+ .+ \n [ab] \s+ \d+ }xm,
"n - sanity check (multiple,times)";

######################################
# Format Conversions
######################################

# Json.
is
j( { a => 1 } ),
q({"a":1}),
"j - ref to json string";

is_deeply
j( q({"a":1}) ),
{ a => 1 },
"j - json string to ref";

# XML/HTML.
is
x ( "<h1>title</h1>" )->at( "h1" )->text,
"title",
"x - at, text";

# YAML.
is
yml( { a => 1 } ),
"---\na: 1\n",
Expand All @@ -94,4 +88,72 @@ is_deeply
{ a => 1 },
"yml - yml string to ref";

######################################
# Enhanced Types
######################################

# String Object.
is b( "abc" )->size, 3, "b - size";

# Array Object.
is_deeply
c( 1, 2, 1, 3 )->uniq->to_array,
[ 1, 2, 3 ],
"c - uniq";

######################################
# Files Convenience
######################################

# File Object.
is
f( "/proc/cpuinfo" )->basename,
"cpuinfo",
"f - basename";

######################################
# Output
######################################

# Table
{
my @lines = ( [qw(key value)], [qw(red 111)], [qw(blue 222)] );
my @expected = (
"+------+-------+",
"| key | value |",
"+------+-------+",
"| red | 111 |",
"| blue | 222 |",
"+------+-------+",
);

my @list = table( @lines );
is_deeply
\@list,
\@expected,
"table - list context";

my $scalar = table( @lines );
is
$scalar,
join("\n", @expected),
"table - scalar context";

is
run( sub { table( @lines ) }),
join("", map {"$_\n"} @expected),
"table - void context";

}

######################################
# Package Building
######################################

monkey_patch( "A", func => sub { 111 } );
is
A::func(),
111,
"monkey_patch - simple example";

done_testing();

0 comments on commit 2ed9123

Please sign in to comment.