-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdman
executable file
·75 lines (59 loc) · 1.95 KB
/
dman
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
progname=$(basename $0)
main () { parse_opts "$@"
if [ "$terminal" = true ]; then
apropos . | fzf | cut -d " " -f 1-2 | tr -d " " | xargs -I {} man '{}'
exit $?
fi
directory=/tmp/$progname
cleanup() { rm -rf $directory; }
trap cleanup EXIT
trap exit INT HUP TERM
[ ! -d "$directory" ] && mkdir $directory
title="$(apropos . | dmenu -l 10 | cut -d " " -f 1-2 | tr -d " ")"
[ "$title" = "" ] && exit 1
files="$(for i in $title; do
file="$directory/$i – $(printf '%s' "$i" | xargs -r man | head -1 |
tr -s " " | cut -d " " -f 2- | rev | cut -d " " -f 2- | rev |
# Straight apostrophes with non-whitespace characters surrounding them:
# https://unix.stackexchange.com/a/713841/513913
sed -e :1 -e "s/\([^[:space:]]\)'\([^[:space:]]\)/\1’\2/g" -e t1 |
sed "s/'/‘/ ; s/'/’/")"
printf '%s' "$i" | xargs -r man -Tpdf > "$file"
[ -s "$file" ] && printf '%s\n' "$file"
done)"
if [ -z "$READER" ]; then
if [ ! -x "$(command -v xdg-mime)" ]; then
printf "$progname: %s\n" '`$READER` is not set and xdg-utils is not installed.' >&2
exit 1
fi
READER="$(xdg-mime query default application/pdf | sed s/\.desktop$//)"
fi
printf '%s' "$files" | sed -e "s|'|'\\\\''|g ; s|\(.*\)|'\1'|" | xargs $READER
}
help() { printf "Usage:\t%s" "$progname [option]
Options:
Modes:
-t|--terminal │ Use the terminal user interface (fzf and man)
-g|--graphical │ Use the graphical user interface (dmenu and PDF) (default)
-h|--help │ Print this help message and exit
"; }
parse_opts() {
while getopts htg- OPT; do
if [ "$OPT" = "-" ]; then
OPT="${OPTARG%%=*}"
OPTARG="${OPTARG#$OPT}"
OPTARG="${OPTARG#=}"
fi
case "$OPT" in
h | help) help ; exit 0 ;;
t | terminal) terminal=true ;;
g | graphical) graphical=true ;;
??*) printf "%s\n" "Illegal option --$OPT" >&2 ; exit 2 ;;
?) exit 2 ;;
esac
done
shift $((OPTIND-1))
[ "$graphical" != true -a "$terminal" != true ] && graphical=true
}
main "$@"