#!/bin/bash . $(ffoom path) ffoo import inigrep ffoo import pretty # # Where to store clips # STORAGE_DIR="$SATURNIN_CACHE_HOME/clips" usage() { mkusage "[ls]" \ "save [-1|-2|-c]" \ "load [-1|-2|-c]" \ "rm" \ "clean" } clipln() { # # Print desired clipboard(s) and \n # case $1 in primary|secondary|clipboard) xclip -o -selection $1 2>/dev/null ;; ALL) xclip -o -selection primary 2>/dev/null xclip -o -selection secondary 2>/dev/null xclip -o -selection clipboard 2>/dev/null ;; esac echo "" } save_clip() { # # Save single clip # local clipname=$1 mkdir -p "$STORAGE_DIR" || die "could not create directory for saving" local path="$STORAGE_DIR/$(date +%Y%m%d-%H%M%S.clip)" clipln $clipname > "$path" } lsclips() { # # List clips with MIME types and hints # local clipname=$1 local ft hint name test -d "$STORAGE_DIR" || return 0 ls "$STORAGE_DIR/"*.clip 2>/dev/null \ | while read name; do ft=$(file -b -i $name | cut -d\; -f1) case $ft in text/*) hint=$(head -c 80 $name | tr '\n' '↵') ;; *) hint=$(head -c 16 $name | hexdump -C | head -1) ;; esac echos "$(basename $name) || $ft || $hint" done } lspaths() { # # List only clip paths # ls "$STORAGE_DIR/"*.clip } load_clip() { # # Load single clip of choice # local clipname=$1 local name=$(pick_clip) test -n "$name" || return 1 cat $STORAGE_DIR/$name | xclip -i -selection $clipname } rm_clip() { # # Remove single clip of choice # local clipname=$1 local name=$(pick_clip) test -n "$name" || return 1 rm -f $STORAGE_DIR/$name } rm_all() { # # Dump all clips # test -n "$STORAGE_DIR" || die "storage directory is unset, aborting" test -d "$STORAGE_DIR" || return 0 find "$STORAGE_DIR" -name "*.clip" | xargs rm -f rmdir "$STORAGE_DIR" 2>/dev/null | : } pick_clip() { saturnin clip ls \ | saturnin dmenu \ | cut -d\ -f 1 } clipname=primary action=list_nice while true; do case "$1" in save) action=save; shift ;; load) action=load; shift ;; ls) action=list; shift ;; lsh) action=list_nice shift ;; rm) action=remove; shift ;; clean) action=clean; shift ;; -1) clipname=primary; shift ;; -2) clipname=secondary; shift ;; -c) clipname=clipboard; shift ;; "") break ;; *) usage ;; esac done debug "\$@='$@'" debug -v clipname action case $action in save) save_clip $clipname ;; load) load_clip $clipname ;; remove) rm_clip ;; clean) rm_all ;; list) lspaths ;; list_nice) lsclips ;; esac