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

87 lines
3.2 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"
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.
#!/bin/bash
width=$(${xorg.xwininfo}/bin/xwininfo -root | grep Width | sed "s/^.*:\s//")
height=$(${xorg.xwininfo}/bin/xwininfo -root | grep Height | sed "s/^.*:\s//")
offset_horiz=$(( $width/8 ))
offset_vert=$(( $height/8 ))
scale_width=$(( $width/4 + $width/2 ))
scale_height=$(( $height/4 + $height/2 ))
2024-06-16 00:26:14 +01:00
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}/bin/nsxiv -tioq -g "$scale_width"x"$scale_height"-$offset_horiz-$offset_vert -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
'')
];
}