Improve completion for task
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / stempel
index 8754200..be9b66c 100755 (executable)
@@ -1,8 +1,7 @@
 #! /usr/bin/perl
 
 #  Time Tracker
-#
-#  Copyright (c) 2007,8  Martin Schulze <joey@infodrom.org>
+#  Copyright (c) 2007,8,13,14  Martin Schulze <joey@infodrom.org>
 #
 #  This program is free software; you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License as published by
@@ -29,10 +28,7 @@ my $table = "stempel";
 my $engine = "dbi:Pg:dbname=infocon";
 
 my $dbh = DBI->connect($engine);
-if (!$dbh) {
-    print "Access to database denied!\n";
-    return 1;
-}
+die "Access to database denied!\n" unless $dbh;
 $dbh->do("SET DateStyle = 'ISO'");
 
 sub min2hour
@@ -64,32 +60,81 @@ sub customerlist
            push @res, $row->{customer};
        }
     }
-    return @res;
+    return \@res;
+}
+
+sub tasklist
+{
+    my $customer = shift;
+    my @res;
+
+    my $query = q{SELECT DISTINCT task FROM stempel WHERE start > now() - interval '1 month' AND customer = ? ORDER BY task};
+    my $sth = $dbh->prepare($query);
+    if ($sth && $sth->execute($customer) > 0) {
+       while ((my $row = $sth->fetchrow_hashref)) {
+           push @res, $row->{task};
+       }
+    }
+    return \@res;
+}
+
+sub complete_customer
+{
+    my ($customer, $text, $line, $start) = @_;
+
+    # return () unless exists $answers->{category} && length $answers->{category};
+
+    my $sql = sprintf("SELECT DISTINCT task FROM stempel WHERE start > now() - interval '1 month' AND customer = '%s' AND task LIKE '%s%%' ORDER BY task",
+                     $customer,
+                     $line);
+
+    my $sth = $dbh->prepare($sql);
+    $sth->execute;
+    my @complete;
+    while (my $row = $sth->fetchrow_hashref) {
+       $row->{task} = substr $row->{task}, $start if $start;
+       push @complete, $row->{task};
+    }
+
+    return @complete;
 }
 
 sub open_task
 {
     my $term = new Term::ReadLine '';
-    my @customers = customerlist;
-
-    $term->addhistory($_) foreach (@customers);
+    my $customers = customerlist;
 
     my $attribs = $term->Attribs;
     $attribs->{completion_entry_function} = $attribs->{list_completion_function};
-    $attribs->{completion_word} = \@customers;
+    $attribs->{completion_word} = $customers;
 
-    printf "[%s]\n", join (', ', @customers);
+    printf "[%s]\n", join (', ', @$customers);
 
     my $customer = $term->readline ('Kunde: ');
 
     if (defined $customer && length($customer) > 0) {
        $customer =~ s/\s*$//;
-       $attribs->{completion_word} = undef;
-       my $task = $term->readline ('Aufgabe: ');
-       if (length($task) > 0) {
+
+       my $tasks = tasklist $customer;
+
+       $term->addhistory($_) foreach (@$tasks);
+       $attribs->{completion_word} = $tasks;
+       $attribs->{completion_entry_function} = undef;
+       $term->{completion_function} = sub {return complete_customer $customer, @_};
+       while (1) {
+           my $task = $term->readline ('Aufgabe: ');
+           return unless length $task;
+           return if $task eq 'q';
+
+           if ($task eq '?') {
+               printf "  %s\n", $_ foreach (@$tasks);
+               next;
+           }
+
            my $query = q{INSERT INTO stempel (start,customer,task) VALUES('now()',?,?)};
            my $sth = $dbh->prepare($query);
            $sth->execute($customer, $task);
+           last;
        }
     }
 }
@@ -236,6 +281,16 @@ sub late_close_task
     $dbh->do($query);
 }
 
+sub move_task
+{
+    my $minutes = shift;
+
+    return unless is_open;
+
+    my $query = sprintf("UPDATE stempel SET start = start - interval '%d minutes' WHERE stop IS NULL", $minutes);
+    $dbh->do($query);
+}
+
 sub alter_task
 {
     return unless is_open;
@@ -259,6 +314,7 @@ sub help
     print <<"END";
 stempel  Copyright (c) 2007,8  Martin Schulze <joey\@infodrom.org>
     --list [month] list month [default=current|all]
+    --back n       move start of current task back by n minutes
     --open         list open
     --close time   close open task
     --help         this text
@@ -271,7 +327,9 @@ END
 
 my $opt_close = undef;
 my $opt_list = undef;
+my $opt_back = undef;
 my %options = ('list:s' => \$opt_list,
+              'back=i' => \$opt_back,
               'open' => \&list_open,
               'help' => \&help,
               'close=i' => \$opt_close,
@@ -285,6 +343,8 @@ if ($opt_close) {
     late_close_task $opt_close;
 } elsif (defined $opt_list) {
     list $opt_list;
+} elsif (defined $opt_back) {
+    move_task $opt_back;
 } else {
     toggle_task;
 }