Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 134 lines (105 sloc) 2.96 KB
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use Encode qw(encode decode);
use File::Dropbox;
use IO::All;
use Tie::IxHash;
use List::Util 'shuffle';
open my $raw_stdout, '>&', \*STDOUT;
binmode STDIN, 'encoding(UTF-8)';
binmode STDOUT, 'encoding(UTF-8)';
tie my %tree, 'Tie::IxHash';
my $tree = \%tree;
my @stack = ($tree);
my $dropbox = File::Dropbox->new(
access_token => $ENV{DROPBOX_ACCESS_TOKEN},
root => 'dropbox',
oauth2 => 1,
);
open $dropbox, '<', $ENV{LB_NOTES};
while (<$dropbox>) {
$_ = decode('UTF-8', $_);
my ($depth, $msg) = m/^(\t*)(.+)$/;
next unless $msg;
pop @stack while $#stack > length $depth;
tie my %itree, 'Tie::IxHash';
push @stack, ( $stack[-1]{$msg} = \%itree );
}
sub show {
my ($hash, $simplify, $fh, $offset) = @_;
$fh ||= \*STDOUT;
$offset ||= 0;
my $i = 0;
for my $key (keys %$hash) {
my $out = "$key\n";
$out =~ s/\[_\]\s*// if $simplify;
my $str = ("\t" x $offset) . $out;
$str = encode('UTF-8', $str);
print $fh "\n" if !$offset && $i;
print $fh $str;
show($hash->{$key}, $simplify, $fh, $offset + 1) if $hash->{$key} && %{ $hash->{$key} };
$i++
}
}
sub update {
open $dropbox, '>', "$ENV{LB_NOTES}";
show(shift, 0, $dropbox);
close $dropbox;
}
sub dig_random {
my @path = @_;
my $inner_tree = $tree;
$inner_tree = $inner_tree->{$_} for @path;
my ($choice) = shuffle(grep !m/\[X\]/, keys %$inner_tree);
$inner_tree->{$choice =~ s/\[_\]/[X]/r } =
delete $inner_tree->{$choice};
update($tree);
show({ $choice => $inner_tree->{$choice} }, 1);
}
chomp(my $in = do { local $/; <STDIN> });
$in =~ s/^\s*//;
$in =~ s/\s*$//;
if ($in =~ m/^inspire\s+me$/i) {
warn sprintf "[%d] note: inspiring\n", getppid;
dig_random('Incubation', 'Inspiration')
} elsif ($in =~ m/^beer\s+me\s+a\s+video$/i) {
warn sprintf "[%d] note: videoing\n", getppid;
dig_random('Incubation', 'Videos')
} elsif ($in =~ m/^beer\s+me\s+a\s+song$/i) {
warn sprintf "[%d] note: musicing\n", getppid;
dig_random('Incubation', 'Music')
} elsif ($in =~ m/^(?:q|enqueue|queue)\s+(video|song|music|inspiration|idea|drama|comedy|restaurant)\s+(.*)$/i) {
my $key = lc $1;
my $item = $2;
warn sprintf "[%d] note: q'ing $item\n", getppid;
if ($key eq 'video') {
$key = 'Videos'
} elsif ($key =~ m/^(?:song|music)$/) {
$key = 'Music'
} elsif ($key eq 'inspiration') {
$key = 'Inspiration'
} elsif ($key eq 'idea') {
$key = 'Ideas'
} elsif ($key eq 'drama') {
$key = 'Dramas'
} elsif ($key eq 'comedy') {
$key = 'Comedies'
} elsif ($key eq 'restaurant') {
$key = 'Restaurants'
}
$tree->{Incubation}{$key}{"$item"} = {};
update($tree);
print 'Enqueued item!';
} elsif ($in =~ m/^(?:q|enqueue|queue)\s+in\s+(.*)$/i) {
my $item = $1;
warn sprintf "[%d] note: q in $item\n", getppid;
$tree->{IN}{"$item"} = {};
update($tree);
print 'Enqueued item!';
} elsif ($in =~ m/^echo notes$/i) {
show($tree, 0, $raw_stdout)
} else {
exit 1
}
You can’t perform that action at this time.