-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpipe-to-clipboard.sh
More file actions
executable file
·228 lines (165 loc) · 6.38 KB
/
Copy pathpipe-to-clipboard.sh
File metadata and controls
executable file
·228 lines (165 loc) · 6.38 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
declare -r SCRIPT_NAME="${BASH_SOURCE[0]##*/}" # This script's filename only, without any path components.
declare -r VERSION_NUMBER="2.01"
declare -r -i EXIT_CODE_SUCCESS=0
declare -r -i EXIT_CODE_ERROR=1
declare -r -i BOOLEAN_TRUE=0
declare -r -i BOOLEAN_FALSE=1
abort ()
{
echo >&2 && echo "Error in script \"$SCRIPT_NAME\": $*" >&2
exit $EXIT_CODE_ERROR
}
is_tool_installed ()
{
if command -v "$1" >/dev/null 2>&1 ;
then
return $BOOLEAN_TRUE
else
return $BOOLEAN_FALSE
fi
}
verify_tool_is_installed ()
{
local TOOL_NAME="$1"
local DEBIAN_PACKAGE_NAME="$2"
if is_tool_installed "$TOOL_NAME"; then
return
fi
local ERR_MSG="Tool '$TOOL_NAME' is not installed. You may have to install it with your Operating System's package manager."
if [[ $DEBIAN_PACKAGE_NAME != "" ]]; then
ERR_MSG+=" For example, under Ubuntu/Debian the corresponding package is called \"$DEBIAN_PACKAGE_NAME\"."
fi
abort "$ERR_MSG"
}
declare -r XSEL_TOOLNAME="xsel"
display_help ()
{
echo
echo "$SCRIPT_NAME version $VERSION_NUMBER"
echo "Copyright (c) 2022 R. Diez - Licensed under the GNU AGPLv3"
echo
echo "This script helps you pipe the output of a shell command to the X clipboard."
echo
echo "It is just a wrapper around '$XSEL_TOOLNAME', partly because I can never remember its command-line arguments."
echo
echo "In case of a single text line, the script automatically removes the end-of-line character."
echo "Otherwise, pasting the text to a shell console becomes annoying."
echo
echo "Usage example:"
echo " echo \"whatever\" | $SCRIPT_NAME"
echo "Afterwards, paste the copied text from the X clipboard into any application."
echo
echo "You can also specify one of the following options:"
echo " --help displays this help text"
echo " --version displays the tool's version number (currently $VERSION_NUMBER)"
echo " --license prints license information"
echo
echo "Exit status: 0 means success, anything else is an error."
echo
echo "Feedback: Please send feedback to rdiezmail-tools at yahoo.de"
echo
}
display_license()
{
cat - <<EOF
Copyright (c) 2022 R. Diez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 as published by
the Free Software Foundation.
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 Affero General Public License version 3 for more details.
You should have received a copy of the GNU Affero General Public License version 3
along with this program. If not, see L<http://www.gnu.org/licenses/>.
EOF
}
if (( $# != 0 )); then
case "$1" in
--help)
display_help
exit $EXIT_CODE_SUCCESS;;
--license)
display_license
exit $EXIT_CODE_SUCCESS;;
--version)
echo "$VERSION_NUMBER"
exit $EXIT_CODE_SUCCESS;;
-*) abort "Unknown option \"$1\".";;
*) abort "This tool takes no arguments. Run this tool with the --help option for usage information.";;
esac
fi
# If the user starts the tool from a terminal without piping any input...
if [ -t 0 ]; then
MSG="This tool is designed to take text data from stdin, by using a pipe to forward data from another process."
MSG+=" Run this tool with the --help option for usage information."
abort "$MSG"
fi
# Alternatively, you could use tool 'xclip' as follows, but the following xclip command does not work well with Emacs.
# I don't know why, but I suspect it's because xclip remains as a background process detached from the console
# and it does not close stdout.
# exec xclip -i -selection clipboard
verify_tool_is_installed "$XSEL_TOOLNAME" "xsel"
if false; then
# The output of a shell command is often a single word or a single line of text,
# followed by an end-of-line character. That end-of-line character becomes annoying
# if you paste the text to a shell console. That is the reason why I decided
# to remove the end-of-line character if there is just one line of text.
#
# I kept this implementation for future reference, in case I do not want
# to modify the data anymore.
exec "$XSEL_TOOLNAME" --input --clipboard
else
readarray TEXT_LINES
declare -r -i TEXT_LINE_COUNT="${#TEXT_LINES[@]}"
if false; then
echo "TEXT_LINE_COUNT=$TEXT_LINE_COUNT"
for (( i=0; i < TEXT_LINE_COUNT; i++ )); do
echo "$(( i + 1 )): <${TEXT_LINES[ $i ]}>"
done
fi
if (( TEXT_LINE_COUNT == 0 )); then
# This is the case with a command like this:
# echo -n "" | pipe-to-clipboard.sh
# We could also supply an empty string to 'xsel',
# which seems to have the same effect as clearing the clipboard.
"$XSEL_TOOLNAME" --clear --clipboard
echo "Got no text, clipboard cleared."
elif (( TEXT_LINE_COUNT == 1 )); then
FIRST_TEXT_LINE="${TEXT_LINES[0]}"
# The text line can be just one character, or just one end-of-line character,
# but it cannot be completely empty.
if [ -z "$FIRST_TEXT_LINE" ]; then
abort "Internal error: The text line is not expected to be empty."
fi
# Remove an eventual end-of-line character.
FIRST_TEXT_LINE="${FIRST_TEXT_LINE%$'\n'}"
if [ -z "$FIRST_TEXT_LINE" ]; then
# This is the case with a command like this:
# echo "" | pipe-to-clipboard.sh
# The new-line character makes it 1 text line.
# We don't actually need this special case, because supplying an empty string
# to 'xsel' seems to have the same effect as clearing the clipboard.
"$XSEL_TOOLNAME" --clear --clipboard
echo "Got an empty text line, clipboard cleared."
else
echo -n "$FIRST_TEXT_LINE" | "$XSEL_TOOLNAME" --input --clipboard
echo "1 text string placed on the clipboard."
fi
else
{
for (( i = 0; i < TEXT_LINE_COUNT; i++ )); do
echo -n "${TEXT_LINES[ $i ]}"
done
} | "$XSEL_TOOLNAME" --input --clipboard
echo "$TEXT_LINE_COUNT text lines placed on the clipboard."
fi
fi
# Note that xsel forks and detaches from the terminal (if it is not just clearing the clipboard).
# xsel then waits indefinitely for other programs to retrieve the text it is holding,
# perhaps multiple times. When something else replaces or deletes the clipboard's contents,
# xsel automatically terminates.