#!/usr/bin/perl
# PODNAME: clipedit
use strict;
use warnings;
use Clipboard;
use File::Temp qw( tempfile );
my $orig = Clipboard->paste;
my ($tmp_fh, $tmpfilename) = tempfile();
print {$tmp_fh} $orig;
close $tmp_fh;
my $ed = $ENV{VISUAL} || $ENV{EDITOR} || 'vim';
system($ed, $tmpfilename);
sub _slurp
{
my $filename = shift;
open my $in, '<', $filename
or die "Cannot open '$filename' for slurping - $!";
local $/;
my $contents = <$in>;
close($in);
return $contents;
}
my $edited = _slurp($tmpfilename);
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;
}
__END__
=pod
=encoding UTF-8
=head1 NAME
clipedit - Edit clipboard contents in one swoop.
=head1 VERSION
version 0.29
=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>
=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Websites
The following websites have more information about this module, and may be of help to you. As always,
in addition to those websites please use your favorite search engine to discover more resources.
=over 4
=item *
MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
L<https://metacpan.org/release/Clipboard>
=item *
RT: CPAN's Bug Tracker
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
L<https://rt.cpan.org/Public/Dist/Display.html?Name=Clipboard>
=item *
CPANTS
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
L<http://cpants.cpanauthors.org/dist/Clipboard>
=item *
CPAN Testers
The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
L<http://www.cpantesters.org/distro/C/Clipboard>
=item *
CPAN Testers Matrix
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
L<http://matrix.cpantesters.org/?dist=Clipboard>
=item *
CPAN Testers Dependencies
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
L<http://deps.cpantesters.org/?module=Clipboard>
=back
=head2 Bugs / Feature Requests
Please report any bugs or feature requests by email to C<bug-clipboard at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Clipboard>. You will be automatically notified of any
progress on the request by the system.
=head2 Source Code
The code is open to the world, and available for you to hack on. Please feel free to browse it and play
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
from your repository :)
L<https://github.com/shlomif/Clipboard>
git clone git://github.com/shlomif/Clipboard.git
=head1 AUTHOR
Shlomi Fish <shlomif@cpan.org>
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
L<https://github.com/shlomif/Clipboard/issues>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2024 by Ryan King <rking@panoptic.com>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut