| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 | #!/bin/bash
. $(ffoom path)
ffoo import config
ffoo import pretty
STORAGE_DIR="$SATURNIN_CACHE_HOME/clips"
usage() {
    usage_is "[ls]"
    usage_is "save [-1|-2|-c]"
    usage_is "load [-1|-2|-c]"
    usage_is "rm"
    usage_is "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() {
    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() {
    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
}
load_clip() {
    local clipname=$1
    local name=$(saturnin clip ls | saturnin dmenu | cut -d\   -f 1)
    cat $STORAGE_DIR/$name | xclip -i -selection $clipname
}
rm_clip() {
    local clipname=$1
    local name=$(saturnin clip ls | saturnin dmenu | cut -d\   -f 1)
    rm -f $STORAGE_DIR/$name
}
rm_all() {
    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 | :
}
clipname=primary
action=list
while true; do case "$1" in
    save)   action=save;        shift ;;
    load)   action=load;        shift ;;
    ls)     action=list;        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)   lsclips ;;
esac
 |