summaryrefslogtreecommitdiff
path: root/dot/local/bin/cutbuffer
blob: daf8e37b860b9c178c67180cc515146e8a7c84d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
dest_basename=cutbuffer_grab
dest_extension=txt
date_format=+%F_%H%M%S

destdir=$HOME/$dest_basename
nameglob=$dest_basename.*.$dest_extension
cutbuffer_count=8

cutbuffer_push()
{
    now=$(date "$date_format")
    dest=$dest_basename.$now.$dest_extension
    xsel --output > "$dest"
    xcb -r 1
    xcb -s 0 < "$dest"
}

cutbuffer_pop()
{
    f=$(find . -maxdepth 1 -type f  -name "$nameglob" -print0 | sort -z -V -r | head -z -n 1 | xargs -0)

    xcb -s 0 < "$f"
    xsel --input --primary < "$f"
    xsel --input --clipboard < "$f"

    mkdir -p .popped
    mv "$f" -t .popped
}

cutbuffer_menu()
{
    find_any | xargs -0 -x bash -c 'populate "$@"'
    xcb -l v
}

find_any()
{
    find . .popped -maxdepth 1 -type f  -name "$nameglob" -printf "%f\t%h/%f\0" | sort -k1 -z -V -r | head -z -n "$cutbuffer_count" | perl -0pe 's/.*?\t//'
}

populate()
{
    i=0
    for f
    do
        xcb -s $i < "$f"
        let ++i
    done
}
export -f populate

set -e
mkdir -p "$destdir"
cd "$destdir"
case "$*" in
    push|pop|menu) cutbuffer_$1 ;;
esac