~tardypad/dotfiles

ref: 00c0c349a1b8960152233f11caa9ea1a40e395a3 dotfiles/files/scripts/shell/documentation-all -rwxr-xr-x 1.7 KiB
00c0c349 — Damien Tardy-Panis Add presentation mode to mako to hide notifications 4 months ago
                                                                                
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
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  CACHE_TOPICS_PATH="${XDG_CACHE_HOME:-$HOME/.cache}/documentation_topics"
  ZSH_VERSION=$( zsh --version | cut -d ' ' -f 2 )
  ZSH_HELPDIR="/usr/share/zsh/${ZSH_VERSION}/help"
  MAN_PATH="${MANPATH:-/usr/share/man}"

  BUILD_CACHE_ONLY=false
}

parse_command_line() {
  while getopts c OPT; do
    case "${OPT}" in
      c) BUILD_CACHE_ONLY=true ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))
}

# shellcheck disable=SC2120
exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

build_cache() {
  mkdir -p "$( dirname "${CACHE_TOPICS_PATH}" )"

  if [ "${BUILD_CACHE_ONLY}" = 'false' ]; then
    echo "Building list of topics..." >&2
  fi

  PATH_COMMANDS=$(
    printf '%s' "${PATH}" \
      | tr ':' '\n' \
      | xargs -I '[]' \
        -- find -L '[]' \
           -type f \
           -exec basename {} \; \
           2> /dev/null
  )

  ZSH_HELP_TOPICS=$( ls "${ZSH_HELPDIR}" )

  MAN_TOPICS=$(
    echo "${MAN_PATH}" \
      | tr ':' '\n' \
      | xargs -I{} find -L {} -type f \
      | sed -e '/.*\.gz$/s#.*/\([^/]*\)\.[^\.]*\.gz#\1#' \
            -e '/.*\.[0-9][^.]*$/s#.*/\([^/]*\)\.[0-9]*[^.]*$#\1#'
  )

  {
    echo "${PATH_COMMANDS}";
    echo "${ZSH_HELP_TOPICS}";
    echo "${MAN_TOPICS}";
  } \
  | sort -u \
  > "${CACHE_TOPICS_PATH}"
}

init_variables
parse_command_line "$@"

if [ "${BUILD_CACHE_ONLY}" = 'true' ]; then
  build_cache
  exit
fi

[ -f "${CACHE_TOPICS_PATH}" ] || build_cache

SELECTED_TOPIC=$(
  fzf --layout=reverse \
      --no-multi \
      --height=10 \
    < "${CACHE_TOPICS_PATH}"
)

if [ -n "${SELECTED_TOPIC}" ]; then
  documentation "${SELECTED_TOPIC}"
fi