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.5 by root, Wed Jan 18 10:11:03 2006 UTC vs.
Revision 1.19 by root, Sun Jun 10 13:58:06 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines