Latest fixes

This commit is contained in:
G2-Games 2022-12-15 15:16:13 -06:00
parent 1a0495355a
commit 7c248ad649

View file

@ -10,26 +10,31 @@ number=0
empty="[\033[35m?\033[0m]" empty="[\033[35m?\033[0m]"
success="[\033[32m✓\033[0m]" success="[\033[32m✓\033[0m]"
fail="[\033[31m✗\033[0m]" fail="[\033[31m✗\033[0m]"
wait="[\033[34m…\033[0m]"
setup () {
copy () { # Test if the target exists
tput sc if [ -z "$target" ] || ! [ -f "$target" ]; then
echo "Copying $target to clipboard..." tput rc el ed
if [ -z $target ] || ! [ -f $target ]; then
echo "$fail File not found" echo "$fail File not found"
exit exit 1
fi fi
# Find the absolute path of the requested file or folder... # Find the absolute path of the requested file or folder...
path="$(cd "$(dirname -- "$target")" >/dev/null; pwd -P)/$(basename -- "$target")" 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" echo "$fail \033[33;1m$target\033[0m already in clipboard"
exit 0 exit 0
fi fi
# Create the clipboard directory if it doesn't already exist # Create the clipboard directory if it doesn't already exist
mkdir -p $clipboarddir mkdir -p $clipboarddir
# Create the clipboard file if it doesn't already exist, if it does, assign a number to the file # Create the clipboard file if it doesn't already exist, if it does, assign a number to the file
if [ -f $clipboardfile ]; then if [ -f $clipboardfile ]; then
number=$(tail -n 1 $clipboardfile | cut -d ':' -f2) number=$(tail -n 1 $clipboardfile | cut -d ':' -f2)
@ -38,16 +43,32 @@ copy () {
touch $clipboardfile touch $clipboardfile
number=0 number=0
fi fi
}
copy () {
tput sc
echo "$wait Copying $target to clipboard..."
setup
# Store the file location in the clipboard with the operation # Store the file location in the clipboard with the operation
cp -r "$path" "$clipboarddir/$number.tmp" cp -r "$path" "$clipboarddir/$number.tmp"
echo "copy:$number:$path" >> $clipboardfile echo "copy:$number:$path" >> $clipboardfile
tput rc tput rc el ed
tput el
echo "$success Copied \033[33;1m$target\033[0m to clipboard" echo "$success Copied \033[33;1m$target\033[0m to clipboard"
} }
ct () { # Cut 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 cl () { # Clear
@ -59,75 +80,115 @@ cl () { # Clear
fi 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 [ -f $clipboardfile ]; then
if [ -z $target ]; then lines="$(cat "$clipboardfile" | cut -d ':' -f2)"
file=$(tail -n 1 $clipboardfile | cut -d ':' -f3) linenum=$(echo "$lines" | grep -n "$target" | cut -d ':' -f1)
name=$(tail -n 1 $clipboardfile | rev | cut -d '/' -f1 | rev) if [ -z "$target" ]; then
operation=$(tail -n 1 $clipboardfile | cut -d ':' -f1) file="$(tail -n 1 $clipboardfile | cut -d ':' -f3)"
number=$(tail -n 1 $clipboardfile | cut -d ':' -f2) name="$(tail -n 1 $clipboardfile | rev | cut -d '/' -f1 | rev)"
elif [ $(cat "$clipboardfile" | wc -l) -ge $((target + 1)) ]; then 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)) target=$((target + 1))
file=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f3) file="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f3)"
name=$(sed "${target}q;d" $clipboardfile | rev | cut -d '/' -f1 | rev) name="$(sed "${linenum}q;d" $clipboardfile | rev | cut -d '/' -f1 | rev)"
operation=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f1) operation="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f1)"
number=$(sed "${target}q;d" $clipboardfile | cut -d ':' -f2) number="$(sed "${linenum}q;d" $clipboardfile | cut -d ':' -f2)"
else else
echo "$fail Invalid history line" echo "$fail Invalid history line"
exit exit
fi 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?" echo -n "\033[33;1m$name\033[0m already exists in this folder, do you want to overwrite?"
read -p " y/N " yn read -p " y/N " yn
case $yn in case $yn in
[Yy]* ) overwrite=1;; [Yy]* ) ;;
[Nn]* ) tput rc;tput el;echo "$fail Canceled"; exit;; * ) tput rc el ed;echo "$fail Canceled paste"; exit;;
* ) tput rc;tput el;echo "$fail Canceled"; exit;;
esac 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 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 else
echo "$empty Nothing to paste" echo "$empty Nothing to paste"
exit 0 exit 0
fi fi
} }
show () { list () { # Show clipboard
first=" \033[32;1m<= Current\033[0m"
if [ -f $clipboardfile ]; then if [ -f $clipboardfile ]; then
echo "\033[35;1;4mClipboard History:\033[0m" echo "\033[35;1;4mClipboard History:\033[0m"
printf '%s\n' "$(tac $clipboardfile)" | while IFS= read -r line; do printf '%s\n' "$(tac $clipboardfile)" | while IFS= read -r line; do
filename=$(echo "$line" | rev | cut -d '/' -f1 | rev) filename=$(echo "$line" | rev | cut -d '/' -f1 | rev)
operation=$(echo "$line" | cut -d ':' -f1)
number=$(echo $line | cut -d ':' -f2) number=$(echo $line | cut -d ':' -f2)
echo "$number: $filename" echo "\033[34;1m$number:\033[33m $operation\033[0m $filename$first"
first=""
done done
else else
echo "$empty Clipboard empty" echo "$empty Clipboard empty"
fi 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<copy|cut|paste|clear|show> \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 copy
elif [ "$op" = "cut" ]; then elif [ "$op" = "cut" ] || [ "$op" = "ct" ]; then
ct ct
elif [ "$op" = "paste" ]; then elif [ "$op" = "paste" ]; then
pt pt
elif [ "$op" = "clear" ]; then elif [ "$op" = "clear" ]; then
cl cl
elif [ "$op" = "show" ]; then elif [ "$op" = "remove" ] || [ "$op" = "rm" ]; then
show remove
elif [ "$op" = "list" ] || [ "$op" = "ls" ]; then
list
elif [ "$op" = "help" ] || [ "$op" = "?" ]; then
help
elif [ -z "$op" ]; then elif [ -z "$op" ]; then
show list
else else
echo "$fail Invalid argument" echo "$fail Invalid argument"
echo
help
fi fi