From 97d8a879eb381c00e636a5e4e98486358847e125 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 16 Jun 2024 00:26:14 +0100 Subject: [PATCH] added clipboard manager scripts to dwm --- home/common/optional/desktop/dwm/default.nix | 1 + .../desktop/dwm/scripts/clipboard-recall.nix | 79 +++++++++++++++++++ .../desktop/dwm/scripts/clipboard-save.nix | 37 +++++++++ 3 files changed, 117 insertions(+) create mode 100644 home/common/optional/desktop/dwm/scripts/clipboard-recall.nix create mode 100644 home/common/optional/desktop/dwm/scripts/clipboard-save.nix diff --git a/home/common/optional/desktop/dwm/default.nix b/home/common/optional/desktop/dwm/default.nix index 7f8a034..46db3fd 100644 --- a/home/common/optional/desktop/dwm/default.nix +++ b/home/common/optional/desktop/dwm/default.nix @@ -22,6 +22,7 @@ # Helper scripts ./scripts/emoji-picker.nix + ./scripts/clipboard-save.nix ]; home.packages = [ diff --git a/home/common/optional/desktop/dwm/scripts/clipboard-recall.nix b/home/common/optional/desktop/dwm/scripts/clipboard-recall.nix new file mode 100644 index 0000000..e9e63ea --- /dev/null +++ b/home/common/optional/desktop/dwm/scripts/clipboard-recall.nix @@ -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 + '') + + ]; +} diff --git a/home/common/optional/desktop/dwm/scripts/clipboard-save.nix b/home/common/optional/desktop/dwm/scripts/clipboard-save.nix new file mode 100644 index 0000000..1987617 --- /dev/null +++ b/home/common/optional/desktop/dwm/scripts/clipboard-save.nix @@ -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 + + '') + ]; +}