]> snippets.scripts.mit.edu Git - Scripts/git/.git/blobdiff - rt/BarnOwl/lib/BarnOwl/Module/RT.pm
BarnOwl::Module::RT: Launch rt asynchronously, so we don’t block the UI
[Scripts/git/.git] / rt / BarnOwl / lib / BarnOwl / Module / RT.pm
index b4ee67ff0fcddcdec87c10e8a8b848f3f98fc413..609200fbe359ee2f912314f067d404bff4187eec 100644 (file)
@@ -12,7 +12,7 @@ Foo
 =cut
 
 package BarnOwl::Module::RT;
-use IPC::Open3;
+use AnyEvent::Util qw(run_cmd);
 use Text::ParseWords;
 
 our $VERSION = '1.0.1';
@@ -48,7 +48,7 @@ if(-r "$file_path") {
     close($fh);
 }
 
-my $file_path = "$cfg/rtcommands";
+$file_path = "$cfg/rtcommands";
 if(-r "$file_path") {
     open(my $fh, "<:encoding(UTF-8)", "$file_path") or die("Unable to read $file_path:$!\n");
     while(defined(my $line = <$fh>)) {
@@ -105,23 +105,21 @@ sub cmd_rt{
 }
 
 sub run_rt_command{
-    my @args = ("athrun","tooltime","rt");
-    push (@args, @_);
-    local(*IN, *OUT, *ERR);
-    my $pid = open3(*IN, *OUT, *ERR, @args) || die("RT threw $!");
-    close(*IN); 
-    my $out = do { local $/; <OUT> };
-    close(*OUT);
-    $out .= do { local $/; <ERR> };
-    close(*ERR);
-
-    waitpid( $pid, 0 );
-
-    if (($out =~ tr/\n//) eq 1){
-       return $out;
-    }
-    BarnOwl::popless_text($out);
-    return;
+    my $out;
+    run_cmd(
+        ["athrun", "tooltime", "rt", @_],
+        "<", "/dev/null",
+        ">", \$out,
+        "2>", \$out,
+    )->cb(sub {
+        my $ret = shift->recv;
+        $ret == 0 or die("RT returned $ret");
+        if (($out =~ tr/\n//) == 1) {
+            BarnOwl::message($out);
+        } else {
+            BarnOwl::popless_text($out);
+        }
+    });
 }
 
 BarnOwl::new_command("rt",
@@ -165,7 +163,7 @@ rtcommands:
 
            It is a good place to put custom queries which will be used frequently.
          Examples:
-           "list-owner (\w+)" "rt list -o +Created \"((Status=new or Status=stalled or Status=open) and (Queue='\$q') and 'Owner='\$1')\""
+           "list-owner (\\w+)" "rt list -o +Created \"((Status=new or Status=stalled or Status=open) and (Queue='\$q') and 'Owner='\$1')\""
        
            \$t is the current ticket
            \$q is the current queue