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

88 lines
3.3 KiB
Nix
Raw Permalink 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"
2024-06-16 00:26:14 +01:00
elif ((diff<3600)); then
echo "''$((diff/60)) minutes"
2024-06-16 00:26:14 +01:00
elif ((diff<86400)); then
echo "''$((diff/3600)) hours"
2024-06-16 00:26:14 +01:00
else
echo "''$((diff/86400)) days"
2024-06-16 00:26:14 +01:00
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}/bin/bat --style=numbers --color=always {1}' \
2024-06-16 00:26:14 +01:00
--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.
monitor_name=$(get-focused-monitor)
monitor_info=$(xrandr | grep "$monitor_name")
width=$(echo $monitor_info | grep -oP '\d+x\d+' | cut -dx -f1)
height=$(echo $monitor_info | grep -oP '\d+x\d+' | cut -dx -f2)
scale_width=$(( $width/4 + $width/2 ))
scale_height=$(( $height/4 + $height/2 ))
if [ "$1" == "" ] || [ -f "$1" ]; then
notify-send -t 1000 "Clipboard Recall" "Directory not specified or missing."
2024-06-16 00:26:14 +01:00
exit 1
fi
2024-06-16 00:26:14 +01:00
filesdir="$(find $1 -type f -printf '%T@ %p\n' | sort -rn | cut -d ' ' -f 2-)"
[ "$filesdir" == "" ] && notify-send -t 1000 "Clipboard Recall" "No images in directory" && exit 1
output="$(echo "$filesdir" | ${nsxiv}/bin/nsxiv -tioq -g "$scale_width"x"$scale_height" -N nsxiv-float )"
2024-06-16 00:26:14 +01:00
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}/bin/xclip -selection clipboard -t image/png -i "$output" && ${libnotify}/bin/notify-send "Image to Clipboard" "$output copied to clipboard" -t 5000
2024-06-16 00:26:14 +01:00
fi
'')
];
}