-
Notifications
You must be signed in to change notification settings - Fork 0
/
gman
executable file
·173 lines (151 loc) · 5.85 KB
/
gman
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# G-Man: A simple tool for quick & easy access to MAN pages in GNOME
#
# This tool is a graphical frontend which uses Zenity and basic command
# line tools to locate a MAN page in the local filesystem and display it
# with Yelp (the GNOME Help Center).
#
# Author: Dimitris Lampridis <dlampridis_at_gmail.com>
# (C) Copyright 2009 Dimitris Lampridis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# To begin with, set the app title and version
TITLE="G-Man"
VERSION="0.60"
# Set window title and window width
WINDOW_TITLE=$TITLE" (v"$VERSION")"
WINDOW_WIDTH=400
WINDOW_HEIGHT=250
# Paths to needed executables. Please adjust as necessary
ZENITY=/usr/bin/zenity
AWK=/usr/bin/awk
SED=/bin/sed
WHATIS=/usr/bin/whatis
MAN=/usr/bin/man
# Check if Zenity can be found. If not, produce error message
# (with "xmessage", since zenity is not available) and bail out
if [ ! -e "$ZENITY" ]; then
xmessage "ERROR: $ZENITY not found. You must install the Zenity\
package, or provide an alternative location"
exit
fi
# Check if Whatis can be found. If not, produce error message and bail out
if [ ! -e "$WHATIS" ]; then
$ZENITY --warning --text="ERROR: $WHATIS not found. You must install the \
man-db package, or provide an alternative location" --title="$WINDOW_TITLE"
exit
else
# Look whether $WHATIS is actually a script (man vs man-db) and flag it so
file $WHATIS | grep -qi "shell script"
if [ $? -eq 0 ]; then ISBINARY=0; else ISBINARY=1; fi;
fi
# Check if Awk can be found. If not, produce error message and bail out
if [ ! -e "$AWK" ]; then
$ZENITY --warning --text="ERROR: $AWK not found. You must install the \
Awk package, or provide an alternative location" --title="$WINDOW_TITLE"
exit
fi
# Check if Sed can be found. If not, produce error message and bail out
if [ ! -e "$SED" ]; then
$ZENITY --warning --text="ERROR: $SED not found. You must install the \
Sed package, or provide an alternative location" --title="$WINDOW_TITLE"
exit
fi
# Check if BROWSER is defined, else revert to default X sys browser
if [ -z $BROWSER ]; then
if [ -e "/usr/bin/x-www-browser" ]; then
XBROWSER=/usr/bin/x-www-browser
fi
fi
# Loop until a candidate page is found, or user cancels
CLI=0
PAGEFOUND=0
while [ $PAGEFOUND -eq 0 ]; do
# Check if user has provided a name as a command-line argument. If not,
# create a text entry dialog. In any case, save user input as $ENTRY.
if [ $# -gt 0 ] && [ $CLI -eq 0 ]; then
ENTRY=$1
CLI=1
else
ENTRY=$($ZENITY --width $WINDOW_WIDTH --entry --title="$WINDOW_TITLE" \
--text="Please enter a MAN page:")
fi
# If user aborts, zenity returns nothing, and $ENTRY will be zero length,
# so bail out
if [ -z "$ENTRY" ] ; then
exit
fi
# Check if there is any page at all
PAGECHECK=$($WHATIS $ENTRY | grep -v "nothing appropriate")
if [ -z "$PAGECHECK" ]; then
# Check whether we are running whatis from man or man-db.
# (Man cannot perform a substring search)
if [ $ISBINARY -eq 1 ]; then
# Ask user whether to do a substring search
$ZENITY --question --title="$WINDOW_TITLE" \
--text="No exact hit found. Would you like a substring search?"
if [ $? -eq 0 ]; then
# Redefine $WHATIS to treat $ENTRY as a regular
# expression and try again
WHATIS="$WHATIS -r"
REGEXPCHECK=$($WHATIS $ENTRY | grep -v "nothing appropriate")
# Nothing found...
if [ -z "$REGEXPCHECK" ]; then
$ZENITY --warning --text="Page \"$ENTRY\" not found" \
--title="$WINDOW_TITLE"
else
PAGEFOUND=1
fi
fi
else
$ZENITY --warning --text="Page \"$ENTRY\" not found" \
--title="$WINDOW_TITLE"
fi
else
PAGEFOUND=1
fi
done
# Count number of results (output lines of "whatis")
LINECOUNT=$($WHATIS $ENTRY | wc -l)
# If more than one line are returned, create a list window with zenity and
# save selection in $SELECTION
if [ $LINECOUNT -gt 1 ]; then
LISTMESSAGE="Multiple pages found. Please select one:"
SELECTION=$($WHATIS $ENTRY | \
awk 'BEGIN {FS="[ \t]+-[ \t]+"} {printf "%s\n%s\n", $1, $2}' | \
$ZENITY --width $WINDOW_WIDTH --height $WINDOW_HEIGHT \
--list --text="$LISTMESSAGE" --title="$WINDOW_TITLE" \
--column=Name/Section --column=Description)
# If user presses OK in the previous dialog without selecting
# an item from the list (or just presses Cancel), Zenity returns
# an empty string. Bail out (again)
if [ -z "$SELECTION" ]; then
exit
fi
# If only one result exists, then $SELECTION defaults to that
# result and no list window appears
else
SELECTION=$($WHATIS $ENTRY)
fi
# Finally, use awk to get all the relevant fields of the selected result
# First, we get the page name (first field of $SELECTION)
NAME=$(echo $SELECTION | $AWK '{print $1}')
# Then we get the section name (second field of $SELECTION)
SECTION=$(echo $SELECTION | $AWK '{print $2}' | $SED 's/[()]//g')
# Finally, call man and exit script
if [ -z $XBROWSER ]; then
$MAN -H $SECTION $NAME
else
$MAN --html=$XBROWSER $SECTION $NAME
fi