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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines