Skip to content

Commit

Permalink
Version 1.26
Browse files Browse the repository at this point in the history
Internal cleanup.
Added color when STDOUT is closed.
Added set command (Set::Scalar).
  • Loading branch information
Timofey Potapov committed Jul 27, 2024
1 parent d9d372c commit 2ab2302
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 31 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ my $builder = $class->new(
'Data::Trace' => '1.05',
'Mojolicious' => '0',
'Runtime::Debugger' => '1.03',
'Set::Scalar' => '0',
'Sub::Util' => '0',
'Term::Table' => '0',
'Tiny::Prof' => '0.03',
Expand Down
8 changes: 8 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Revision history for e

=================
1.26 - 2024-07-27
=================

Internal cleanup.
Added color when STDOUT is closed.
Added set command (Set::Scalar).

=================
1.25 - 2024-07-04
=================
Expand Down
97 changes: 88 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
⠹⡽⣾⣿⠹⣿⣆⣾⢯⣿⣿ ⡞ ⠻⣿⣿⣿⠁ ⢠⣿⢏ ⡀ ⡟ ⢀⣴⣿⠃⢁⡼⠁ ⠈
⠈⠛ ⢻⣿⣧⢸⢟⠶⢾⡇ ⣸⡿⠁ ⢠⣾⡟⢼ ⣷ ⡇ ⣰⠋⠙⠁
⠈⣿⣻⣾⣦⣇⢸⣇⣀⣶⡿⠁⣀⣀⣾⢿⡇⢸ ⣟⡦⣧⣶⠏ unleashed
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.25
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.26
⢻⡄ ⠐⠚⠋⣠⡾⣧⣿⠁⠙⢳⣽⡟
⠈⠳⢦⣤⣤⣀⣤⡶⠛ ⠈⢿⡆ ⢿⡇
⠈ ⠈⠓ ⠈
Expand Down Expand Up @@ -264,6 +264,11 @@ Decode a byte steam to UTF-8 code point:

Set STDOUT and STDERR as UTF-8 encoded.

If given a filehandle, will set the encoding
for it to UTF-8.

utf8($fh);

## Enhanced Types

### b
Expand All @@ -284,6 +289,57 @@ Work with arrays.

Turn list into a [Mojo::Collection](https://metacpan.org/pod/Mojo%3A%3ACollection) object.

### set

Work with sets.

my $set = set(2,4,6,4);

Turn list into a [Set::Scalar](https://metacpan.org/pod/Set%3A%3AScalar) object.

$ perl -Me -e 'say set(2,4,6,2)'
(2 4 6)


Get elements:

$ perl -Me -e 'say for sort(set(2,4,6,2)->elements)'
$ perl -Me -e 'say for sort(set(2,4,6,2)->@*)'
2
4
6

Intersection:

$ perl -Ilib/ -Me -e 'say set(2,4,6,2) * set(3,4,5,6)'
(4 6)

Create a new universe:

# Universe 1:
# ...
Set::Scalar::Universe->new->enter;
# Universe 2:
# ...

Operations:

set value

$a (a b c d e _ _ _ _)
$b (_ _ c d e f g _ _)
$c (_ _ _ _ e f g h i)

union: $a + $b (a b c d e f g _ _)
union: $a + $b + $c (a b c d e f g h i)
intersection: $a * $b (_ _ c d e _ _ _ _)
intersection: $a * $b * $c (_ _ _ _ e _ _ _ _)
difference: $a - $b (a b _ _ _ _ _ _ _)
difference: $a - $b - $c (a b _ _ _ _ _ _ _)
unique: $a % $b (a b _ _ _ f g _ _)
symm_diff: $a / $b (a b _ _ _ f g _ _)
complement: -$a (_ _ c d e f g h i)

## Files Convenience

### f
Expand All @@ -309,11 +365,11 @@ Always sends output to the terminal even
when STDOUT and/or STDERR are redirected:

$ perl -Me -e '
say "Shown before";
close *STDOUT;
close *STDERR;
say 111;
print "999\n";
say 222;
say "Shown with no stdout/err";
print "Print not seen\n";
'
111
222
Expand Down Expand Up @@ -406,16 +462,39 @@ Insert subroutines into the symbol table.

Extracted from Mojo::Util for performance.

Import methods into another function
(as done this module):

$ perl -e 'package A; use e; sub import { my $c = caller(); monkey_patch $c, new => sub { say "Im new" } } package main; A->import; new()'
Import methods into another package
(as done in this module):

$ perl -e '
package A;
use e;
sub import {
my $c = caller();
monkey_patch
$c,
new => sub { say "Im new" };
}
package main;
A->import;
new();
'
Im new

Import methods into the same package
(probably not so useful):

$ perl -e 'package A; use e; sub import { my $c = caller(); monkey_patch $c, new => sub { say "Im new" } } A->import; A->new()'
$ perl -e '
package A;
use e;
sub import {
my $c = caller();
monkey_patch
$c,
new => sub { say "Im new" };
}
A->import;
A->new();
'
Im new

Perhaps can be updated based on the outcome
Expand Down
136 changes: 114 additions & 22 deletions lib/e.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ package e;
⠹⡽⣾⣿⠹⣿⣆⣾⢯⣿⣿ ⡞ ⠻⣿⣿⣿⠁ ⢠⣿⢏ ⡀ ⡟ ⢀⣴⣿⠃⢁⡼⠁ ⠈
⠈⠛ ⢻⣿⣧⢸⢟⠶⢾⡇ ⣸⡿⠁ ⢠⣾⡟⢼ ⣷ ⡇ ⣰⠋⠙⠁
⠈⣿⣻⣾⣦⣇⢸⣇⣀⣶⡿⠁⣀⣀⣾⢿⡇⢸ ⣟⡦⣧⣶⠏ unleashed
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.25
⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.26
⢻⡄ ⠐⠚⠋⣠⡾⣧⣿⠁⠙⢳⣽⡟
⠈⠳⢦⣤⣤⣀⣤⡶⠛ ⠈⢿⡆ ⢿⡇
⠈ ⠈⠓ ⠈
Expand All @@ -45,7 +45,7 @@ use 5.006;
use strict;
use warnings;

our $VERSION = '1.25';
our $VERSION = '1.26';

=head1 SYNOPSIS
Expand Down Expand Up @@ -284,6 +284,11 @@ Decode a byte steam to UTF-8 code point:
Set STDOUT and STDERR as UTF-8 encoded.
If given a filehandle, will set the encoding
for it to UTF-8.
utf8($fh);
=cut

=head2 Enhanced Types
Expand All @@ -308,6 +313,58 @@ Turn list into a L<Mojo::Collection> object.
=cut

=head3 set
Work with sets.
my $set = set(2,4,6,4);
Turn list into a L<Set::Scalar> object.
$ perl -Me -e 'say set(2,4,6,2)'
(2 4 6)
Get elements:
$ perl -Me -e 'say for sort(set(2,4,6,2)->elements)'
$ perl -Me -e 'say for sort(set(2,4,6,2)->@*)'
2
4
6
Intersection:
$ perl -Ilib/ -Me -e 'say set(2,4,6,2) * set(3,4,5,6)'
(4 6)
Create a new universe:
# Universe 1:
# ...
Set::Scalar::Universe->new->enter;
# Universe 2:
# ...
Operations:
set value
$a (a b c d e _ _ _ _)
$b (_ _ c d e f g _ _)
$c (_ _ _ _ e f g h i)
union: $a + $b (a b c d e f g _ _)
union: $a + $b + $c (a b c d e f g h i)
intersection: $a * $b (_ _ c d e _ _ _ _)
intersection: $a * $b * $c (_ _ _ _ e _ _ _ _)
difference: $a - $b (a b _ _ _ _ _ _ _)
difference: $a - $b - $c (a b _ _ _ _ _ _ _)
unique: $a % $b (a b _ _ _ f g _ _)
symm_diff: $a / $b (a b _ _ _ f g _ _)
complement: -$a (_ _ c d e f g h i)
=cut

=head2 Files Convenience
=head3 f
Expand Down Expand Up @@ -335,11 +392,11 @@ Always sends output to the terminal even
when STDOUT and/or STDERR are redirected:
$ perl -Me -e '
say "Shown before";
close *STDOUT;
close *STDERR;
say 111;
print "999\n";
say 222;
say "Shown with no stdout/err";
print "Print not seen\n";
'
111
222
Expand Down Expand Up @@ -436,16 +493,39 @@ Insert subroutines into the symbol table.
Extracted from Mojo::Util for performance.
Import methods into another function
(as done this module):
$ perl -e 'package A; use e; sub import { my $c = caller(); monkey_patch $c, new => sub { say "Im new" } } package main; A->import; new()'
Import methods into another package
(as done in this module):
$ perl -e '
package A;
use e;
sub import {
my $c = caller();
monkey_patch
$c,
new => sub { say "Im new" };
}
package main;
A->import;
new();
'
Im new
Import methods into the same package
(probably not so useful):
$ perl -e 'package A; use e; sub import { my $c = caller(); monkey_patch $c, new => sub { say "Im new" } } A->import; A->new()'
$ perl -e '
package A;
use e;
sub import {
my $c = caller();
monkey_patch
$c,
new => sub { say "Im new" };
}
A->import;
A->new();
'
Im new
Perhaps can be updated based on the outcome
Expand Down Expand Up @@ -497,7 +577,7 @@ sub monkey_patch {

sub import {
my ( $class, $caller ) = @_;
my %imported;
my %imported; # Require only once a package.
$caller //= caller;

monkey_patch(
Expand Down Expand Up @@ -637,6 +717,14 @@ sub import {
Mojo::Collection::c( @_ );
},

# Array Object.
set => sub {
if ( !$imported{$caller}{"Set::Scalar"}++ ) {
require Set::Scalar;
}
Set::Scalar->new( @_ );
},

######################################
# Files Convenience
######################################
Expand All @@ -662,15 +750,19 @@ sub import {
# issues with next say() if still closed:
# "say() on closed filehandle STDOUT"
if ( !-t STDOUT ) {
open my $tty, ">", "/dev/tty" or die $!;
caller->can( "utf8" )->( $tty ); # Method now in caller.
CORE::say $tty @args;
close $tty;
if ( open my $tty, ">", "/dev/tty" ) {
caller->can( "utf8" )->( $tty ); # Method now in caller.
my $prefix =
caller->can( "dye" )->( "no-stdout: ", "CYAN" );
CORE::say( $tty $prefix, @args );
close $tty;
}
}

# Send to output incase something expects it there.
caller->can( "utf8" );
CORE::say @args;
caller->can( "utf8" )->();
CORE::say( @args );

},

# Pretty Print.
Expand Down Expand Up @@ -735,7 +827,7 @@ sub import {
my @lines = Term::Table->new(
header => $header,
rows => \@rows,
sanitize => 0, # To not show \n
sanitize => 0, # To not show \n
)->render;

return @lines if wantarray;
Expand All @@ -753,10 +845,10 @@ sub import {
if ( !$imported{$caller}{"Mojo::UserAgent"}++ ) {
require Mojo::UserAgent;
}
my $UA = Mojo::UserAgent->new;
$UA->max_redirects( 10 ) unless defined $ENV{MOJO_MAX_REDIRECTS};
$UA->proxy->detect unless defined $ENV{MOJO_PROXY};
$UA->get( @_ )->result;
my $ua = Mojo::UserAgent->new;
$ua->max_redirects( 10 ) unless defined $ENV{MOJO_MAX_REDIRECTS};
$ua->proxy->detect unless defined $ENV{MOJO_PROXY};
$ua->get( @_ )->result;
},

# URL.
Expand Down
Loading

0 comments on commit 2ab2302

Please sign in to comment.