File: clipedit

package info (click to toggle)
libclipboard-perl 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 264 kB
  • ctags: 130
  • sloc: perl: 1,260; sh: 43; makefile: 10
file content (90 lines) | stat: -rwxr-xr-x 2,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/perl
use strict;
use Clipboard;
use File::Temp qw( tempfile );

my $orig = Clipboard->paste;

my ($tmpfile, $tmpfilename) = tempfile();
open my $tmpfile, ">$tmpfilename" or die "Failure to open $tmpfilename: $!";
print $tmpfile $orig;
close $tmpfile;

my $ed = $ENV{VISUAL} || $ENV{EDITOR} || 'sensible-editor';
system($ed, $tmpfilename);

open $tmpfile, $tmpfilename or die "Failure to open $tmpfilename: $!";
my $edited = join '', <$tmpfile>;

my $current = Clipboard->paste;

if ($current ne $orig) {
    local $| = 1;
    boldprint("1) When you started, the Clipboard contained:\n");
    print $orig;
    boldprint("\n2) ...but now the Clipboard contains:\n");
    print $current;
    boldprint("\n3) and you edited to this:\n");
    print $edited;
    boldprint("\nWhich would you like to use (1, 2, or the default, 3)? ");
    my %actions = (
        1 => $orig,
        2 => $current,
        3 => $edited,
    );
    my $answer;
    while (1) {
        $answer = <STDIN>;
        chomp $answer;
        $answer = 3 if $answer eq '';
        last if exists $actions{$answer};
        my @puzzles = qw(hrm what huh uhh who because sneevle);
        boldprint(ucfirst($puzzles[int rand $#puzzles]) . "? ");
    }
    $edited = $actions{$answer};
}
Clipboard->copy($edited);
print Clipboard->paste;
boldprint("\n...is now in the Clipboard\n");

unlink($tmpfilename) or die "Couldn't remove $tmpfilename: $!";

sub boldprint {
    # If you are in a situation where this output is annoying, such as in a
    # DOS console without ANSI parsing, please send a patch.  For now, I'll
    # just do the simplest thing and print it every time:
    printf "\e[033m%s\e[0m", shift;
}

=head1 NAME

clipedit - Edit clipboard contents in one swoop.

=head1 MOTIVATION

Eliminating the "Open editor, edit stuff, copy back to the clipboard" shuffle.

=head1 NOTE

If for some reason the clipboard contents changes during the edit session, you
will be prompted to choose between 1) the original Clipboard contents, 2) the
new Clipboard contents, and 3) the result of your edits (which is the default
if you just hit "Enter").

=head1 CONFIGURATION

If you don't want the script to use C<vim> to edit, set either the
environment variable C<$VISUAL> or C<$EDITOR>.

=head1 AUTHOR

Ryan King <rking@panoptic.com>

=head1 COPYRIGHT

Copyright (c) 2010.  Ryan King.  All rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>