tangetools/clipboard/clipboard
2024-08-06 01:11:05 +02:00

138 lines
2.7 KiB
Bash
Executable file

#!/bin/bash
: <<=cut
=encoding utf8
=head1 NAME
clipboard - copy to or from X clipboard
=head1 SYNOPSIS
B<clipboard>
=head1 DESCRIPTION
B<clipboard> can be used in 3 ways.
=head2 End of a pipe - save to clipboard
If placed last in a pipe B<clipboard> will take standard output from a
command and save it to the clipboard.
Equivalent to: B<cat | tee >>B<(xsel -i -b) | xclip -i >>B</dev/null>
=head3 Example
cat file | clipboard
=head2 Start of a pipe - paste the clipboard
If placed first in a pipe B<clipboard> will paste content of the
clipboard to standard output.
Equivalent to: B<xsel -o -b>
=head3 Example
clipboard | cat
=head2 In the middle of a pipe - save and paste
If placed in the middle of a pipe B<clipboard> will copy standard
output from a command and copy it to the clipboard and pass on the
standard output.
Equivalent to: B<tee >>B<(xsel -i -b) | xclip -i >>B</dev/null; xsel -o -b>
=head3 Example
cat | clipboard | cat
=head1 OPTIONS
=over 4
=item B<-f>
Run B<clipboard> every second. Only print output when it
changes. Similar to B<tail -f>.
Select the text 'END' to stop.
=back
=head1 AUTHOR
Copyright (C) 2023-2024 Ole Tange,
http://ole.tange.dk and Free Software Foundation, Inc.
=head1 LICENSE
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 SEE ALSO
B<tee>(1), B<xclip>(1), B<xsel>(1)
=cut
#debug_log=/tmp/T-debug
debug_log=/dev/null
if [ "-f" = "$1" ]; then
# run until c = END
while [ "$c" != "END" ]; do
c=$(clipboard 2>/dev/null)
if [ "$c" != "$last" ]; then
echo "$c"
last="$c"
fi
sleep 1
done
exit 0
fi
if tty -s ; then
# STDIN is terminal
# Don't care what STDOUT is
# clipboard | cat => paste clipboard
# clipboard => paste clipboard
echo xsel -o -b >> $debug_log
xsel -o -b
else
if [ -t 1 ] ; then
# STDOUT = terminal
# cat | clipboard => copy to clipboard
echo xsel -i -b >> $debug_log
tee >(xsel -i -b) | xclip -i >/dev/null
else
# STDOUT redir'ed
# cat | clipboard | cat => copy and forward clipboard
echo xsel tee >> $debug_log
tee >(xsel -i -b) | xclip -i >/dev/null
xsel -o -b
fi
fi