diff --git a/clipboard.sh b/clipboard.sh index fd2a3e3..c7c4513 100755 --- a/clipboard.sh +++ b/clipboard.sh @@ -10,26 +10,31 @@ number=0 empty="[\033[35m?\033[0m]" success="[\033[32m✓\033[0m]" fail="[\033[31m✗\033[0m]" +wait="[\033[34m…\033[0m]" - -copy () { - tput sc - echo "Copying $target to clipboard..." - if [ -z $target ] || ! [ -f $target ]; then +setup () { + # Test if the target exists + if [ -z "$target" ] || ! [ -f "$target" ]; then + tput rc el ed echo "$fail File not found" - exit + exit 1 fi # Find the absolute path of the requested file or folder... path="$(cd "$(dirname -- "$target")" >/dev/null; pwd -P)/$(basename -- "$target")" - if grep -sq "$path" $clipboardfile; then + name="$(echo $path | rev | cut -d '/' -f1 | rev)" + + # Make sure the file isn't already in the clipboard + if grep -sqF "$name" "$clipboardfile"; then + tput rc el ed echo "$fail \033[33;1m$target\033[0m already in clipboard" exit 0 fi # Create the clipboard directory if it doesn't already exist mkdir -p $clipboarddir + # Create the clipboard file if it doesn't already exist, if it does, assign a number to the file if [ -f $clipboardfile ]; then number=$(tail -n 1 $clipboardfile | cut -d ':' -f2) @@ -38,16 +43,32 @@ copy () { touch $clipboardfile number=0 fi +} + +copy () { + tput sc + echo "$wait Copying $target to clipboard..." + + setup + # Store the file location in the clipboard with the operation cp -r "$path" "$clipboarddir/$number.tmp" echo "copy:$number:$path" >> $clipboardfile - tput rc - tput el + tput rc el ed echo "$success Copied \033[33;1m$target\033[0m to clipboard" } ct () { # Cut - echo cut + tput sc + echo "$wait Cutting $target to clipboard..." + + setup + + # Store the file location in the clipboard with the operation + cp -r "$path" "$clipboarddir/$number.tmp" + echo "cut:$number:$path" >> $clipboardfile + tput rc el ed + echo "$success Cut \033[33;1m$target\033[0m to clipboard" } cl () { # Clear @@ -59,75 +80,115 @@ cl () { # Clear fi } -pt () { +remove () { + target=$(echo "$target" | sed 's/[^0-9]*//g') + if [ -n "$target" ]; then + lines="$(cat "$clipboardfile" | cut -d ':' -f2)" + linenum=$(echo "$lines" | grep -n "$target" | cut -d ':' -f1) + if [ -n "$linenum" ]; then + sed -i "${linenum}d" $clipboardfile + rm -f "$clipboarddir/$target.tmp" + echo "$success Deleted clipboard item #$target" + else + echo "$fail Item #$target doesn't exist" + fi + exit + else + echo "$fail Please specify a clip number to remove" + fi +} + +pt () { # Paste + target=$(echo "$target" | sed 's/[^0-9]*//g') if [ -f $clipboardfile ]; then - if [ -z $target ]; then - file=$(tail -n 1 $clipboardfile | cut -d ':' -f3) - name=$(tail -n 1 $clipboardfile | rev | cut -d '/' -f1 | rev) - operation=$(tail -n 1 $clipboardfile | cut -d ':' -f1) - number=$(tail -n 1 $clipboardfile | cut -d ':' -f2) - elif [ $(cat "$clipboardfile" | wc -l) -ge $((target + 1)) ]; then + lines="$(cat "$clipboardfile" | cut -d ':' -f2)" + linenum=$(echo "$lines" | grep -n "$target" | cut -d ':' -f1) + if [ -z "$target" ]; then + file="$(tail -n 1 $clipboardfile | cut -d ':' -f3)" + name="$(tail -n 1 $clipboardfile | rev | cut -d '/' -f1 | rev)" + operation="$(tail -n 1 $clipboardfile | cut -d ':' -f1)" + number="$(tail -n 1 $clipboardfile | cut -d ':' -f2)" + elif [ $(cat "$clipboardfile" | wc -l) -ge $((target + 1)) ] && [ "$target" -eq "$target" ]; then target=$((target + 1)) - file=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f3) - name=$(sed "${target}q;d" $clipboardfile | rev | cut -d '/' -f1 | rev) - operation=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f1) - number=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f2) + file="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f3)" + name="$(sed "${linenum}q;d" $clipboardfile | rev | cut -d '/' -f1 | rev)" + operation="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f1)" + number="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f2)" else echo "$fail Invalid history line" exit fi - if [ -f $name ]; then - tput sc + tput sc + if [ -f "$name" ]; then echo -n "\033[33;1m$name\033[0m already exists in this folder, do you want to overwrite?" read -p " y/N " yn case $yn in - [Yy]* ) overwrite=1;; - [Nn]* ) tput rc;tput el;echo "$fail Canceled"; exit;; - * ) tput rc;tput el;echo "$fail Canceled"; exit;; + [Yy]* ) ;; + * ) tput rc el ed;echo "$fail Canceled paste"; exit;; esac - if [ $overwrite -eq 1 ]; then - echo cp -rf "$clipboarddir/$number.tmp" $name - tput rc - tput el - echo "$success Pasted \033[33;1m$name\033[0m" - else - exit - fi - else - cp "$clipboarddir/$number.tmp" $name - echo "$success Pasted \033[33;1m$name\033[0m" fi + echo "$wait Pasting \033[33;1m$name\033[0m from clipboard..." + cp -rf "$clipboarddir/$number.tmp" "$name" + if [ "$operation" = "cut" ]; then + rm $file + fi + tput rc el ed + echo "$success Pasted \033[33;1m$name\033[0m" else echo "$empty Nothing to paste" exit 0 fi } -show () { +list () { # Show clipboard + first=" \033[32;1m<= Current\033[0m" if [ -f $clipboardfile ]; then echo "\033[35;1;4mClipboard History:\033[0m" printf '%s\n' "$(tac $clipboardfile)" | while IFS= read -r line; do filename=$(echo "$line" | rev | cut -d '/' -f1 | rev) + operation=$(echo "$line" | cut -d ':' -f1) number=$(echo $line | cut -d ':' -f2) - echo "$number: $filename" + echo "\033[34;1m$number:\033[33m $operation\033[0m $filename$first" + first="" done else echo "$empty Clipboard empty" fi } -if [ "$op" = "copy" ]; then +help () { + echo "\033[33mclipboard.sh:\033[0m command line clipboard utility +\033[35;1;4mUsage:\033[0m +\033[32mcb\033[0m \033[34m \033[36m[file...] + +\033[35;1;4mOperations:\033[0m + copy [file] : Copy a file + cut [file] : Cut a file + paste [clip #] : Paste a file + clear : Clear the clipboard + remove [clip #] : + list : Show the copied files (default) +" +} + +if [ "$op" = "copy" ] || [ "$op" = "cp" ]; then copy -elif [ "$op" = "cut" ]; then +elif [ "$op" = "cut" ] || [ "$op" = "ct" ]; then ct elif [ "$op" = "paste" ]; then pt elif [ "$op" = "clear" ]; then cl -elif [ "$op" = "show" ]; then - show +elif [ "$op" = "remove" ] || [ "$op" = "rm" ]; then + remove +elif [ "$op" = "list" ] || [ "$op" = "ls" ]; then + list +elif [ "$op" = "help" ] || [ "$op" = "?" ]; then + help elif [ -z "$op" ]; then - show + list else echo "$fail Invalid argument" + echo + help fi