From 569087787f9f2560ece03d8b2b28119da8374576 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Tue, 4 Jan 2022 15:49:07 -0300 Subject: src/bin/copy: Promote from shell alias to executable --- src/bin/copy | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 src/bin/copy (limited to 'src') diff --git a/src/bin/copy b/src/bin/copy new file mode 100755 index 00000000..04dc265d --- /dev/null +++ b/src/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 -- cgit v1.3