Skip to content

Instantly share code, notes, and snippets.

@bduggan
Last active September 11, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bduggan/7de0d4d8767fd4e067752df1249542f5 to your computer and use it in GitHub Desktop.
Save bduggan/7de0d4d8767fd4e067752df1249542f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use JSON::Fast;
use Terminal::ANSIColor;
my @frames = < / - | - \ - >;
sub spinner {
@frames[$++ % +@frames];
}
sub download-spinners {
my $store = "~/.spinners.json";
unless $store.IO.e {
say "downloading spinners";
my $url='https://raw.githubusercontent.com'
~ '/sindresorhus/cli-spinners'
~ '/HEAD/spinners.json';
shell "curl -s $url > $store";
}
from-json( $store.IO.slurp );
}
multi MAIN(
Bool :$list-spinners!, #= list spinners
) {
say download-spinners.keys.sort.join("\t");
}
multi MAIN(
$expr, #= what to search for
$filename where *.IO.e, #= a filename to grep
Numeric :$wait = 2, #= when to notify
:$spinner is copy, #= which spinner
) {
my $spinners = download-spinners;
$spinner = $spinners.keys.pick unless $spinner;
@frames = $spinners{ $spinner }<frames><>;
exit MAIN(:list-spinners) unless @frames;
shell 'tput civis';
my $proc = Proc::Async.new(<<tail -f "$filename">>);
my $out = $proc.stdout;
my $last-seen = DateTime.now;
start react {
whenever $out.lines.grep( / "$expr" / ) {
.say
}
whenever $out.lines {
print " " ~ spinner() ~ "\r";
$last-seen = DateTime.now;
}
whenever Supply.interval(1) {
if DateTime.now - $last-seen > $wait {
say color('red')
~ "--$wait seconds, no lines--"
~ color('reset');
}
}
whenever signal(SIGINT) {
shell 'tput cnorm';
exit;
}
}
await $proc.start;
}
@b2gills
Copy link

b2gills commented Sep 11, 2018

You can put the $proc.start in a whenever in the react block, so that you don't need start react

react {
    ...
        
    whenever $proc.start {
        done
    }
}

I would just use now instead of DateTime.now, as it is a lighter weight object.

You probably want .IO.f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment