~skiqqy/bin

8ecc22ceaf02ecaad3c432d7083882458da0e8c5 — Stephen Cochrane 1 year, 8 months ago 2ffbd75
Progress on clip
1 files changed, 68 insertions(+), 2 deletions(-)

R copy => clip
R copy => clip +68 -2
@@ 1,17 1,81 @@
#!/usr/bin/env bash
# Simple xclip X dmenu script
# Simple xclip X dmenu script, TL;DR Clipboard manager
# Author: Skiqqy
scriptname="$(basename "$0")"

usage()
{
	cat << EOF
$scriptname ~ Simple copy buffer

$scriptname ~ Simple clipboard manager

    Options
      -h                  Shows this message.
      -b PATH/TO/FILE     Specify the buffer file to use, current = "$buffer"

    Commands
      copy                Put current x selection into the buffer.
      paste [put]         Select an item from the buffer, if put is supplied, paste automatically.
      clean               Reset the buffer

EOF
	exit "${1:-0}"
}

# Select an item from the buffer to paste
paste()
{
	local len
	touch "$buffer"
	len="$(wc -l "$buffer" | tr -s ' ' | cut -d ' ' -f 1)"

	if [ "$len" -gt 0 ]
	then
		dmenu -l "$len" < "$buffer" | xclip -r -selection clipboard
	else
		printf 'Buffer is empty.\n'
		return 1
	fi

	if [ "$1" = "put" ]
	then
		xdotool key 'ctrl+shift+v' # Paste the output.
	fi
}

# Put current selection into the buffer
copy()
{
	if [ ! "$(wc -l "$buffer"| tr -s ' ' | cut -d ' ' -f 1)" -le "$max" ]
	then
		# Remove oldest item, ie top item in file
		printf 'Buffer full, removing oldest entry...\n'
		tail "$buffer" -n +2 > "$buffer.tmp"
		mv "$buffer.tmp" "$buffer"
	fi

	if [ -n "$(xclip -o)" ]
	then
		{
		xclip -o
		printf '\n'
		} >> "$buffer"
	else
		printf 'Selection is empty.\n'
		return 1
	fi
}

# Reset the buffer
clean()
{
	printf 'Resetting buffer...\n'
	true > "$buffer" # True is just to no-op
}

main()
{
	max=10 # Max items to be kept in the buffer.
	buffer='/tmp/copy.buffer'

	while getopts hb: opt


@@ 38,6 102,8 @@ main()
	shift 1

	case "$com" in
		copy|paste|clean) $com "$@"
			;;
		'')
			usage
			;;