Improve completion for task
authorJoey Schulze <joey@infodrom.org>
Thu, 25 Dec 2014 14:06:27 +0000 (14:06 +0000)
committerJoey Schulze <joey@infodrom.org>
Thu, 25 Dec 2014 14:06:27 +0000 (14:06 +0000)
src/InfoCon/stempel/stempel

index ba0ba62..be9b66c 100755 (executable)
@@ -78,13 +78,32 @@ sub tasklist
     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 $attribs = $term->Attribs;
     $attribs->{completion_entry_function} = $attribs->{list_completion_function};
     $attribs->{completion_word} = $customers;
@@ -100,6 +119,8 @@ sub open_task
 
        $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;