1#!/usr/bin/perl
2
3# PODNAME: clipedit
4use strict;
5use warnings;
6
7use Clipboard;
8use File::Temp qw( tempfile );
9
10my $orig = Clipboard->paste;
11
12my ($tmp_fh, $tmpfilename) = tempfile();
13print {$tmp_fh} $orig;
14close $tmp_fh;
15
16my $ed = $ENV{VISUAL} || $ENV{EDITOR} || 'vim';
17system($ed, $tmpfilename);
18
19sub _slurp
20{
21    my $filename = shift;
22
23    open my $in, '<', $filename
24        or die "Cannot open '$filename' for slurping - $!";
25
26    local $/;
27    my $contents = <$in>;
28
29    close($in);
30
31    return $contents;
32}
33
34my $edited = _slurp($tmpfilename);
35
36my $current = Clipboard->paste;
37
38if ($current ne $orig) {
39    local $| = 1;
40    boldprint("1) When you started, the Clipboard contained:\n");
41    print $orig;
42    boldprint("\n2) ...but now the Clipboard contains:\n");
43    print $current;
44    boldprint("\n3) and you edited to this:\n");
45    print $edited;
46    boldprint("\nWhich would you like to use (1, 2, or the default, 3)? ");
47    my %actions = (
48        1 => $orig,
49        2 => $current,
50        3 => $edited,
51    );
52    my $answer;
53    while (1) {
54        $answer = <STDIN>;
55        chomp $answer;
56        $answer = 3 if $answer eq '';
57        last if exists $actions{$answer};
58        my @puzzles = qw(hrm what huh uhh who because sneevle);
59        boldprint(ucfirst($puzzles[int rand $#puzzles]) . "? ");
60    }
61    $edited = $actions{$answer};
62}
63Clipboard->copy($edited);
64print Clipboard->paste;
65boldprint("\n...is now in the Clipboard\n");
66
67unlink($tmpfilename) or die "Couldn't remove $tmpfilename: $!";
68
69sub boldprint {
70    # If you are in a situation where this output is annoying, such as in a
71    # DOS console without ANSI parsing, please send a patch.  For now, I'll
72    # just do the simplest thing and print it every time:
73    printf "\e[033m%s\e[0m", shift;
74}
75
76__END__
77
78=pod
79
80=encoding UTF-8
81
82=head1 NAME
83
84clipedit - Edit clipboard contents in one swoop.
85
86=head1 VERSION
87
88version 0.28
89
90=head1 MOTIVATION
91
92Eliminating the "Open editor, edit stuff, copy back to the clipboard" shuffle.
93
94=head1 NOTE
95
96If for some reason the clipboard contents changes during the edit session, you
97will be prompted to choose between 1) the original Clipboard contents, 2) the
98new Clipboard contents, and 3) the result of your edits (which is the default
99if you just hit "Enter").
100
101=head1 CONFIGURATION
102
103If you don't want the script to use C<vim> to edit, set either the
104environment variable C<$VISUAL> or C<$EDITOR>.
105
106=head1 AUTHOR
107
108Ryan King <rking@panoptic.com>
109
110=head1 COPYRIGHT
111
112Copyright (c) 2010.  Ryan King.  All rights reserved.
113
114This program is free software; you can redistribute it and/or modify it
115under the same terms as Perl itself.
116
117See L<http://www.perl.com/perl/misc/Artistic.html>
118
119=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
120
121=head1 SUPPORT
122
123=head2 Websites
124
125The following websites have more information about this module, and may be of help to you. As always,
126in addition to those websites please use your favorite search engine to discover more resources.
127
128=over 4
129
130=item *
131
132MetaCPAN
133
134A modern, open-source CPAN search engine, useful to view POD in HTML format.
135
136L<https://metacpan.org/release/Clipboard>
137
138=item *
139
140RT: CPAN's Bug Tracker
141
142The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
143
144L<https://rt.cpan.org/Public/Dist/Display.html?Name=Clipboard>
145
146=item *
147
148CPANTS
149
150The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
151
152L<http://cpants.cpanauthors.org/dist/Clipboard>
153
154=item *
155
156CPAN Testers
157
158The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
159
160L<http://www.cpantesters.org/distro/C/Clipboard>
161
162=item *
163
164CPAN Testers Matrix
165
166The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
167
168L<http://matrix.cpantesters.org/?dist=Clipboard>
169
170=item *
171
172CPAN Testers Dependencies
173
174The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
175
176L<http://deps.cpantesters.org/?module=Clipboard>
177
178=back
179
180=head2 Bugs / Feature Requests
181
182Please report any bugs or feature requests by email to C<bug-clipboard at rt.cpan.org>, or through
183the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Clipboard>. You will be automatically notified of any
184progress on the request by the system.
185
186=head2 Source Code
187
188The code is open to the world, and available for you to hack on. Please feel free to browse it and play
189with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
190from your repository :)
191
192L<https://github.com/shlomif/Clipboard>
193
194  git clone git://github.com/shlomif/Clipboard.git
195
196=head1 AUTHOR
197
198Shlomi Fish <shlomif@cpan.org>
199
200=head1 BUGS
201
202Please report any bugs or feature requests on the bugtracker website
203L<https://github.com/shlomif/Clipboard/issues>
204
205When submitting a bug or request, please include a test-file or a
206patch to an existing test-file that illustrates the bug or desired
207feature.
208
209=head1 COPYRIGHT AND LICENSE
210
211This software is copyright (c) 2021 by Ryan King <rking@panoptic.com>.
212
213This is free software; you can redistribute it and/or modify it under
214the same terms as the Perl 5 programming language system itself.
215
216=cut
217