|
root / base / hammerhead-desktop / hh-recent-pipemenu
hh-recent-pipemenu Plain Text 48 lines 1.4 KB
 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
#!/bin/sh
# hh-recent-pipemenu - Openbox "Recent Files" pipe menu (GTK-free)
#
# Loosely modeled on CrunchBang++'s cbpp-recent-files-pipemenu
# (github.com/CBPP/cbpp-pipemenus), but that one parses GTK's
# ~/.local/share/recently-used.xbel, which nothing on Hammerhead writes
# (no GTK). Instead: find(1) over $HOME for recently-modified
# docs/images, opened with the matching viewer.
#
# Usage: <menu id="recent-menu" label="Recent Files" execute="hh-recent-pipemenu"/>

max_entries=12
home="${HOME:-/root}"

xml_escape() {
    printf '%s' "$1" | sed \
        -e 's/&/\&amp;/g' \
        -e 's/</\&lt;/g' \
        -e 's/>/\&gt;/g' \
        -e 's/"/\&quot;/g'
}

viewer_for() {
    case "$1" in
        *.pdf) echo "hh-pdf" ;;
        *.png|*.jpg|*.jpeg|*.gif|*.bmp) echo "hh-images" ;;
        *) echo "xnedit" ;;
    esac
}

printf '<openbox_pipe_menu>\n'

count=0
find "$home" -maxdepth 3 -type f \
    \( -iname '*.pdf' -o -iname '*.txt' -o -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' \) \
    -mtime -14 -print 2>/dev/null | while IFS= read -r f; do
    viewer="$(viewer_for "$f")"
    label="$(xml_escape "${f##*/}")"
    path="$(xml_escape "$f")"
    printf '  <item label="%s">\n' "$label"
    printf '    <action name="Execute"><command>%s %s</command></action>\n' "$viewer" "$path"
    printf '  </item>\n'
    count=$((count + 1))
    [ "$count" -ge "$max_entries" ] && break
done

printf '</openbox_pipe_menu>\n'