added clipboard manager scripts to dwm

This commit is contained in:
Sam 2024-06-16 00:26:14 +01:00
parent 0d6b762a6e
commit 97d8a879eb
3 changed files with 117 additions and 0 deletions

View File

@ -22,6 +22,7 @@
# Helper scripts # Helper scripts
./scripts/emoji-picker.nix ./scripts/emoji-picker.nix
./scripts/clipboard-save.nix
]; ];
home.packages = [ home.packages = [

View File

@ -0,0 +1,79 @@
{ pkgs, ... }:
{
home.packages = with pkgs; [
(writeShellScriptBin "clipboard-text-recall" ''
# Function to convert seconds to days, hours, minutes or seconds
clipboard_dir="/tmp/clipboard/text/*"
function format_time() {
local diff=$1
if ((diff<60)); then
echo "${diff} seconds"
elif ((diff<3600)); then
echo "$((diff/60)) minutes"
elif ((diff<86400)); then
echo "$((diff/3600)) hours"
else
echo "$((diff/86400)) days"
fi
}
# Function to get file info and sort it by modification time in reverse order
function get_files_info() {
now=$(date +%s)
# Sort files by modification time before the for loop
sorted_files=( $(ls -t $clipboard_dir) )
for file in "''${sorted_files[@]}"; do
mtime=$(stat -c %Y "$file")
diff=$((now-mtime))
# Get the number of lines in the file
num_lines=$(wc -l <"$file")
# Get the first non-empty line of the file and truncate it to 40 characters
first_line=$(grep . "$file" | head -n 1 | cut -c -60)
echo "$file ($num_lines lines) $(format_time $diff) ago, - '$first_line'..."
done
}
# Usage: get_files_info *
get_files_info $clipboard_dir | ${fzf}/bin/fzf --ansi \
--height 100% \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--preview 'bat --style=numbers --color=always {1}' \
--preview-window 'up,70%,+{2}+3/3,~3' \
--bind 'enter:become(cat {1} | nohup xclip -loops 0 -i -selection CLIPBOARD >/dev/null 2>&1 && touch {1})'
exit
'')
(writeShellScriptBin "clipboard-image-recall" ''
# Script to view and select and output images in image clipboard dir to clipboard.
if [ "$1" == "" ]; then
echo "Please provide directory"
exit 1
fi
filesdir="$(find $1 -type f -printf '%T@ %p\n' | sort -rn | cut -d ' ' -f 2-)"
output="$(echo "$filesdir" | nsxiv -tioq -g 1000x1000+0+0)"
num_lines=$( echo "$output" | wc -l)
if [ "$num_lines" -gt 1 ]; then
echo "More than one image selected, outputting list to clipboard as text"
echo "$output" | ${xclip}/bin/xclip -i -selection CLIPBOARD && ${libnotify}/bin/notify-send "Image to Clipboard" "Paths:\n$output\ncopied to clipboard" -t 5000
elif [ -z "$output" ]; then
echo "Nothing selected"
exit 1
else
echo "Outputted to clipboard"
xclip -selection clipboard -t image/png -i "$output" && ${libnotify}/bin/notify-send "Image to Clipboard" "$output copied to clipboard" -t 5000
fi
'')
];
}

View File

@ -0,0 +1,37 @@
{ pkgs, ... }:
{
home.packages = with pkgs; [
(writeShellScriptBin "clipboard-save" ''
# Script to automatically save copied images and text in specified directories
while ${clipnotify}/bin/clipnotify
do
image_location="/tmp/clipboard/images"
text_location="/tmp/clipboard/text"
[[ -d $image_location ]] || mkdir -p $image_location
[[ -d $text_location ]] || mkdir -p $text_location
case "$(xclip -selection clipboard -t TARGETS -o)" in
*image*)
echo "$(${xclip}/bin/xclip -selection clipboard -t TARGETS -o)"
filename=$(${xclip}/bin/xclip -selection clipboard -t image/png -o | ${openssl}/bin/openssl sha1 | cut -b 49-)
file_exists=$(/bin/ls $image_location | grep $filename | sed "s/\..*//")
[[ $filename != "$file_exists" ]] &&
xclip -selection clipboard -t image/png -o > "$image_location/$filename.png" &&
notify-send -t 5000 "Image Copied" "$image_location/$filename.png"
;;
*UTF8_STRING*)
echo "$(${xclip}/bin/xclip -selection clipboard -t TARGETS -o)"
filename=$(${xclip}/bin/xclip -selection clipboard -t UTF8_STRING -o | ${openssl}/bin/openssl sha1 | cut -b 49-)
file_exists=$(/bin/ls "$text_location" | grep "$filename" | sed "s/\..*//")
echo "$filename" "$file_exists"
[[ "$filename" != "$file_exists" ]] &&
xclip -selection clipboard -t UTF8_STRING -o > "$text_location/$filename"
echo "$text_location/$filename"
touch "$text_location/$filename"
;;
esac
done
'')
];
}