Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use v6;
## agregation of STDERR
## filtering of STDERR for colour coding and ENV vars
## exception handling with STDERR stashing
## https://colabti.org/irclogger/irclogger_log/raku?date=2020-07-04#l268
subset Arrayish of Any where { .^can(push) && .^can(list) }
class Shell::Pipe {
has @.pipees;
has @.starters;
has $.name is rw = "Shell::Pipe <anon>";
method start {
do for @.starters.reverse -> &c { |c }
}
method sink { note "sinking {self.gist}"; await self.start }
method gist {
@.pipees.map(*.&gist-of-pipee).join('')
}
sub gist-of-pipee($e) {
given $e {
when Proc::Async { .path.IO.basename }
when Routine { .name }
when Block { Block({.file.IO.basename}:{.line}) }
when Arrayish { .?name // .WHAT.gist }
}
}
}
my multi infix:«|>»(Proc::Async:D $out, &c) {
my $pipe = Shell::Pipe.new;
$pipe.pipees.append: $out, &c;
$pipe
}
my multi infix:«|>»(Shell::Pipe:D $pipe, &c) {
$pipe.pipees.append: &c;
$pipe
}
my multi infix:«|>»(Proc::Async:D $out, Proc::Async:D $in) {
my $pipe = Shell::Pipe.new;
$pipe.pipees.append: $out, $in;
$pipe.starters.append: -> {
$in.start, $out.start
}
$in.bind-stdin: $out.stdout;
$pipe
}
my multi infix:«|>»(Shell::Pipe:D $pipe, Proc::Async:D $in) {
my $out = $pipe.pipees.tail;
$pipe.pipees.push: $in;
given $out {
when Proc::Async {
$in.bind-stdin: .stdout;
$pipe.starters.push: -> { $in.start };
}
when Arrayish {
fail "Arrayish not at the tail or head of a pipe.";
}
}
$pipe
}
my multi infix:«|>»(Proc::Async:D $out, Arrayish:D \a) {
my $pipe = Shell::Pipe.new;
$pipe.pipees.push: $out;
$pipe.pipees.push: a;
$out.stdout.lines.tap(-> \e { a.push: e });
$pipe.starters.push(-> { $out.start });
$pipe
}
my multi infix:«|>»(Shell::Pipe:D $pipe, Arrayish:D \a) {
my $out = $pipe.pipees.tail;
$pipe.pipees.push: a;
$out.stdout.lines.tap(-> \e { a.push: e });
$pipe
}
my multi infix:«|>»(Arrayish:D \a, Proc::Async:D $in) {
my $pipe = Shell::Pipe.new;
$pipe.pipees.push: a;
$pipe.pipees.push: $in;
# FIXME workaround R#3778
$in.^attributes.grep(*.name eq '$!w')[0].set_value($in, True);
$pipe.starters.push: -> {
| $in.start, start {
LEAVE try $in.close-stdin;
$in.write: $_\n.encode for a.list;
}
}
$pipe
}
my multi infix:«|>»(Supply:D \s, Proc::Async:D $in) {
# TODO
}
my multi infix:«|>»(Supply:D \s, Shell::Pipe:D $in) {
# TODO
}
my $find = Proc::Async.new('/usr/bin/find', '/tmp');
my $grep = Proc::Async.new('/bin/grep', 'a');
my $sort = Proc::Async.new('/usr/bin/sort');
my @a;
my @spy;
#
# sub my-little-filter { $^a ~~ s/a/b/ }
#
# my $pipe = $find \
# |> @spy \
# |> $grep \
# |> &my-little-filter \
# |> $sort \
# |> { .uc } \
# |> @a;
#
# say $pipe;
@a = <3 b a 5 c>;
my $p = $find |> $sort |> @a;
say $p;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment