Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 142 lines (115 sloc) 3.24 KB
#!/usr/bin/env perl
use 5.14.0;
use warnings;
use DateTime;
use JSON::PP;
package Record {
use Moose;
use Time::Duration::Parse;
has desc => (is => 'ro');
for (qw(moods tags)) {
has $_ => (
isa => 'ArrayRef[Str]',
traits => [ 'Array' ],
handles => { $_ => 'elements' },
);
}
has done_at => (
is => 'ro',
default => sub {
DateTime->now(time_zone => 'America/New_York');
}
);
has week => (
is => 'ro',
lazy => 1,
default => sub {
my ($self) = @_;
my $year = $self->done_at->year;
my $week = int( $self->done_at->day_of_year / 7 );
sprintf("%4d-%02d", $year, $week);
},
);
has took => (
is => 'ro',
isa => 'Int',
predicate => 'has_took',
);
my %moods = (
'+' => 'better', # ++ much-better, +++ much-much-better
'-' => 'worse', # -- and so on
'D' => 'very-happy', # :-D
')' => 'happy', # :-)
'(' => 'unhappy', # :-(
'X' => 'angry', # :-X
'P' => 'silly', # :-P
'/' => 'exasperated', # :-/
'?' => 'confused', # :-?
'S' => 'frustrated', # :-S
"'(" => 'sad', # :'(
"'" => 'sad', # :'(
);
sub _interpret_mood {
my ($self, $moods) = @_;
# XXX: not working, surely something stupid -- rjbs, 2013-11-22
for (@$moods) {
# smileys might have noses; do not confuse with "worse"
$_ = exists $moods{"-$_"} ? $moods{"-$_"}
: exists $moods{ $_ } ? $moods{ $_ }
: $_;
# complain about suspicious stuff we can't recognize?
}
}
sub new_from_argv {
my ($class, @argv) = @_;
my @tags;
my @mood;
my @rest;
my @took;
for (@argv) {
s/^:// and push(@mood, $_), next;
s/^\+// and push(@tags, $_), next;
s/^~// and push(@took, $_), next;
push @rest, $_;
}
$class->_interpret_mood(\@mood);
if (@took > 1) {
warn "ignoring took values after $took[0]\n";
$#took = 1;
}
return $class->new({
desc => "@rest",
moods => \@mood,
tags => [ sort @tags ],
(@took ? (took => parse_duration($took[0])) : ()),
});
}
sub as_text {
my ($self) = @_;
return "Did : " . $self->desc . "\n"
. "At : " . $self->done_at . " (" . $self->week . ")\n"
# "Took: "
. "Tags: " . join(" ", $self->tags) . "\n"
. "Felt: " . join(" ", $self->moods) . "\n";
}
sub as_yaml { # not really, probably
my ($self) = @_;
state $json = JSON::PP->new;
return "- done: " . $self->desc . "\n"
. " when: " . $self->done_at . "\n"
. " time: " . $self->done_at->epoch . "\n"
. ($self->has_took ? " took: " . $self->took . "\n" : q{})
. " mood: " . $json->encode([ $self->moods ]) . "\n"
. " tags: " . $json->encode([ $self->tags ]) . "\n";
}
no Moose;
}
my @argv = @ARGV
? @ARGV
: do { local $|=1; print "did> "; my $x = <STDIN>; chomp $x; split ' ', $x; };
my $record = Record->new_from_argv(@argv);
print $record->as_text;
my $fn = "$ENV{HOME}/log/done/" . $record->week;
open(my $log, '>>', $fn) || die "can't open $fn to append: $!";
print {$log} $record->as_yaml;
close($log) || die "error closing $fn: $!";
Something went wrong with that request. Please try again.