aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-05-12 02:17:29 -0300
committerEuAndreh <eu@euandre.org>2022-05-12 02:17:29 -0300
commit7b74ea900231ab7713f642c556ee5cb67956e718 (patch)
tree7dcb09d055e971d59a09a55258531fd5566aa351
parent.local/bin/msg: Allow $ACTION_DONE to fallthrough to "wait" statement (diff)
downloaddotfiles-7b74ea900231ab7713f642c556ee5cb67956e718.tar.gz
dotfiles-7b74ea900231ab7713f642c556ee5cb67956e718.tar.xz
.local/bin/copy: Add working version
-rwxr-xr-x.local/bin/copy67
1 files changed, 67 insertions, 0 deletions
diff --git a/.local/bin/copy b/.local/bin/copy
new file mode 100755
index 0000000..64e1e32
--- /dev/null
+++ b/.local/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