nixos/home/common/optional/desktop/dwm/scripts/clipboard-recall.nix

80 lines
2.7 KiB
Nix
Raw Normal View History

2024-06-16 00:26:14 +01:00
{ 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
'')
];
}