-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-read-pdf
executable file
·51 lines (43 loc) · 1.36 KB
/
menu-read-pdf
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
#!/usr/bin/sh
# Program for choosing the file.
# Default: "dmenu -p"
MENU_CMD=${1:-"dmenu -p"}
# Program to display the chosen pdf file.
# Default: zathura
PDF_READER=${2:-"zathura"}
# Hide file extension and certain directory names.
HIDE_DIRS='\(Downloads\|Documents_static\|Documents\)'
FORMATTING="sed s/^${HIDE_DIRS}\///;s/.pdf$//"
# Fix filenames with special characters.
placeholder () {
[ $1 = "add" ] \
&& sed s/\ /SPACE_PLACEHOLDER/g \
| sed s/\(/LPARENS_PLACEHOLDER/g \
| sed s/\)/RPARENS_PLACEHOLDER/g \
|| [ $1 = "remove" ] \
&& sed s/SPACE_PLACEHOLDER/\ /g \
| sed s/LPARENS_PLACEHOLDER/\(/g \
| sed s/RPARENS_PLACEHOLDER/\)/g \
|| echo "ERROR: Invalid argument $1 for 'placeholder': expected 'add' or 'remove'"
}
# List all pdfs in $HOME.
FILES=`rg --files \
--glob '!Projects' \
--glob '*.pdf' \
| placeholder add`
# Use $MENU_CMD to choose a file.
CHOICE=`echo $FILES \
| tr ' ' '\n' \
| placeholder remove \
| $FORMATTING \
| sort \
| $MENU_CMD 'Read PDF file: ' \
| placeholder add`
# Find filepath from filename.
FILEPATH=`echo $FILES \
| tr ' ' '\n' \
| rg $CHOICE.pdf - \
| placeholder remove`
# If something is chosen (ESC not pressed), then open it in $PDF_READER.
[ -z $CHOICE ] && notify-send "$FILE not found" \
|| $PDF_READER "$FILEPATH"