aboutsummaryrefslogtreecommitdiff
path: root/bin/copy
diff options
context:
space:
mode:
Diffstat (limited to 'bin/copy')
-rwxr-xr-xbin/copy67
1 files changed, 67 insertions, 0 deletions
diff --git a/bin/copy b/bin/copy
new file mode 100755
index 0000000..64e1e32
--- /dev/null
+++ b/bin/copy
@@ -0,0 +1,67 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ copy [-n] < STDIN
+ copy -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -n remove newlines
+ -h, --help show this message
+
+ Examples:
+
+ Copy numbers to clipboard:
+ seq 10 | copy
+
+ Copy string without newline:
+ echo 'with automatic newline' | copy -n
+ EOF
+}
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+TRIM=false
+while getopts 'nh' flag; do
+ case "$flag" in
+ n)
+ TRIM=true
+ ;;
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+if [ "$TRIM" = true ]; then
+ cat - | tr -d '\n' | xclip -sel clip
+else
+ cat - | xclip -sel clip
+fi