ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
(Generate patch)

Comparing rxvt-unicode/src/perl/selection-pastebin (file contents):
Revision 1.6 by root, Wed Jan 18 10:31:37 2006 UTC vs.
Revision 1.14 by root, Wed Jan 25 14:53:14 2006 UTC

1#! perl 1#! perl
2
3my $timers = {};
4my $pastebin_cmd;
5my $pastebin_url;
6 2
7sub upload_paste { 3sub upload_paste {
8 my ($self) = @_; 4 my ($self) = @_;
9 5
10 require Digest::MD5; 6 require Digest::MD5;
12 my $txt = $self->selection; 8 my $txt = $self->selection;
13 my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; 9 my $filename = Digest::MD5::md5_hex ($txt) . ".txt";
14 10
15 my $tmpfile = "/tmp/$filename"; 11 my $tmpfile = "/tmp/$filename";
16 12
17 my $msg = "uploaded $filename"; 13 my $msg = "uploaded as $filename";
18 14
19 if (open my $o, ">$tmpfile") { 15 if (open my $o, ">:utf8", $tmpfile) {
20 chmod 0644, $tmpfile; 16 chmod 0644, $tmpfile;
21 print $o $txt; 17 print $o $txt;
22 close $o; 18 close $o;
23 } else { 19 } else {
24 $msg = "couldn't write $tmpfile: $!"; 20 $msg = "couldn't write $tmpfile: $!";
25 } 21 }
26 22
27 my $cmd = $pastebin_cmd; 23 my $cmd = $self->{pastebin_cmd};
28 $cmd =~ s/%/$tmpfile/; 24 $cmd =~ s/%/$tmpfile/;
29 25
30 if (system ($cmd) == 0) { 26 my $pid = $self->exec_async ("/bin/sh", "-c", $cmd);
31 my $url = $pastebin_url;
32 $url =~ s/%/$filename/;
33 27
34 $self->selection ($url); 28 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
35 } else { 29 my (undef, $status) = @_;
36 $msg = "couldn't upload, '$cmd' failed";
37 }
38 30
39 unlink $tmpfile; 31 delete $self->{pw};
40 32
41 my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); 33 if ($status) {
42 $ov->set (0, 0, $msg); 34 $status >>= 8;
35 $msg = "ERROR: command returned status $status";
36 } else {
37 my $url = $self->{pastebin_url};
38 $url =~ s/%/$filename/;
43 39
44 $timers->{t1} = 40 $self->selection ($url);
41 }
42
43 unlink $tmpfile;
44
45 my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0);
46 $ov->set (0, 0, $msg);
47
48 $self->{timer} =
45 urxvt::timer 49 urxvt::timer
46 ->new 50 ->new
47 ->start ((int urxvt::NOW) + 5) # make sure we update "on" the second
48 ->interval (1) 51 ->after (5)
49 ->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); 52 ->cb (sub { delete $self->{timer}; undef $ov; });
53 });
50} 54}
51 55
52sub on_start { 56sub on_start {
53 my ($self) = @_; 57 my ($self) = @_;
54 $pastebin_cmd = $self->x_resource ("selection-pastebin.cmd")
55 || "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
56 58
57 $pastebin_url = $self->x_resource ("selection-pastebin.url") 59 $self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd")
58 || "http://www.ta-sa.org/files/txt/%"; 60 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
59 61
60 push @urxvt::ext::selection_popup::hook, sub { 62 $self->{pastebin_url} = $self->x_resource ("selection-pastebin.url")
63 || "http://www.ta-sa.org/files/txt/%";
64
65 push @{ $self->{term}{selection_popup_hook} }, sub {
61 ("pastebin upload" => sub { $self->upload_paste }) 66 ("pastebin upload" => sub { $self->upload_paste })
62 }; 67 };
63 68
64 () 69 ()
65} 70}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines