aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/menuman
blob: 26c9bfc5cbfeebf8c7a8e8849208552019d7439e (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
60
61
62
63
64
#!/bin/sh
# Search and open man page
# Options: 
# -t use in terminal with fzf otherwise launch gui menu
# -s search with apropos.

issearch=
isterminal=

printusage() {
	echo "Options:" >&2
	echo " -t use in terminal with fzf otherwise gui menu." >&2
	echo " -s search with apropos." >&2
}

while getopts "st" curopt "$@"; do
	case "$curopt" in
		"s")
			issearch=1
			continue;;
		"t")
			isterminal=1
			continue;;
		"?")
			printusage
			exit 1;;
	esac
done

if [ ! -z "$issearch" ]; then
	if [ ! -z "$isterminal" ]; then
		printf "Enter query:\t"
		read query
	else
		query="$(echo "" | rofi -dmenu -p "Enter apropos query" -i)"
	fi
	if [ -z "$query" ]; then
		echo "Empty query" >&2
		exit 1
	fi
	list="$(apropos "$query")"
else
	list="$(man -k . | awk '{ print $1$2 }')"
fi

if [ ! -z "$isterminal" ]; then
	chosen=$(printf "$list" | fzf)
else
	chosen=$(printf "$list" | rofi -dmenu -p "Select man page" -i)
fi

if [ -z "$chosen" ]; then
    exit 0
fi

if [ ! -z "$issearch" ]; then
	chosen="$(printf "$chosen" | sed "s/\(.* ([a-zA-Z0-9]*)\)\s*-.*/\1/")"
fi

if [ ! -z "$isterminal" ]; then
	man "$chosen" && exit
fi

$TERMINAL $SHELL -ic "man \"$chosen\""