tangetools/clipboard/clipboard
2023-06-09 18:09:10 +02:00

110 lines
2.2 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 | xsel -i -b>
=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<xsel -i -b; xsel -o -b>
=head3 Example
cat | clipboard | cat
=head1 AUTHOR
Copyright (C) 2023 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<xsel>(1), B<tee>(1), B<cat>(1)
=cut
#debug_log=/tmp/T-debug
debug_log=/dev/null
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
xsel -i -b
else
# STDOUT redir'ed
# cat | clipboard | cat => copy and forward clipboard
echo xsel tee >> $debug_log
tee >(xsel -i -b)
fi
fi