Skip to content

Instantly share code, notes, and snippets.

@sjas
Last active March 3, 2019 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjas/bc31c3540509ac8d80a20b664a1d55c3 to your computer and use it in GitHub Desktop.
Save sjas/bc31c3540509ac8d80a20b664a1d55c3 to your computer and use it in GitHub Desktop.
a better manpage interface
m() {
dpkg -s dialog &>/dev/null || { echo "Needs 'dialog' package which is not installed on this system!" && return; }
[[ $# -lt 1 ]] && echo 'Man page name consisting of a single word expected but not given.' && return
MANPAGE=$1
[[ -n $2 ]] && DEFAULTCHOICE=$2 || DEFAULTCHOICE='0'
man $MANPAGE > /dev/null 2>&1 || { echo $MANPAGE man page not present on this system!; return; }
mapfile -t OUTLINE < <(PAGER=cat man $MANPAGE | grep ^\\w | grep -v -e AUTHORS -e COPYRIGHT | sed '1d;$d')
RESULTSIZE=$(echo ${#OUTLINE[*]})
CHOICE=$(
dialog --keep-tite --default-item "$DEFAULTCHOICE" --menu "$(
echo -n $MANPAGE | tr '[a-z]' '[A-Z]'
) manpage contents" $(( $RESULTSIZE + 8 )) 35 $RESULTSIZE $(
COUNT=0
for i in $( seq 0 $(( ${#OUTLINE[@]} - 1 )) )
do
echo -n $COUNT
echo -n " $( echo "${OUTLINE[i]}" | sed 's/ /\_/g' ) "
(( COUNT++ ))
done
) 2>&1 >/dev/tty || echo CANCEL
)
[[ $CHOICE =~ "CANCEL" ]] && return
MANPAGER="less +/^$( echo ${OUTLINE[$CHOICE]} | sed 's/ /\\ /g' )" man $MANPAGE
m $MANPAGE $CHOICE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment