Ubuntu Forums Archive Viewer

Shell script to browse installed package documentation

Archived thread 2112000 from Ubuntu Application Development. Markdown source: Ubuntu_Application_Development/thread_2112000_Shell_script_to_browse_installed_package_documenta.md

Original URL About this archive
#1

Attached tarball ([ATTACH]242174[/ATTACH]) contains shell scripts to browse documentation installed in /usr/share/doc

They are similar in function to debmany(1) from the package debian-goodies.

The main script is doc.bash. Just rename it 'doc' and put somewhere on $PATH. For convenience, I also prepared a very basic .deb with it. It's here (unsigned). The source package is there, too.

Contents of the tarball

  • doc.bash - this is all you need.
  • doc.sh - the same script rewritten for /bin/sh.
  • doc.posix.sh - ditto, strictly POSIX-compliant.
  • doc.legacy.sh - ditto, for legacy Bourne shell.
  • doc.select.* - dumbed down versions using 'select' (bash and zsh).
  • completion.* - completion code (bash and tcsh).
  • compctl.zsh - completion code for zsh, using the 'old' completion subsystem.
  • compsys.zsh - ditto, using the 'new' completion subsystem.
  • _doc - completion function for zsh (to put in $fpath).
  • doc - completion function for yash (to put in $YASH_LOADPATH).

For most purposes, the simplified versions (doc.select.*) should be enough. Below is the code of 'doc.select.bash'. It gives the idea of what the scripts do.


#!/bin/bash -e
[[ -n $1 ]] || { echo "Usage: $0 package_name" >&2; exit 1;}
docpfx=/usr/share/doc/
[[ -d $docpfx$1 ]] || { echo "Directory $docpfx$1 not found" >&2; exit 2;}
: ${COLUMNS:=`tput cols`}
GLOBIGNORE=AUTHORS*:ACK*:CONTRIBUTORS*:CREDITS*:THANKS*
lsopts='-Ldpq --group-directories-first'
shopt -s extglob nullglob checkwinsize
idx=index?(.[a-z][a-z]*).htm?(l) CDPATH=

docmenu () {
  pushd "$1" >/dev/null
  local PS3="...${PWD#$docpfx}> "
  # Grouping index.html first, directories next, then all the rest.
  local -a files=($idx)
  mapfile -tO${#files[@]} files < <(ls $lsopts !($idx))
  select file in "${files[@]}"; do
    [[ $REPLY == [Xx]* ]] && exit
    [[ -z $file ]] && break
    if [[ -d $file ]]; then
      docmenu "$file"
    else
      case $file in
        *.htm?(l)) sensible-browser "$file";;
        *)         sensible-pager   "$file";;
      esac
    fi
  done
  popd >/dev/null
}

pushd "$docpfx" >/dev/null
docmenu "$1"
popd >/dev/null

_doc(){ [[ $COMP_CWORD -eq 1 && $2 == -* ]] && COMPREPLY=(--auto-redisplay);}
complete -W "`cd /usr/share/doc; echo *`" -F _doc doc